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 107.
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
3
Source
2012 ACM/ICPC Asia Regional Chengdu Online
求也就是最小花费,也就是最小割,相当于求最大流,网络流问题。
将N个点进行拆点——>2*N个点(maxn=2*N)
边的数量为2e4,双向加边,此时为8e4,因为存在拆点的情况,所以maxm=8e4+maxn。
构图,跑ISAP。
有一个点需要注意,因为是加的双向边,加边的时候正向从一个边的后端到另一个边的前段,加反向边的时候从后者后端到前者的前段加边。
addedge(2*u,2*v-1,inf);
addedge(2*v,2*u-1,inf);
#include<stdio.h>
#include<math.h>
#include<string>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<stack>
#include<queue>
#include<map>
#include<vector>
using namespace std;
const int maxn=405;
const int maxm=8e4+maxn;
const int inf=0x3f3f3f3f;
int N,M,s,D;
map<int,int>mmp;
struct Edge
{
int to,next,cap,flow;
} edge[maxm];
int tot;
int head[maxn];
int gap[maxn],dep[maxn],cur[maxn];
void init()
{
tot=0;
memset(head,-1,sizeof(head));
}
void debug()
{
cout<<"_____five_____\n";
}
void addedge(int u,int v,int w,int rw=0)
{
edge[tot].to=v;
edge[tot].cap=w;
edge[tot].flow=0;
edge[tot].next=head[u];
head[u]=tot++;
edge[tot].to=u;
edge[tot].cap=rw;
edge[tot].flow=0;
edge[tot].next=head[v];
head[v]=tot++;
}
int Q[maxn];
void BFS(int s,int e)
{
memset(dep,-1,sizeof(dep));
memset(gap,0,sizeof(gap));
gap[0]=1;
int front=0,rear=0;
dep[e]=0;
Q[rear++]=e;
while(front!=rear)
{
int u=Q[front++];
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].to;
if(dep[v]!=-1)
continue;
Q[rear++]=v;
dep[v]=dep[u]+1;
gap[dep[v]]++;
}
}
}
int S[maxn];
int sap(int s,int e,int n)
{
BFS(s,e);
memcpy(cur,head,sizeof(head));
int top=0;
int u=s;
int ans=0;
while(dep[s]<n)
{
if(u==e)
{
int Min=inf;
int inser;
for(int i=0; i<top; i++)
if(Min>edge[S[i]].cap-edge[S[i]].flow)
{
Min=edge[S[i]].cap-edge[S[i]].flow;
inser=i;
}
for(int i=0; i<top; i++)
{
edge[S[i]].flow+=Min;
edge[S[i]^1].flow-=Min;
}
ans+=Min;
top=inser;
u=edge[S[top]^1].to;
continue;
}
bool flag=false;
int v;
for(int i=cur[u]; i!=-1; i=edge[i].next)
{
v=edge[i].to;
if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u])
{
flag=true;
cur[u]=i;
break;
}
}
if(flag)
{
S[top++]=cur[u];
u=v;
continue;
}
int Min=n;
for(int i=head[u]; i!=-1; i=edge[i].next)
if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<Min)
{
Min=dep[edge[i].to];
cur[u]=i;
}
gap[dep[u]]--;
if(!gap[dep[u]])
return ans;
dep[u]=Min+1;
gap[dep[u]]++;
if(u!=s)
u=edge[S[--top]^1].to;
}
return ans;
}
int main()
{
while(~scanf("%d%d",&N,&M)){
init();
scanf("%d%d",&s,&D);
//其中S和D分别为源点和汇点
for(int i=1;i<=N;++i){
int x;
scanf("%d",&x);
addedge(2*i-1,2*i,x);
addedge(2*i,2*i-1,x);
}
for(int i=1;i<=M;++i){
int u,v;
scanf("%d%d",&u,&v);
//从u到v
addedge(2*u,2*v-1,inf);
addedge(2*v,2*u-1,inf);
}
int super_s=2*s-1,super_d=2*D;
printf("%d\n",sap(super_s,super_d,N*2));
}
return 0;
}