标签:
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 22294 | Accepted: 5916 |
Description
Input
Output
Sample Input
1 3 3 1 2 3 1 3 4 2 3 5
Sample Output
Scenario #1: 4
Source
附上代码:
1 #include<iostream> 2 #include<queue> 3 #include<cstring> 4 #include<cstdio> 5 #include<climits> 6 #define MAXE 1010*1010*2 7 #define MAXP 1010 8 #define Max(a,b) a>b?a:b 9 #define Min(a,b) a<b?a:b 10 using namespace std; 11 struct Edge 12 { 13 int s,t,f,next; 14 } edge[MAXE]; 15 int head[MAXP]; 16 int cur[MAXP]; 17 int pre[MAXP]; 18 int stack[MAXE]; 19 int used[MAXP]; 20 int ent; 21 int maxn; 22 int n,m,s,t; 23 int num; 24 void add(int start,int last,int f) 25 { 26 edge[ent].s=start; 27 edge[ent].t=last; 28 edge[ent].f=f; 29 edge[ent].next=head[start]; 30 head[start]=ent++; 31 edge[ent].s=last; 32 edge[ent].t=start; 33 edge[ent].f=0; 34 edge[ent].next=head[last]; 35 head[last]=ent++; 36 } 37 bool bfs(int S,int T) 38 { 39 memset(pre,-1,sizeof(pre)); 40 pre[S]=0; 41 queue<int>q; 42 q.push(S); 43 while(!q.empty()) 44 { 45 int temp=q.front(); 46 q.pop(); 47 for(int i=head[temp]; i!=-1; i=edge[i].next) 48 { 49 int temp2=edge[i].t; 50 if(pre[temp2]==-1&&edge[i].f>maxn) 51 { 52 pre[temp2]=pre[temp]+1; 53 q.push(temp2); 54 } 55 } 56 } 57 return pre[T]!=-1; 58 } 59 void dinic(int start,int last) 60 { 61 int flow=0,now; 62 maxn=0; 63 while(bfs(start,last)) 64 { 65 int top=0; 66 memcpy(cur,head,sizeof(head)); 67 int u=start; 68 while(1) 69 { 70 if(u==last)//如果找到终点结束对中间路径进行处理并计算出该流 71 { 72 int minn=INT_MAX; 73 for(int i=0; i<top; i++) 74 { 75 if(minn>edge[stack[i]].f) 76 { 77 minn=edge[stack[i]].f; 78 now=i; 79 } 80 } 81 maxn=Max(maxn,minn); 82 edge[stack[now]].f=edge[stack[now]^1].f=0; 83 top=now; 84 u=edge[stack[top]].s; 85 } 86 for(int i=cur[u]; i!=-1; cur[u]=i=edge[i].next) //找出从u点出发能到的边 87 if(edge[i].f&&pre[edge[i].t]==pre[u]+1) 88 break; 89 if(cur[u]==-1)//如果从该点未找到可行边,将该点标记并回溯 90 { 91 if(top==0)break; 92 pre[u]=-1; 93 u=edge[stack[--top]].s; 94 } 95 else//如果找到了继续运行 96 { 97 stack[top++]=cur[u]; 98 u=edge[cur[u]].t; 99 } 100 } 101 } 102 } 103 int main() 104 { 105 int cas; 106 cin>>cas; 107 int sum=1; 108 while(cas--) 109 { 110 memset(head,-1,sizeof(head)); 111 ent=0; 112 scanf("%d%d",&n,&m); 113 s=1;t=n; 114 int u,v,flow; 115 for(int i=0;i<m;i++) 116 { 117 scanf("%d%d%d",&u,&v,&flow); 118 add(u,v,flow); 119 add(v,u,flow); 120 } 121 printf("Scenario #%d:\n",sum++); 122 dinic(s,t); 123 printf("%d\n\n",maxn); 124 } 125 return 0; 126 }
hdu 1797 靠谱的算法应该是最大生成树,但是本人用最大流做的
标签:
原文地址:http://www.cnblogs.com/lthb/p/4495600.html