标签:
对于30%的数据,N ≤ 20,M ≤ 120。
对于100%的数据,N ≤ 200,M ≤ 20000。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<algorithm> 6 #include<cmath> 7 #include<vector> 8 #include<queue> 9 #define inf 1000007 10 #define MOD 500 11 using namespace std; 12 int n,m,x,y,z,cnt,ans1,ans2,dis[501],path[501],head[501],q[501]; 13 bool v[501]; 14 int next[100000],from[100000],list[100000],flow[100000],cost[100000]; 15 void insert(int x,int y,int w,int z) 16 { 17 next[++cnt]=head[x]; 18 head[x]=cnt; 19 from[cnt]=x; 20 list[cnt]=y; 21 flow[cnt]=w; 22 cost[cnt]=z; 23 } 24 bool spfa() 25 { 26 int t=0,w=1,x; 27 for (int i=1;i<=2*n;i++) dis[i]=inf; 28 memset(v,0,sizeof(v)); 29 q[1]=1; dis[1]=0; v[1]=1; 30 while (t!=w) 31 { 32 t=(t+1)%MOD; 33 x=q[t]; 34 for (int i=head[x];i;i=next[i]) 35 if (flow[i]>0&&dis[x]+cost[i]<dis[list[i]]) 36 { 37 dis[list[i]]=dis[x]+cost[i]; 38 path[list[i]]=i; 39 if (!v[list[i]]) 40 { 41 v[list[i]]=1; 42 w=(w+1)%MOD; 43 q[w]=list[i]; 44 } 45 } 46 v[x]=0; 47 } 48 return dis[2*n]!=inf; 49 } 50 void mcf() 51 { 52 int x=inf,i; 53 i=path[2*n]; 54 while (i) 55 { 56 x=min(x,flow[i]); 57 i=path[from[i]]; 58 } 59 ans1++; 60 i=path[2*n]; 61 while (i) 62 { 63 ans2+=x*cost[i]; 64 flow[i]-=x; 65 flow[i^1]+=x; 66 i=path[from[i]]; 67 } 68 } 69 int main() 70 { 71 scanf("%d%d",&n,&m); 72 cnt=1; 73 for (int i=1;i<=m;i++) 74 { 75 scanf("%d%d%d",&x,&y,&z); 76 insert(x+n,y,1,z); 77 insert(y,x+n,0,-z); 78 } 79 for (int i=2;i<n;i++) 80 { 81 insert(i,i+n,1,0); 82 insert(i+n,i,0,0); 83 } 84 insert(1,n+1,inf,0); 85 insert(n+1,1,0,0); 86 insert(n,2*n,inf,0); 87 insert(2*n,n,0,0); 88 while (spfa()) mcf(); 89 printf("%d %d",ans1,ans2); 90 return 0; 91 }
标签:
原文地址:http://www.cnblogs.com/ws-fqk/p/4657160.html