题目链接:HDU6214
留一个链式前向星+Dinic模板(希望不要被某人发现,嘿嘿嘿)。
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <vector> 5 #include <queue> 6 using namespace std; 7 #define next Next 8 const int inf = 0x3f3f3f3f; 9 const int maxn=205; 10 int level[maxn]; 11 int iter[maxn]; 12 int head[maxn],tot; 13 struct edge{ 14 int to,cap,Next; 15 } e[2005]; ///此处应为边的两倍,加一条容量为0的反向边 16 void init(){ 17 memset(head,-1,sizeof(head)); 18 tot=0; 19 } 20 void add(int from,int to,int cap){ 21 e[tot].Next=head[from]; 22 e[tot].to=to; 23 e[tot].cap=cap; 24 head[from]=tot; 25 tot++; 26 } 27 void addedge(int from,int to,int cap){ 28 add(from,to,cap); 29 add(to,from,0); 30 } 31 void bfs(int s){ 32 memset(level,-1,sizeof(level)); 33 queue<int> q; 34 level[s]=0; 35 q.push(s); 36 while(!q.empty()){ 37 int v=q.front(); q.pop(); 38 for(int i=head[v];~i;i=e[i].Next){ 39 edge &ed=e[i]; 40 if(ed.cap>0&&level[ed.to]<0){ 41 level[ed.to]=level[v]+1; 42 q.push(ed.to); 43 } 44 } 45 } 46 } 47 int dfs(int v,int t,int f){ 48 if(v==t) return f; 49 for(int &i=iter[v];~i;i=e[i].Next){ 50 edge &ed=e[i]; 51 if(ed.cap>0&&level[v]<level[ed.to]){ 52 int d=dfs(ed.to,t,min(f,ed.cap)); 53 if(d>0){ 54 ed.cap-=d; 55 e[i^1].cap+=d; 56 return d; 57 } 58 } 59 } 60 return 0; 61 } 62 int max_flow(int s,int t){ 63 int flow=0; 64 while(1){ 65 bfs(s); 66 if(level[t]<0) return flow; 67 memcpy(iter,head,sizeof(iter)); 68 int f; 69 while((f=dfs(s,t,inf))>0){ 70 flow+=f; 71 } 72 } 73 } 74 int main() 75 { 76 int n,m,T; 77 scanf("%d",&T); 78 while(T--) 79 { 80 init(); 81 int s,t; 82 scanf("%d %d",&n,&m); 83 scanf("%d %d",&s,&t); 84 for(int i=1;i<=m;i++) 85 { 86 int u,v,w; 87 scanf("%d %d %d",&u,&v,&w); 88 addedge(u,v,w*300+1); 89 } 90 int ans = max_flow(s,t); 91 printf("%d\n",ans%300); 92 } 93 return 0; 94 }