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                
            

相关内容

热门资讯

赤水性价比粮食酒推荐:2025... 赤水性价比粮食酒推荐:2025年酱香酒选购全攻略 一、开篇背景与市场痛点 2025年的赤水河流域酒类...
非白酒板块11月19日跌0.3... 证券之星消息,11月19日非白酒板块较上一交易日下跌0.33%,*ST椰岛领跌。当日上证指数报收于3...
以运河文化赋能产业发展|古贝春... 11月17日至19日,以“新质开新局,聚力创未来”为主题的2025年第六届中国白酒黄淮核心产区高质量...
深夜小酌的灵魂搭档:油炝脆骨,... 油炝脆骨是一道充满锅气与烟火气息的家常菜,以其爽脆的口感和浓郁的香辣风味深受许多人喜爱。这道菜的制作...
初中毕业新征程:为什么西点烘焙... 站在初中毕业的人生路口,许多女孩都在思考:哪条路能通往一个既美好又独立的未来?如果有一条道路,能将女...