Control
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2933 Accepted Submission(s): 1259
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
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).
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.
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
5 3
5
2
3
4
12
1 5
5 4
2 3
2 4
4 3
2 1
Sample Output
3
Source
题目大意:给你n个点,m条无向边,给你起点和终点,以及拆除每个城市所需要的花费,点权,然后m条无向边给出,问你如何拆除城市,使得从起点无法走到终点,问这个最小花费。
思路:
1、首先,虽然最大流是在网络流相关问题中的一种概念,而且网络是一种特殊的有向图,那么无向图为何可以使用这些个解决有向图问题的算法来实现呢?无向图是特殊的有向图,将无向图我们可以看成有向图,无非就是将两个点之间的一条边,变成有方向的两条边罢了,其无论权值为多少,是不会影响最大流算法的实现的。
2、那么我们将一个城市节点一分为二,并且在其建立双向边,权值为其点权,其余m条无向边,我们将其两个节点连接,并设其权值为INF.那么根据最大流最小割定理可知,我们现在拆的点,变成了拆边,而且我们想从起点到不了终点,那么问题也就变成了最小割。最大流==最小割,所以我们跑一遍Dinic/Sap就能求出当前图的最大流,也就是我们的最小割。
3、那么我们现在的起点不变,汇点就变成了t+n(我们设定节点1的拆分点为1和1+n)
Ac代码:
#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
using namespace std;
#define INF 0x3f3f3f3f
struct node
{
int from;
int to;
int w;
int next;
}e[1151515];
int cur[10000];
int divv[10000];
int head[1000];
int ss,tt;
int n,m,cont;
void add(int from,int to,int w)
{
e[cont].to=to;
e[cont].w=w;
e[cont].next=head[from];
head[from]=cont++;
}
int makedivv()
{
queue<int >s;
s.push(ss);
memset(divv,0,sizeof(divv));
divv[ss]=1;
while(!s.empty())
{
int u=s.front();
if(u==tt)return 1;
s.pop();
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].to;
int w=e[i].w;
if(w&&divv[v]==0)
{
divv[v]=divv[u]+1;
s.push(v);
}
}
}
return 0;
}
int Dfs(int u,int maxflow,int tt)
{
if(u==tt)return maxflow;
int ret=0;
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].to;
int w=e[i].w;
if(divv[v]==divv[u]+1&&w)
{
int f=Dfs(v,min(maxflow-ret,w),tt);
e[i].w-=f;
e[i^1].w+=f;
ret+=f;
if(ret==maxflow)return ret;
}
}
return ret;
}
void Dinic()
{
int ans=0;
while(makedivv()==1)
{
memcpy(cur,head,sizeof(head));
ans+=Dfs(ss,INF,tt);
}
printf("%d\n",ans);
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
cont=0;
memset(head,-1,sizeof(head));
scanf("%d%d",&ss,&tt);
tt+=n;
for(int i=1;i<=n;i++)
{
int tmp;
scanf("%d",&tmp);
add(i,i+n,tmp);
add(i+n,i,0);
add(i+n,i,tmp);
add(i,i+n,0);
}
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
add(x+n,y,INF);
add(y,x+n,0);
add(y+n,x,INF);
add(x,y+n,0);
}
Dinic();
}
}