HDU4289:Control(最小割)
Control
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5023 Accepted Submission(s): 2067
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4289
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
题意:
给出起点和终点城市,每个城市都有一定的权值,有一群坏人要从起点到终点,现在要在一些城市上布置警察拦截这些坏人,问最少花费。
题解:
由于点有权值,我们考虑拆点;同时这题是双向边。
如果我们像以往那样反向边容量也为c,则有误。
我们这样考虑插边:插入的反向边容量还是0,只是这样来构造双向边:u'->v , v'->u。其中u'为出度点,u为入度点。
最后跑个最大流就行了。最大流等于最小割
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#define INF 99999999
using namespace std;
typedef long long ll;
const int N = ,M = 1e5;
int head[N],d[N];
int tot,n,m,s,t;
struct Edge{
int v,next,c;
}e[M];
void adde(int u,int v,int c){
e[tot].v=v;e[tot].next=head[u];e[tot].c=c;head[u]=tot++;
e[tot].v=u;e[tot].next=head[v];e[tot].c=;head[v]=tot++;
}
bool bfs(int S,int T){
memset(d,,sizeof(d));d[S]=;
queue <int > q;q.push(S);
while(!q.empty()){
int u=q.front();q.pop();
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(!d[v] && e[i].c>){
d[v]=d[u]+;
q.push(v);
}
}
}
return d[t]!=;
}
int dfs(int u,int a){
int flow=,f;
if(u==t || a==) return a;
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(d[v]!=d[u]+) continue ;
f=dfs(v,min(a,e[i].c));
if(f>){
e[i].c-=f;
e[i^].c+=f;
flow+=f;
a-=f;
if(a==) break;
}
}
if(!flow) d[u]=-;
return flow;
}
int Dinic(){
int max_flow=;
while(bfs(s,t))
max_flow+=dfs(s,INF);
return max_flow;
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
memset(head,-,sizeof(head));tot=;
scanf("%d%d",&s,&t);
t+=;
for(int i=,c;i<=n;i++){
scanf("%d",&c);
adde(i,i+,c);
}
for(int i=,u,v;i<=m;i++){
scanf("%d%d",&u,&v);
adde(u+,v,INF);
adde(v+,u,INF);
}
printf("%d\n",Dinic());
}
return ;
}
HDU4289:Control(最小割)的更多相关文章
- HDU4289 Control —— 最小割、最大流 、拆点
题目链接:https://vjudge.net/problem/HDU-4289 Control Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- hdu-4289.control(最小割 + 拆点)
Control Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- hdu4289 Control --- 最小割,拆点
给一个无向图.告知敌人的起点和终点.你要在图上某些点安排士兵.使得敌人不管从哪条路走都必须经过士兵. 每一个点安排士兵的花费不同,求最小花费. 分析: 题意可抽象为,求一些点,使得去掉这些点之后,图分 ...
- HDU 4289 Control 最小割
Control 题意:有一个犯罪集团要贩卖大规模杀伤武器,从s城运输到t城,现在你是一个特殊部门的长官,可以在城市中布置眼线,但是布施眼线需要花钱,现在问至少要花费多少能使得你及时阻止他们的运输. 题 ...
- hdu4289 Control 最大流最小割
You, the head of Department of Security, recently received a top-secret information that a group of ...
- [USACO Section 4.4]追查坏牛奶Pollutant Control (最小割)
题目链接 Solution 一眼看过去就是最小割,但是要求割边最少的最小的割. 所以要用骚操作... 建边的时候每条边权 \(w = w * (E+1) + 1;\) 那么这样建图跑出来的 \(max ...
- 洛谷 P1344 追查坏牛奶Pollutant Control —— 最小割
题目:https://www.luogu.org/problemnew/show/P1344 就是求最小割: 但是还要边数最小,所以把边权都*1001+1,这样原来流量部分是*1001,最大流一样的不 ...
- LG1344 「USACO4.4」Pollutant Control 最小割
问题描述 LG1344 题解 我太菜了,我一开始竟然没有看出这是个最小割裸题... 两个询问. 第一个询问,直接跑最小割就好了. 第二个询问,建图的时候边权建 \(1\) ,代表割掉这条边需要 \(1 ...
- HDU4289(KB11-I 最小割)
Control Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
随机推荐
- C# 测量程序运行时间
using System.Diagnostics; Stopwatch watch = new Stopwatch(); watch.Start(); /* 需要测量运行时间的程序 */ watch. ...
- Jupyter Notebook里面使用Matplotlib画图 图表中文乱码问题
可查看以下链接: https://blog.csdn.net/ccblogger/article/details/79613335
- mongodb的学习之旅一
描述 作为一枚菜鸟级别的coder,刚接触nodejs没有多久.现在在学习微信公众号的开发,但是碰到了mongodb保存用户数据的时候,出现了DeprecationWarning: Mongoose: ...
- 三角形xjoi 8.14
问题描述:离圣诞节只有一个月了,家里要你准备一个很大的星星,然后把它粘在圣诞树的顶端.你已经准备好了一个三角形的银色包装纸来做星星,可忽然有一天你发现在这张大纸上被弄了好多的小洞,原来是你的弟弟妹妹已 ...
- EAS集锦
前言 之前看过的相关BOS开发文档,整理了一些常用的API,一直没有来得及放上来,现在把整理的文件放上来,以备忘查看,分享.闲话少说,上干货! ps 图片不方便查看的话,可以拖住图片,加载到浏览器新页 ...
- 【APUE】Chapter10 Signals
Signal主要分两大部分: A. 什么是Signal,有哪些Signal,都是干什么使的. B. 列举了非常多不正确(不可靠)的处理Signal的方式,以及怎么样设计来避免这些错误出现. 10.2 ...
- 使用IDEA快速搭建Gradle项目
- Navicat oracle to postgresql ERR
可能的解决思路是设好源数据和目标数据库后,先建立表结构,然后修改表结构字段数据类型 参考 https://www.cnblogs.com/stephen-liu74/archive/2012/04/3 ...
- ADB常用指令
adb 命令是adb程序自带的一些命令:adb shell则是调用Android系统的命令,Android系统特有的命令都放在Android设备的/system/bin目录中 MonkeyRunner ...
- 拉普拉斯矩阵(Laplacian Matrix) 及半正定性证明
摘自 https://blog.csdn.net/beiyangdashu/article/details/49300479 和 https://en.wikipedia.org/wiki/Lapla ...