# HDU 4289 Control （网络流，最大流）

### 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（Weapon of Mass Destruction）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.

### 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.

5 6
5 3
5
2
3
4
12
1 5
5 4
2 3
2 4
4 3
2 1

3

## 代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxN=600;
const int maxM=20001*20;
const int inf=2147483647;

class Edge
{
public:
int u,v,flow;
};

int n,m;
int S,T;
int cnt=-1;
int Next[maxM];
Edge E[maxM];
int depth[maxN];
int Q[maxM];
int cur[maxN];

bool bfs();
int dfs(int u,int flow);

int main()
{
while (cin>>n>>m)
{
cnt=-1;
scanf("%d%d",&S,&T);//源点和汇点
T=T+n;//这里要把T变成拆点后的后一个点
for (int i=1;i<=n;i++)
{
int cost;
scanf("%d",&cost);
}
for (int i=1;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);//读入边，连边，注意是无向的
}
int Ans=0;//求解最大流，最大流就是最小割
while (bfs())
{
for (int i=1;i<=2*n;i++)
while (int di=dfs(S,inf))
Ans+=di;
}
printf("%d\n",Ans);
}
return 0;
}

{
cnt++;
E[cnt].u=u;
E[cnt].v=v;
E[cnt].flow=flow;

cnt++;
E[cnt].u=v;
E[cnt].v=u;
E[cnt].flow=0;
return;
}

bool bfs()
{
memset(depth,-1,sizeof(depth));
int h=1,t=0;
Q=S;
depth[S]=1;
do
{
t++;
int u=Q[t];
{
int v=E[i].v;
if ((depth[v]==-1)&&(E[i].flow>0))
{
depth[v]=depth[u]+1;
h++;
Q[h]=v;
}
}
}
while (t!=h);
if (depth[T]==-1)
return 0;
return 1;
}

int dfs(int u,int flow)
{
if (u==T)
return flow;
for (int &i=cur[u];i!=-1;i=Next[i])
{
int v=E[i].v;
if ((depth[v]==depth[u]+1)&&(E[i].flow>0))
{
int di=dfs(v,min(flow,E[i].flow));
if (di>0)
{
E[i].flow-=di;
E[i^1].flow+=di;
return di;
}
}
}
return 0;
}

posted @ 2017-08-17 16:42 SYCstudio 阅读(...) 评论(...) 编辑 收藏