又到了新专题的刷模板题环节
看了好久的网络流才稍微看懂了一点
对着打了下模板
哎图论太难了
题目思路
套模板就好
ac代码
#include <stdio.h>#include <iostream>#include <algorithm>#include <math.h>#include <string.h>#include <vector>#include <stack>#include <queue>#include <map>#include <set>#include <utility>#define pi 3.1415926535898#define ll long long#define lson rt<<1#define rson rt<<1|1#define eps 1e-6#define ms(a,b) memset(a,b,sizeof(a))#define legal(a,b) a&b#define print1 printf(\"111\\n\")using namespace std;const int maxn = 2e5+10;const int inf = 0x3f3f3f3f;const ll llinf =0x3f3f3f3f3f3f3f3f;const int mod = 1e9+7;struct node{int to,next,v;}e[maxn];int n,m,len,first[maxn];int d[maxn];void add(int u,int v,int w){e[len].to=v;e[len].next=first[u];e[len].v=w;first[u]=len++;}bool makelevel(int s,int t){ms(d,0);queue<int>q;d[s]=1;q.push(s);while(!q.empty()){int x=q.front();q.pop();if(x==t)return true;for(int i=first[x];i!=-1;i=e[i].next){if(d[e[i].to]==0&&e[i].v!=0){q.push(e[i].to);d[e[i].to]=d[x]+1;//进行分层操作 在后面搜索的时候只有满足d[e[i].to]==d[x]+1的路才能走}}}return false;}int dfs(int x,int flow,int t){if(x==t)return flow;//到达了终点就返回流过去的值int sum=0;for(int i=first[x];i!=-1;i=e[i].next){if(e[i].v!=0&&d[e[i].to]==d[x]+1)//判断是否可以流{int tem=dfs(e[i].to,min(flow-sum,e[i].v),t);e[i].v-=tem;e[i^1].v+=tem;sum+=tem;if(sum==flow)return sum;}}return sum;}int main(){while(~scanf(\"%d%d\",&n,&m)&&n+m){ms(first,-1);for(int i=1;i<=n;i++){int x,y,z;scanf(\"%d%d%d\",&x,&y,&z);add(x,y,z);add(y,x,0);//存边的时候反向边要标为0 避免影响搜索时的结果}int ans=0;while(makelevel(1,m))//判断网络里面是否还有增广路{ans+=dfs(1,inf,m);}printf(\"%d\\n\",ans);}}