hdoj 3549 Flow Problem 【最大流】
admin
2024-02-08 17:40:54

题目:hdoj 3549 Flow Problem

题意:给出一个图,让你求最大流。

分析:这个题目用dinci写的,因为点比较少,而dinci复杂度O(m*n^2),但是还是跑了160ms,不知道15的神牛怎么写的。

dinci的写法要注意的地方就是存图的时候要考虑怎么存,因为要更新网络残量,即反向的流量,所以这里要注意一下。

思想就不讲了,很多地方有讲。

代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define Del(a,b) memset(a,b,sizeof(a))
const int N = 20;
const int inf = 0x3f3f3f3f;
int n,m;
struct Node
{int from,to,cap,flow;
};
vector v[N];
vector e;
int vis[N];  //
void add_Node(int from,int to,int cap)
{e.push_back((Node){from,to,cap,0});e.push_back((Node){to,from,0,0});int tmp=e.size();v[from].push_back(tmp-2);v[to].push_back(tmp-1);
}
bool bfs(int s,int t)
{Del(vis,-1);queue q;q.push(s);vis[s] = 0;while(!q.empty()){int x=q.front();q.pop();for(int i=0;itmp.flow)  //第二个条件保证{vis[tmp.to]=vis[x]+1;q.push(tmp.to);}}}if(vis[t]>0)return true;return false;
}
int dfs(int o,int f,int t)
{if(o==t || f==0)return f;int a = 0,ans=0;for(int i=0;i0){tmp.flow+=a;e[v[o][i]^1].flow-=a; //存图方式ans+=a;f-=a;}}return ans;  //优化
}
int dinci(int s,int t)
{int ans=0;while(bfs(s,t)){int tm=dfs(s,inf,t);//printf("%d\n",tm);ans+=tm;}return ans;
}
int main()
{//freopen("Input.txt","r",stdin);int T;scanf("%d",&T);for(int cas=1;cas<=T;cas++){scanf("%d%d",&n,&m);for(int i=0;i                
            

相关内容

热门资讯

原创 餐... 2026年9月8日晚,南京浦口区泰冯路29号,墨语江南·阿婆菜餐厅户外帐篷里,灯火温润,其间还天降喜...
原创 它... 人到中年肝肾精血慢慢耗损,容易腰膝酸软、头晕眼花、精神疲惫。黄精性平,擅长补肝肾、益精血,搭配不同食...
同样卖奶茶,为何古茗赚了15亿... 出品/联商专栏 撰文/老刀 编辑/蔡建桢 今年上半年,蜜雪、古茗、霸王茶姬、茶百道、沪上阿姨全部盈利...
现烤散装走俏、新口味亮相、健康... 中秋佳节临近,成都月饼市场已全面开启“上新季”。记者连日来走访成都多家商超看到,月饼专区占据卖场核心...
散装月饼靠性价比当上“人气王” 华润超市货架上的散装月饼。 深圳报业集团记者 欧阳莹 摄 深圳晚报讯 (深圳报业集团记者 欧阳莹) ...