Control
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3916 Accepted Submission(s): 1636
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
Sample Output
3
题意:给出n个点,m条双向边,源点和汇点,以及切断每个点的权,求最小割,注意,边为双向
关于最小割有个定理:最大流即最小割,那么直接求最大流就行了
对于本题,由于要切断点,所以就要割点,把点分为左和右,这样切断这点就相当于,切掉这条边,这里还有一点需要注意的:双向边,由于边u-->v为双向边,设割点后,u点的左边为u,右边为u',v点的左边为v,右边为v',那么u-->v割点后的双向建边就应该为,u'-->v,v'-->u
#include <cstdio>
#include <cstring>
#include <cctype>
#include <stdlib.h>
#include <string>
#include <map>
#include <iostream>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
const int N=5000+20;
const int M=50000+20;
int top;//当前边下标
int dis[N],pre[N];//源点到点i的最小距离,pre[i]记录前驱
bool vis[N];//标记数组
int maxflow;
struct vertex
{
int first;
} V[N];
struct Edge
{
int v,next;
int cap,flow,cost;
} E[M*2];
void init()
{
mem(V,-1);
top=0;
maxflow=0;
}
void add_edge(int u,int v,int c,int cost)
{
E[top].v=v;
E[top].cap=c;
E[top].flow=0;
E[top].cost=cost;
E[top].next=V[u].first;
V[u].first=top++;
}
void add(int u,int v,int c,int cost)
{
add_edge(u,v,c,cost);
add_edge(v,u,0,-cost);
}
bool spfa(int s,int t)
{
int i,u,v;
queue<int>q;
mem(vis,false);
mem(pre,-1);
mem(dis,inf);
vis[s]=true;
dis[s]=0;
q.push(s);
while(!q.empty())
{
u=q.front();
q.pop();
vis[u]=false;
for(int i=V[u].first; i!=-1; i=E[i].next)
{
v=E[i].v;
if(E[i].cap>E[i].flow&&dis[v]>dis[u]+E[i].cost)
{
dis[v]=dis[u]+E[i].cost;
pre[v]=i;
if(!vis[v])
{
q.push(v);
vis[v]=true;
}
}
}
}
if(dis[t]==inf)
return false;
return true;
}
int MCMF(int s,int t)//minCostMaxFlow
{
int d;
int i,mincost=0;//maxflow当前最大流量,mincost当前最小费用
while(spfa(s,t))//表示找到了从s到t的最小费用路
{
d=inf;
for(int i=pre[t]; i!=-1; i=pre[E[i^1].v]) //遍历反向边
d=min(d,E[i].cap-E[i].flow);
maxflow+=d;//更新最大流
//printf("%d\n",d);
for(int i=pre[t]; i!=-1; i=pre[E[i^1].v]) //增广路上正向边流量+d,反向边流量-d
{
E[i].flow+=d;
E[i^1].flow-=d;
}
mincost+=dis[t]*d;//dis[t]为该路径上单位流量费用之和
}
return mincost;
}
int e1[55][55],e2[55][55],w[55][55],check[55];
int main()
{
int n,m,k,i,j,o;
while(scanf("%d%d%d",&n,&m,&k)&&n+m+k)
{
int ans=0;
mem(check,0);
for(i=1; i<=n; i++)
for(j=1; j<=k; j++)
{
scanf("%d",&e1[i][j]);
check[j]+=e1[i][j];
}
for(i=1; i<=m; i++)
for(j=1; j<=k; j++)
{
scanf("%d",&e2[i][j]);
}
bool flag=0;
for(o=1; o<=k; o++)
{
//源-》供货
init();
for(i=1; i<=n; i++)
{
//供货-》店
for(j=1; j<=m; j++)
{
scanf("%d",&w[j][i]);
add(j,m+i,e2[j][o],w[j][i]);
}
}
if(flag==1)continue;
for(i=1;i<=m;i++)
add(0,i,e2[i][o],0);
for(i=1;i<=n;i++)
add(m+i,n+m+1,e1[i][o],0);
int now=MCMF(0,m+n+1);
if(maxflow!=check[o])
{
flag=1;
continue;
}
else
ans+=now;
}
if(flag)
puts("-1");
else
printf("%d\n",ans);
}
return 0;
}