标签:
Time Limit: 2MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
Description
Input
Output
Sample Input
4 5 1 2 1 2 3 1 3 4 1 1 3 2 2 4 2
Sample Output
6
Source
1 /*by SilverN*/ 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 #include<queue> 8 using namespace std; 9 const int INF=1000000; 10 const int mxn=2100; 11 int head[mxn],dis[mxn],pr[mxn]; 12 bool inqu[mxn]; 13 int n,m; 14 int s,t; 15 int ans; 16 int cnt=1; 17 struct edge{ 18 int from,to,next,v,c; 19 }e[mxn*30]; 20 void add_edge(int f,int t,int v,int c){ 21 e[++cnt]=(edge){f,t,head[f],v,c};head[f]=cnt; 22 e[++cnt]=(edge){t,f,head[t],0,-c};head[t]=cnt; 23 } 24 bool SPFA(){ 25 queue<int>q; 26 memset(inqu,false,sizeof(inqu)); 27 for(int i=0;i<=t;i++)dis[i]=INF; 28 dis[s]=0; 29 inqu[s]=1; 30 q.push(s); 31 int i,j; 32 while(!q.empty()){ 33 int u=q.front();q.pop(); 34 inqu[u]=false; 35 for(i=head[u];i;i=e[i].next){ 36 int v=e[i].to; 37 if(e[i].v && dis[u]+e[i].c<dis[v]){ 38 dis[v]=dis[u]+e[i].c; 39 pr[v]=i; 40 if(!inqu[v]){ 41 q.push(v); 42 inqu[v]=true; 43 } 44 } 45 } 46 } 47 return dis[t]!=INF; 48 } 49 void mcf(){ 50 int i; 51 while(SPFA()){ 52 int temp=INF; 53 for(i=pr[t];i;i=e[pr[i]].from)temp=min(temp,e[i].v); 54 ans+=dis[t]*temp; 55 for(i=pr[t];i;i=e[pr[i]].from){ 56 e[i].v-=temp; 57 e[i^1].v+=temp; 58 } 59 } 60 } 61 int main(){ 62 scanf("%d%d",&n,&m); 63 s=0;t=n+1; 64 int x,y,c; 65 for(int i=1;i<=m;i++){ 66 scanf("%d%d%d",&x,&y,&c); 67 add_edge(x,y,1,c); 68 add_edge(y,x,1,c); 69 } 70 add_edge(s,1,2,0); 71 add_edge(n,t,2,0); 72 mcf(); 73 printf("%d\n",ans); 74 }
标签:
原文地址:http://www.cnblogs.com/SilverNebula/p/5592535.html