Control

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

Total Submission(s): 2139    Accepted Submission(s): 904

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
题意:给一个无向图,有些不法分子要从vs点vt点,如今要抓住全部的不法分子阻止他们去vt,那么就要控制某一些城市等待他们,控制每一个城市花费不同,问最少花费是多少。

解题:最小割,割断全部的通路,花费使得最少,这样就一定能抓住全部的不法分子。

拆点,每一个点拆成一条有向边v->v ’ 边权为控制这个城市的花费,原图中的边u->v,则建成:u+n->v。v+n->u,边权都为INF。再跑一下最大流,就是ans。

/*
最大流:SAP算法,与ISAP的区别就是不用预处理
*/
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
#define captype int const int MAXN = 100010; //点的总数
const int MAXM = 400010; //边的总数
const int INF = 1<<30;
struct EDG{
int to,next;
captype cap,flow;
} edg[MAXM];
int eid,head[MAXN];
int gap[MAXN]; //每种距离(或可觉得是高度)点的个数
int dis[MAXN]; //每一个点到终点eNode 的最短距离
int cur[MAXN]; //cur[u] 表示从u点出发可流经 cur[u] 号边
int pre[MAXN]; void init(){
eid=0;
memset(head,-1,sizeof(head));
}
//有向边 三个參数。无向边4个參数
void addEdg(int u,int v,captype c,captype rc=0){
edg[eid].to=v; edg[eid].next=head[u];
edg[eid].cap=c; edg[eid].flow=0; head[u]=eid++; edg[eid].to=u; edg[eid].next=head[v];
edg[eid].cap=rc; edg[eid].flow=0; head[v]=eid++;
}
captype maxFlow_sap(int sNode,int eNode, int n){//n是包含源点和汇点的总点个数。这个一定要注意
memset(gap,0,sizeof(gap));
memset(dis,0,sizeof(dis));
memcpy(cur,head,sizeof(head));
pre[sNode] = -1;
gap[0]=n;
captype ans=0; //最大流
int u=sNode;
while(dis[sNode]<n){ //推断从sNode点有没有流向下一个相邻的点
if(u==eNode){ //找到一条可增流的路
captype Min=INF ;
int inser;
for(int i=pre[u]; i!=-1; i=pre[edg[i^1].to]) //从这条可增流的路找到最多可增的流量Min
if(Min>edg[i].cap-edg[i].flow){
Min=edg[i].cap-edg[i].flow;
inser=i;
}
for(int i=pre[u]; i!=-1; i=pre[edg[i^1].to]){
edg[i].flow+=Min;
edg[i^1].flow-=Min; //可回流的边的流量
}
ans+=Min;
u=edg[inser^1].to;
continue;
}
bool flag = false; //推断是否能从u点出发可往相邻点流
int v;
for(int i=cur[u]; i!=-1; i=edg[i].next){
v=edg[i].to;
if(edg[i].cap-edg[i].flow>0 && dis[u]==dis[v]+1){
flag=true;
cur[u]=pre[v]=i;
break;
}
}
if(flag){
u=v;
continue;
}
//假设上面没有找到一个可流的相邻点,则改变出发点u的距离(也可觉得是高度)为相邻可流点的最小距离+1
int Mind= n;
for(int i=head[u]; i!=-1; i=edg[i].next)
if(edg[i].cap-edg[i].flow>0 && Mind>dis[edg[i].to]){
Mind=dis[edg[i].to];
cur[u]=i;
}
gap[dis[u]]--;
if(gap[dis[u]]==0) return ans; //当dis[u]这样的距离的点没有了,也就不可能从源点出发找到一条增广流路径
//由于汇点到当前点的距离仅仅有一种。那么从源点到汇点必定经过当前点,然而当前点又没能找到可流向的点,那么必定断流
dis[u]=Mind+1;//假设找到一个可流的相邻点,则距离为相邻点距离+1。假设找不到。则为n+1
gap[dis[u]]++;
if(u!=sNode) u=edg[pre[u]^1].to; //退一条边
}
return ans;
}
int main()
{
int n,m,vs,vt,u,v,cost,ans;
while(scanf("%d%d",&n,&m)>0)
{
scanf("%d%d",&vs,&vt);
vt+=n;
init();
for(int i=1; i<=n; i++){
scanf("%d",&cost);
addEdg(i , i+n , cost);
}
while(m--){
scanf("%d%d",&u,&v);
addEdg(u+n , v , INF);
addEdg(v+n , u , INF);
}
ans=maxFlow_sap(vs , vt , n*2);
printf("%d\n",ans);
}
}

HDU 4289 Control (最小割 拆点)的更多相关文章

  1. HDU 4289 Control 最小割

    Control 题意:有一个犯罪集团要贩卖大规模杀伤武器,从s城运输到t城,现在你是一个特殊部门的长官,可以在城市中布置眼线,但是布施眼线需要花钱,现在问至少要花费多少能使得你及时阻止他们的运输. 题 ...

  2. hdu-4289.control(最小割 + 拆点)

    Control Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  3. HDU 4289 Control(最大流+拆点,最小割点)

    题意: 有一群恐怖分子要从起点st到en城市集合,你要在路程中的城市阻止他们,使得他们全部都被抓到(当然st城市,en城市也可以抓捕).在每一个城市抓捕都有一个花费,你要找到花费最少是多少. 题解: ...

  4. HDU 4289 Control (网络流,最大流)

    HDU 4289 Control (网络流,最大流) Description You, the head of Department of Security, recently received a ...

  5. hdu 4289 Control(最小割 + 拆点)

    http://acm.hdu.edu.cn/showproblem.php?pid=4289 Control Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  6. HDU4289 Control —— 最小割、最大流 、拆点

    题目链接:https://vjudge.net/problem/HDU-4289 Control Time Limit: 2000/1000 MS (Java/Others)    Memory Li ...

  7. hdu4289 Control --- 最小割,拆点

    给一个无向图.告知敌人的起点和终点.你要在图上某些点安排士兵.使得敌人不管从哪条路走都必须经过士兵. 每一个点安排士兵的花费不同,求最小花费. 分析: 题意可抽象为,求一些点,使得去掉这些点之后,图分 ...

  8. HDU(2485),最小割最大流

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2485 Destroying the bus stations Time Limit: 40 ...

  9. HDU 4971 (最小割)

    Problem A simple brute force problem (HDU 4971) 题目大意 有n个项目和m个问题,完成每个项目有对应收入,解决每个问题需要对应花费,给出每个项目需解决的问 ...

随机推荐

  1. [Leetcode Week8]Subsets

    Subsets 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/subsets/description/ Description Given a set ...

  2. Linux下的Backlight子系统(一)【转】

    转自:http://blog.csdn.net/weiqing1981127/article/details/8511676 版权所有,转载必须说明转自 http://my.csdn.net/weiq ...

  3. malloc和new的区别 begin

    http://blog.csdn.net/miss_acha/article/details/7279915#comments 1.综述 1,malloc与free是C++/C语言的标准库函数,new ...

  4. vue指令v-bind

    v-bind用于绑定 html 属性,通常会将v-bind缩写(如"v-bind:class"可缩写成":class"): v-bind除了可以绑定字符串类型变 ...

  5. 【 Tomcat 】后端tomcat获取真实IP

    环境: nginx + tomcat nginx.conf 配置: proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_ad ...

  6. k8s通过configmap管理应用配置信息

    Secret 可以为 Pod 提供密码.Token.私钥等敏感数据:对于一些非敏感数据,比如应用的配置信息,则可以用 ConfigMap. ConfigMap 的创建和使用方式与 Secret 非常类 ...

  7. 关于 log4j.additivity的说明

    log4j.additivity是 子Logger 是否继承 父Logger 的 输出源(appender) 的标志位.具体说,默认情况下 子Logger 会继承 父Logger 的appender, ...

  8. Python:文件操作技巧(File operation)(转)

    Python:文件操作技巧(File operation) 读写文件 # ! /usr/bin/python #  -*- coding: utf8 -*- spath = " D:/dow ...

  9. HDU 1159.Common Subsequence-最长公共子序列(LCS)

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  10. HDU 2199 Can you solve this equation? 【浮点数二分求方程解】

    Now,given the equation 8x^4 + 7x^3 + 2x^2 + 3x + 6 == Y,can you find its solution between 0 and 100; ...