您的位置:首页 > 其它

HDU-4289-Control(最大流+拆点)

2016-04-12 16:55 423 查看

Control

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2603 Accepted Submission(s): 1120



[align=left]Problem Description[/align]
  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

[align=left]Input[/align]
  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).

[align=left]Output[/align]
  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.

[align=left]Sample Input[/align]

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


[align=left]Sample Output[/align]

3


[align=left]Source[/align]
2012 ACM/ICPC Asia Regional Chengdu Online

题意:一个无向图,点带权值,有源点S,汇点T,问最少拆出多少权值的点使ST不连通(include S or T);

可以转化为最大流问题,将每个点P拆分成 P与P',使P->P',边权值赋为点权值,建边(P,Q)时只需连接(P',Q),(Q',P)边权值为INF即可。

CODE:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#define INF 0x3f3f3f3f
using namespace std;
#define MAXN 100005
#define MAXM 2005
struct node
{
int v,next,flow;
} edge[MAXN];
int index,head[MAXN];
void add_edge(int u, int v, int flow)
{
edge[index].v=v;
edge[index].flow=flow;
edge[index].next=head[u];
head[u]=index++;

edge[index].v=u;
edge[index].flow=0;
edge[index].next=head[v];
head[v]=index++;

}
int deep[MAXN];
bool search_(int s,int t)
{
memset(deep,0,sizeof(deep));
queue<int> q;
q.push(s);
deep[s]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].v;
if(!deep[v]&&edge[i].flow)
{
deep[v]=deep[u]+1;
q.push(v);
if(v==t)return 1;
}
}
}
return 0;
}
int DFS(int s,int t,int min_)
{
if(s==t)return min_;
int i,ans=0,f;
for(i=head[s]; i!=-1 &&ans<min_; i=edge[i].next)
{
int v=edge[i].v;
if(deep[v]==deep[s]+1 && edge[i].flow)
{
f=DFS(v,t,min(min_-ans,edge[i].flow));
edge[i].flow-=f;
edge[i^1].flow+=f;
ans+=f;
if(ans==min_)return ans;
}
}
return ans;
}
int dinic(int s,int t)
{
int ans=0,p;
while(search_(s,t))
{
while(p=DFS(s,t,INF))
ans+=p;
}
return ans;
}
/*
int pre[MAXN];
int level[MAXN];
int gap[MAXN];
int SAP(int s,int t)
{
memset(pre,-1,sizeof(pre));
memset(level,0,sizeof(level));
memset(gap,0,sizeof(gap));
gap[0]=t+1;
int v,u=s,maxflow=0,Min=INF,i;
while(level[s]<t)
{
for(i=head[u]; i!=-1; i=edge[i].next)
{
v=edge[i].v;
if(edge[i].flow &&level[u]==level[v]+1)
break;
}
if(i!=-1)
{
pre[v]=i;
u=v;
if(v==t)
{
Min=INF;
for(i=pre[v]; i!=-1; i=pre[ edge[i^1].v ])
{
if(Min>edge[i].flow)Min=edge[i].flow;
}
maxflow+=Min;
for(i=pre[v]; i!=-1; i=pre[ edge[i^1].v ])
{
edge[i].flow-=Min;
edge[i^1].flow+=Min;
}
u=s;
}
}
else
{
int minlevel=t;
for(i=head[u]; i!=-1; i=edge[i].next)
{
v=edge[i].v;
if(edge[i].flow && minlevel>level[v])
minlevel=level[v];
}
gap[level[u]]--;
if(gap[ level[u] ]==0)break;
level[u]=minlevel+1;
gap[ level[u] ]++;
u=edge[ pre[u]^1 ].v;
}
}
return maxflow;
}*/
int food[MAXM],drink[MAXM];
char str[MAXM];
int main()
{
int n,m,a,b;
int s,t;
while(scanf("%d%d",&n,&m)!=EOF)
{
index=0;
memset(head,-1,sizeof(head));
scanf("%d%d",&s,&t);
for(int i=1; i<=n; ++i)
{
scanf("%d",&a);
add_edge(i,i+n,a);
}
for(int i=0; i<m; ++i)
{
scanf("%d%d",&a,&b);
add_edge(a+n,b,INF);
add_edge(b+n,a,INF);
}
int ans=dinic(s,t+n);
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: