标签:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<cmath> 6 #include<algorithm> 7 #include<queue> 8 #define inf 0x7fffffff 9 using namespace std; 10 const int maxn=200+10; 11 const int M = 40000+100; 12 13 int n,m,from,to; 14 struct node 15 { 16 int v,flow,cost; 17 int next; 18 }edge[M*3]; 19 int head[maxn],edgenum; 20 int dis[maxn],pre[maxn],pid[maxn],vis[maxn]; 21 int Maxflow; 22 23 void add(int u,int v,int flow,int cost) 24 { 25 edge[edgenum].v=v ;edge[edgenum].flow=flow ; 26 edge[edgenum].cost=cost ;edge[edgenum].next=head[u]; 27 head[u]=edgenum++; 28 29 edge[edgenum].v=u ;edge[edgenum].flow=0; 30 edge[edgenum].cost=-cost ;edge[edgenum].next=head[v]; 31 head[v]=edgenum++; 32 } 33 34 int spfa() 35 { 36 for (int i=1 ;i<=to ;i++) dis[i]=inf; 37 memset(vis,0,sizeof(vis)); 38 queue<int> Q; 39 Q.push(from); 40 dis[from]=0; 41 vis[from]=1; 42 while (!Q.empty()) 43 { 44 int u=Q.front() ;Q.pop() ; 45 vis[u]=0; 46 for (int i=head[u] ;i!=-1 ;i=edge[i].next) 47 { 48 int v=edge[i].v; 49 if (edge[i].flow>0 && dis[v]>dis[u]+edge[i].cost) 50 { 51 dis[v]=dis[u]+edge[i].cost; 52 pre[v]=u; 53 pid[v]=i; 54 if (!vis[v]) 55 { 56 vis[v]=1; 57 Q.push(v); 58 } 59 } 60 } 61 } 62 return dis[to]; 63 } 64 65 int mincost() 66 { 67 int ans=0,maxflow=0; 68 int aug=0; 69 while (1) 70 { 71 aug=inf; 72 int tmp=spfa(); 73 if (tmp==inf) break; 74 for (int i=to ;i!=from ;i=pre[i]) 75 { 76 if (edge[pid[i] ].flow<aug) 77 aug=edge[pid[i] ].flow; 78 } 79 for (int i=to ;i!=from ;i=pre[i]) 80 { 81 edge[pid[i] ].flow -= aug; 82 edge[pid[i]^1 ].flow += aug; 83 } 84 maxflow += aug; 85 ans += tmp*aug; 86 } 87 Maxflow=maxflow; 88 return ans; 89 } 90 91 int main() 92 { 93 while (scanf("%d%d",&n,&m)!=EOF) 94 { 95 memset(head,-1,sizeof(head)); 96 edgenum=0; 97 int a,b,c; 98 from=2*n+1; 99 to=from+1; 100 Maxflow=0; 101 for (int i=0 ;i<m ;i++) 102 { 103 scanf("%d%d%d",&a,&b,&c); 104 add(a,b+n,1,c); 105 } 106 for (int i=1 ;i<=n ;i++) 107 { 108 add(from,i,1,0); 109 add(i+n,to,1,0); 110 } 111 int ans=mincost(); 112 printf("%d\n",Maxflow==n ? ans : -1); 113 } 114 return 0; 115 }
标签:
原文地址:http://www.cnblogs.com/huangxf/p/4334122.html