Thieves
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 421 Accepted Submission(s): 196
Problem Description
In the kingdom of Henryy, there are N (2 <= N <= 100) cities, with M (M <= 10000) two-direct ways connecting them.
A group of thieves from abroad plan to steal the metropolitan museum in city H (It has not been demolished). However, the brave, brilliant, bright police in the kingdom have known this plan long before, and they also plan to catch the thieves. The thieves are in the city S at present. The police try to catch them on their way from S to H. Although the thieves might travel this way by more than one group, our excellent police has already gather the statistics that the number of the people needed in city I (1<=I<=N) to arrest the thieves.
The police do not want to encounter the thieves in either city S or city H.
The police finish the task with the minimum number of people. Do you know the exact number?
Input
The first line contains an integer T (T <= 10), indicating the number of the test cases.
The first line of each test case has four integers: N, the number of the cities; M, the number of the roads; S (1<=S<=N), the label of city S; H (1<=T<=N, S≠H), the label of city H.
The second line contains N integers, indicating the number of people needed in each city. The sum of these N integers is less than 10000.
Then M lines followed, each containing two integers x and y, indicating that there is a two-direct roads between city x and y. Notices that no road between city S and H.
A blank line is followed after each test case.
Output
For each test case, output a single integer in a separate line, indicating the minimum number of people the police used.
Sample Input
1
5 5 1 5
1 6 6 11 1
1 2
1 3
2 4
3 4
4 5
Sample Output
Source
2010 ACM-ICPC Multi-University Training Contest(6)——Host by BIT
Recommend
zhouzeyong
刚开始看题目还是没有想到网络流。。。后来恍然大悟 一个简单的最小割,把点拆成I,I+N,中间连的点为费用,保证只被切到一次;如果A点和B点相连,2个点连边,边容量为无穷大,保证不被切到 另外对S,H点和自己S+N,H+N要连无穷大的边,保证不被割到 #include<cstdio> #include<cstring> #define min(a,b) a>b?b:a const int inf=0x7ffffff; int n,m,flow[210][210],dist[210],gap[210]; int find_path(int p, int limit=inf) { if(p==n - 1) return limit; for(int i=0; i < n; ++i) { if (dist[p] == dist[i] + 1 && flow[p][i] > 0) { int t = find_path(i, min(limit, flow[p][i])); if (t < 0) return t; if (t > 0) { flow[p][i] -= t; flow[i][p] += t; return t; } } } int label = n; for (int i = 0; i < n; ++i) if (flow[p][i] > 0) label = min(label, dist[i] + 1); if (--gap[dist[p]] == 0 || dist[0] >= n) return -1; ++gap[dist[p] = label]; return 0; } int iSAP() { gap[0] = n; int maxflow = 0,t = 0; while ((t = find_path(0)) >= 0) maxflow += t; return maxflow; } int main() { int T,s,t; scanf("%d",&T); while(T--) { memset(flow,0,sizeof(flow)); memset(gap,0,sizeof(gap)); memset(dist,0,sizeof(dist));//注意初始化 scanf("%d%d%d%d",&n,&m,&s,&t); for(int i=1; i<=n;i++) { int x; scanf("%d",&x); flow[i][i+n]=x; } for(int i=1;i<=m;i++) { int a,b; scanf("%d%d",&a,&b); flow[a+n][b]=inf; flow[b+n][a]=inf; } flow[0][s]=inf; flow[s][s+n]=inf; flow[t+n][n*2+1]=inf; flow[t][t+n]=inf; n=2*n+2; printf("%d/n",iSAP()); } return 0; }