看到Ci最大可以到10 000 000,N<=200,M<=200,我以为用这个算法会超时,据说该算法的复杂度是N*M*C,C为最大流量值。
但是显然没超,可能是数据弱神马的,不知道
没什么思路说的,就是裸的网络最大流,我的代码和上一个题 pigs(麦克卖猪)的代码基本上相同,下面贴代码:
View Code
1 #include <cstdio> 2 #include <cstring> 3 #define MAXN 205 4 #define MAXM 205 5 #define INF 300000000 6 struct Arc 7 { 8 int c,f; 9 } edge[MAXN][MAXN]; 10 int n,m; 11 void init() 12 { 13 int i; 14 memset(edge,0,sizeof(edge)); 15 for(i =0; i < m; ++i) 16 { 17 int u,v,c; 18 scanf("%d%d%d",&u,&v,&c); 19 edge[u][v].c += c; 20 } 21 } 22 void Ford() 23 { 24 int prev[MAXN]; 25 int alpha[MAXN]; 26 int queue[MAXN]; 27 int i,j; 28 while(1) 29 { 30 int front=0,tail=0; 31 memset(prev,0xff,sizeof(prev)); 32 queue[tail++] = 1; 33 alpha[1] = INF; 34 prev[1] = -2; 35 while(front != tail && prev[n] == -1) 36 { 37 int v = queue[front++]; 38 for(i = 1; i <= n; i++) 39 { 40 int tmp; 41 if(prev[i] == -1 && (tmp = edge[v][i].c-edge[v][i].f)) 42 { 43 prev[i] = v; 44 queue[tail++] = i; 45 alpha[i] = alpha[v] < tmp ? alpha[v]:tmp; 46 } 47 } 48 } 49 if(prev[n] == -1) break; 50 for(i = prev[n],j = n; i != -2; j = i, i = prev[i]) 51 { 52 edge[i][j].f += alpha[n]; 53 edge[j][i].f = -edge[i][j].f; 54 } 55 } 56 int p=0; 57 for(i=1; i<n; i++) 58 p += edge[i][n].f; 59 printf("%d\n",p); 60 } 61 int main() 62 { 63 // freopen("in.cpp","r",stdin); 64 while(~scanf("%d%d",&m,&n)) 65 { 66 init(); 67 Ford(); 68 } 69 return 0; 70 }