1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#include <iostream> #include <cstdio> #include <cmath> #include <string> #include <cstring> #include <algorithm> #include <limits> #include <vector> #include <stack> #include <queue> #include <set> #include <map> #define lowbit(x) ( x&(-x) ) #define INF 1e9+7 #define pi 3.141592653589793 #define e 2.718281828459045 using namespace std; typedef long long ll; const int maxN=205; int N,M; queue<int>q; int flow[maxN][maxN], dis[maxN];//dis代表的是这个点的层 bool bfs() { memset(dis,-1,sizeof dis); while(!q.empty()) q.pop(); dis[1]=0; q.push(1); while(!q.empty()){ int u=q.front(); q.pop(); for(int i=1;i<=M;i++) { if(flow[u][i]&&dis[i]==-1) { dis[i]=dis[u]+1; q.push(i); } } } return dis[M]!=-1; } int dfs(int x,int remain) { if(x==M||remain==0) return remain; int k; for(int i=1;i<=M;i++){ if(flow[x][i]>0&&dis[i]==dis[x]+1&&(k=dfs(i,min(remain,flow[x][i])))) { flow[x][i]-=k; flow[i][x]+=k; return k; } } return 0; } int main() { while(~scanf("%d%d",&N,&M)) { memset(flow,0,sizeof flow); for(int i=0;i<N;i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); flow[a][b]+=c; //这个点坑了我很久后来是看了别人的博客才发现的错误。 } //所以如果这样赋值的话我们有向图的反向边也不能直接赋值为0了。 int ans=0; int res; while(bfs())//dinic开始分层 { while(res=dfs(1,INF)) ans+=res; } printf("%dn",ans); } }
|
近期评论