算是一个模版题把。构造一个源点和汇点,所有的核电站都是从源点出发的,所有的用户都汇集到汇点,跑一次最大流就可以了。模版敲的不是很熟。
1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 using namespace std; 5 const int INF = 0xffffff; 6 int flow[201][201],low[201],path[201]; 7 int str,end,n; 8 int bfs() 9 { 10 int t,i; 11 queue<int> que; 12 memset(path,-1,sizeof(path)); 13 que.push(str); 14 low[str] = INF; 15 while(!que.empty()) 16 { 17 t = que.front(); 18 que.pop(); 19 if(t == end) break; 20 for(i = 0; i <= n+1; i ++) 21 { 22 if(i != str&&path[i] == -1&&flow[t][i]) 23 { 24 path[i] = t; 25 low[i] = low[t] < flow[t][i] ? low[t]:flow[t][i]; 26 que.push(i); 27 } 28 } 29 } 30 if(path[end] == -1) 31 return -1; 32 else 33 return low[end]; 34 } 35 int EK() 36 { 37 int ans = 0,now,res; 38 while((res = bfs()) != -1) 39 { 40 ans += res; 41 now = end; 42 while(now != str) 43 { 44 flow[path[now]][now] -= res; 45 flow[now][path[now]] += res; 46 now = path[now]; 47 } 48 } 49 return ans; 50 } 51 inline void clean() 52 { 53 char ch; 54 for(;;) 55 { 56 scanf("%c",&ch); 57 if(ch == '(') break; 58 } 59 } 60 int main() 61 { 62 int i,m,sv,ev,w,np,nc; 63 while(scanf("%d%d%d%d%*c",&n,&np,&nc,&m)!=EOF) 64 { 65 memset(flow,0,sizeof(flow)); 66 for(i = 1; i <= m; i ++) 67 { 68 clean(); 69 scanf("%d,%d)%d",&sv,&ev,&w); 70 flow[sv][ev] += w; 71 } 72 for(i = 1; i <= np; i ++) 73 { 74 clean(); 75 scanf("%d)%d",&ev,&w); 76 flow[n][ev] += w; 77 } 78 for(i = 1; i <= nc; i ++) 79 { 80 clean(); 81 scanf("%d)%d%*c",&sv,&w); 82 flow[sv][n+1] += w; 83 } 84 str = n; 85 end = n+1; 86 printf("%d\n",EK()); 87 } 88 return 0; 89 }