http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3130
【题意】: 给一个有向图 每条边有费用 求 s到e 的两条总花费最少的 完全没有交点的 路径
【思路】:把每条路径 的容量赋值为1 于是每条边就只能添加一次了 再每次增广 增广了两次即找到了两条路径就 输出答案了
1 #include<iostream> 2 #include<vector> 3 #include<string.h> 4 #include<queue> 5 #include<stdio.h> 6 using namespace std; 7 8 #define INF 100000000 9 struct E{int from;int to;int c;int f;int cost;}; 10 vector <int> g[202]; 11 vector<E> edge; 12 int d[202],vis[202],p[202];//cur[102],num[102],p[102],w[1102],n; 13 int n,m; 14 queue<int > q; 15 16 17 void add(int from,int to,int c,int cost) 18 { 19 E temp1,temp2; 20 temp1.from =from; temp1.to=to; temp1.c=c;temp1.f=0; temp1.cost=cost; 21 temp2.from =to; temp2.to=from; temp2.c=0;temp2.f=0; temp2.cost=-cost; 22 edge.push_back (temp1); 23 edge.push_back (temp2); 24 int m=edge.size (); 25 g[from].push_back (m-2); 26 g[to].push_back (m-1); 27 } 28 29 int solve(int s,int e) 30 { 31 int maxflow=0; 32 int mincost=0; 33 while(1) 34 { 35 memset(vis,0,sizeof(vis)); 36 for(int i=0;i<=n;i++) 37 d[i]=INF; 38 d[s]=0; p[s]=-1;//!!!! 39 q.push(s); vis[s]=1; 40 while(!q.empty()) 41 { 42 int temp; 43 temp=q.front(); q.pop(); vis[temp]=0; 44 for(int i=0;i<g[temp].size();i++) 45 { 46 int v; 47 v=g[temp][i];//di v tiao bian bian t 48 E t=edge[v]; 49 if(t.c>t.f&&d[t.from]+t.cost<d[t.to]) 50 { 51 d[t.to]=d[t.from]+t.cost; 52 p[t.to]=v; 53 if(vis[t.to]==0) 54 { 55 q.push(t.to); 56 vis[t.to]=1; 57 58 } 59 } 60 61 } 62 } 63 if(d[e]==INF) 64 break; 65 int a=INF; 66 for(int u=e;u!=s;u=edge[p[u]].from) 67 { 68 mincost+=edge[p[u]].cost; 69 edge[p[u]].f+=1; 70 edge[p[u]^1].f-=1; 71 } 72 maxflow++; 73 if(maxflow==2) 74 return mincost; 75 } 76 return -1; 77 } 78 79 void init(int n) 80 { 81 for(int i=0;i<=n;i++) 82 g[i].clear(); 83 edge.clear(); 84 } 85 86 87 int main() 88 { 89 int p=1,a,b,c; 90 while(1) 91 { 92 scanf("%d%d",&n,&m); 93 if(n==0&&m==0) 94 break; 95 init(n); 96 while(m--) 97 { 98 scanf("%d%d%d",&a,&b,&c); 99 add(a,b,1,c); 100 } 101 int pp; 102 pp=solve(0,n-1); 103 printf("Instance #%d: ",p++); 104 if(pp==-1) 105 printf("Not possible "); 106 else 107 printf("%d ",pp); 108 } 109 return 0; 110 }