其实还是不是很懂dinic-----
抄了一个模板---
http://www.cnblogs.com/naturepengchen/articles/4403408.html
先放在这里~~~
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #include<queue> 6 using namespace std; 7 8 const int maxn = 10005; 9 const int INF = (1 << 30) - 1; 10 11 struct Edge{ 12 int v,next,c; 13 }e[maxn]; 14 15 int st,ed,lev[maxn],first[maxn],now[maxn],ecnt; 16 int n,m; 17 18 void init(int a,int b){ 19 st = a; ed = b; 20 memset(first,-1,sizeof(first)); 21 ecnt = 0; 22 } 23 24 void addedges(int u,int v,int c){ 25 e[ecnt].next = first[u]; 26 e[ecnt].v = v; 27 e[ecnt].c = c; 28 first[u] = ecnt++; 29 30 e[ecnt].next = first[v]; 31 e[ecnt].v = u; 32 e[ecnt].c = 0; 33 first[v] = ecnt++; 34 } 35 36 bool bfs(){ 37 queue<int> q; 38 while(!q.empty()) q.pop(); 39 q.push(st); 40 memset(lev,-1,sizeof(lev)); 41 lev[st] = 0; 42 while(!q.empty()){ 43 int x = q.front();q.pop(); 44 for(int i = first[x];~i;i = e[i].next){ 45 int v = e[i].v; 46 if(lev[v] < 0 && e[i].c > 0){ 47 lev[v] = lev[x] + 1; 48 q.push(v); 49 } 50 } 51 } 52 return lev[ed] != -1; 53 } 54 55 int dfs(int p,int minf){ 56 if(p == ed || minf == 0) return minf; 57 for(int &i = now[p];~i;i = e[i].next){ 58 int v = e[i].v; 59 if(lev[v] == lev[p] + 1 && e[i].c > 0){ 60 int d = dfs(v,min(e[i].c,minf)); 61 if(d > 0){ 62 e[i].c -= d; 63 e[i^1].c += d; 64 return d; 65 } 66 } 67 } 68 return 0; 69 } 70 71 int dinic(){ 72 int max_flow = 0,p1; 73 while(bfs()){ 74 memcpy(now,first,sizeof(first)); 75 while((p1 = dfs(st,INF)) > 0) 76 max_flow += p1; 77 } 78 return max_flow; 79 } 80 81 int main(){ 82 int T; 83 int kase = 0; 84 scanf("%d",&T); 85 while(T--){ 86 scanf("%d %d",&n,&m); 87 init(1,n); 88 for(int i = 0;i < m;i++){ 89 int a,b,c; 90 scanf("%d %d %d",&a,&b,&c); 91 addedges(a,b,c); 92 } 93 printf("Case %d: %d ",++kase,dinic()); 94 } 95 return 0; 96 }