Thieves
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1535 Accepted Submission(s): 692
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
11
#include<cstdio> #include<cstring> #include<queue> #include<algorithm> using namespace std; #define MAXN 1010 #define MAXM 1000000 #define INF 0x3f3f3f3f int dis[MAXN],vis[MAXN],map[MAXN][MAXN],cur[MAXN]; int n,S,H,m,cnt,head[MAXN]; struct node { int u,v,cap,flow,next; }edge[MAXM]; void add(int a,int b,int c) { node E={a,b,c,0,head[a]}; edge[cnt]=E; head[a]=cnt++; node E1={b,a,0,0,head[b]}; edge[cnt]=E1; head[b]=cnt++; } void getmap() { cnt=0; memset(head,-1,sizeof(head)); scanf("%d%d%d%d",&n,&m,&S,&H); for(int i=1;i<=n;i++) { int a; scanf("%d",&a); if(i==S||i==H) add(i,i+n,INF); else add(i,i+n,a); } for(int i=0;i<m;i++) { int a,b; scanf("%d%d",&a,&b); add(a+n,b,INF); add(b+n,a,INF); } } bool BFS(int s,int t) { memset(dis,-1,sizeof(dis)); memset(vis,0,sizeof(vis)); queue<int>q; q.push(s); vis[s]=1; dis[s]=0; while(!q.empty()) { int u=q.front(); q.pop(); for(int i=head[u];i!=-1;i=edge[i].next) { node E=edge[i]; if(!vis[E.v]&&E.cap>E.flow) { dis[E.v]=dis[E.u]+1; vis[E.v]=1; if(E.v==t) return true; q.push(E.v); } } } return false; } int DFS(int x,int a,int e) { if(a==0||x==e) return a; int flow=0,f; for(int &i=cur[x];i!=-1;i=edge[i].next) { node &E=edge[i]; if(dis[x]+1==dis[E.v]&&(f=DFS(E.v,min(E.cap-E.flow,a),e))>0) { a-=f; flow+=f; edge[i].flow+=f; edge[i^1].flow-=f; if(a==0) break; } } return flow; } int MAXflow(int s,int t) { int flow=0; while(BFS(s,t)) { memcpy(cur,head,sizeof(head)); flow+=DFS(s,INF,t); } return flow; } int main() { int t; scanf("%d",&t); while(t--) { getmap(); printf("%d\n",MAXflow(S,H+n)); } return 0; }