Control
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2623 Accepted Submission(s): 1133
Problem Description
You, the head of Department of Security, recently received a top-secret information that a group of terrorists is planning to transport some WMD
1
from one city (the source) to another one (the destination). You know their date, source and destination, and they are using the highway network.
The highway network consists of bidirectional highways, connecting two distinct city. A vehicle can only enter/exit the highway network at cities only.
You may locate some SA (special agents) in some selected cities, so that when the terrorists enter a city under observation (that is, SA is in this city), they would be caught immediately.
It is possible to locate SA in all cities, but since controlling a city with SA may cost your department a certain amount of money, which might vary from city to city, and your budget might not be able to bear the full cost of controlling all cities, you must identify a set of cities, that:
* all traffic of the terrorists must pass at least one city of the set.
* sum of cost of controlling all cities in the set is minimal.
You may assume that it is always possible to get from source of the terrorists to their destination.
------------------------------------------------------------
1
Weapon of Mass Destruction
Input
There are several test cases.
The first line of a single test case contains two integer N and M ( 2 <= N <= 200; 1 <= M <= 20000), the number of cities and the number of highways. Cities are numbered from 1 to N.
The second line contains two integer S,D ( 1 <= S,D <= N), the number of the source and the number of the destination.
The following N lines contains costs. Of these lines the ith one contains exactly one integer, the cost of locating SA in the ith city to put it under observation. You may assume that the cost is positive and not exceeding 10
7.
The followingM lines tells you about highway network. Each of these lines contains two integers A and B, indicating a bidirectional highway between A and B.
Please process until EOF (End Of File).
Output
For each test case you should output exactly one line, containing one integer, the sum of cost of your selected set.
See samples for detailed information.
Sample Input
5 6
5 3
5
2
3
4
12
1 5
5 4
2 3
2 4
4 3
2 1
Sample Output
Source
2012 ACM/ICPC Asia Regional Chengdu Online
一个图有n个点m条边,消去每一个点都需要一定的花费,然后给你s,d,你可以消除一些点使得s,d不连通,一般的拆点最小割,将i看成两个点,建图i--i+n边权为对应的花费
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#define MAXN 500+10
#define MAXM 2500000
#define INF 0x3f3f3f3f
int n,m,vis[MAXN],head[MAXN],dis[MAXN],cur[MAXN],cnt,source,sink,s,d;
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()
{
scanf("%d%d",&s,&d);
add(source,s,INF);
add(d+n,sink,INF);
int a,b,c;
for(int i=1;i<=n;i++)
{
scanf("%d",&a);
add(i,i+n,a);
}
for(int i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
add(a+n,b,INF);
add(b+n,a,INF);
}
}
bool BFS(int s,int t)
{
queue<int>q;
memset(vis,0,sizeof(vis));
memset(dis,-1,sizeof(dis));
while(!q.empty()) q.pop();
dis[s]=0;
vis[s]=1;
q.push(s);
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)
{
vis[E.v]=1;
dis[E.v]=dis[E.u]+1;
if(E.v==t) return true;
q.push(E.v);
}
}
}
return false;
}
int DFS(int x,int a,int e)
{
if(x==e||a==0) 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()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
cnt=0;
memset(head,-1,sizeof(head));
source=0,sink=2*n+1;
getmap();
printf("%d\n",MAXflow(0,2*n+1));
}
return 0;
}