HDU 4289:Control(最小割)
http://acm.hdu.edu.cn/showproblem.php?pid=4289
题意:有n个城市,m条无向边,小偷要从s点开始逃到d点,在每个城市安放监控的花费是sa[i],问最小花费可以监控到所有小偷。
思路:求最小割可以转化为最大流。每个城市之间拆点,流量是sa[i],再增加一个超级源点S和s相连,增加一个超级汇点T,让d的第二个点和T相连。然后就可以做了。
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#include <set>
using namespace std;
#define INF 0x3f3f3f3f
#define N 100010
#define M 510
typedef long long LL;
struct Edge {
int v, nxt, cap;
Edge () {}
Edge (int v, int cap, int nxt) : v(v), cap(cap), nxt(nxt) {}
}edge[N*];
int head[M], tot, gap[M], dis[M], cur[M], pre[M];
int sa[M], S, T; void Add(int u, int v, int cap) {
edge[tot] = Edge(v, cap, head[u]); head[u] = tot++;
edge[tot] = Edge(u, , head[v]); head[v] = tot++;
} void BFS() {
memset(dis, -, sizeof(dis));
memset(gap, , sizeof(gap));
queue<int> que; que.push(T);
dis[T] = ; gap[]++;
while(!que.empty()) {
int u = que.front(); que.pop();
for(int i = head[u]; ~i; i = edge[i].nxt) {
Edge& e = edge[i];
if(~dis[e.v]) continue;
dis[e.v] = dis[u] + ;
gap[dis[e.v]]++;
que.push(e.v);
}
}
} int ISAP(int n) {
BFS(); // 别忘了调用!
memcpy(cur, head, sizeof(cur));
int u = pre[S] = S, ans = , i;
while(dis[S] < n) {
if(u == T) {
int flow = INF, index;
for(i = S; i != T; i = edge[cur[i]].v)
if(flow > edge[cur[i]].cap)
flow = edge[cur[i]].cap, index = i;
for(i = S; i != T; i = edge[cur[i]].v)
edge[cur[i]].cap -= flow, edge[cur[i]^].cap += flow;
u = index; ans += flow;
}
for(i = cur[u]; ~i; i = edge[i].nxt)
if(dis[edge[i].v] == dis[u] - && edge[i].cap > ) break;
if(~i) {
pre[edge[i].v] = u; cur[u] = i;
u = edge[i].v;
} else {
if(--gap[dis[u]] == ) break;
int md = n;
for(i = head[u]; ~i; i = edge[i].nxt)
if(edge[i].cap > && dis[edge[i].v] < md)
md = dis[edge[i].v], cur[u] = i;
++gap[dis[u] = md + ];
u = pre[u];
}
}
return ans;
} int main() {
int n, m;
while(~scanf("%d%d", &n, &m)) {
int s, t;
scanf("%d%d", &s, &t);
memset(head, -, sizeof(head)); tot = ;
S = , T = * n + ;
// 第一个点是1~n,第二个点是n+1~2*n,第一个点为进来的点,第二个点为出去的点
Add(S, s, INF); Add(t + n, T, INF);
for(int i = ; i <= n; i++) scanf("%d", &sa[i]);
for(int i = ; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
Add(u + n, v, INF);
Add(v + n, u, INF);
}
for(int i = ; i <= n; i++)
Add(i, i + n, sa[i]);
int ans = ISAP(T + );
printf("%d\n", ans);
}
return ;
}
HDU 4289:Control(最小割)的更多相关文章
- HDU 4289 Control 最小割
Control 题意:有一个犯罪集团要贩卖大规模杀伤武器,从s城运输到t城,现在你是一个特殊部门的长官,可以在城市中布置眼线,但是布施眼线需要花钱,现在问至少要花费多少能使得你及时阻止他们的运输. 题 ...
- HDU 4289 Control (网络流,最大流)
HDU 4289 Control (网络流,最大流) Description You, the head of Department of Security, recently received a ...
- hdu 4289 Control(最小割 + 拆点)
http://acm.hdu.edu.cn/showproblem.php?pid=4289 Control Time Limit: 2000/1000 MS (Java/Others) Mem ...
- HDU 4289 Control (最小割 拆点)
Control Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- HDU(2485),最小割最大流
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2485 Destroying the bus stations Time Limit: 40 ...
- HDU 4971 (最小割)
Problem A simple brute force problem (HDU 4971) 题目大意 有n个项目和m个问题,完成每个项目有对应收入,解决每个问题需要对应花费,给出每个项目需解决的问 ...
- hdu 4859 海岸线 最小割
海岸线 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4859 Description 欢迎来到珠海! 由于土地资源越来越紧张,使得许多海滨城市都只能 ...
- 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 ...
- HDU 4289 Control
最小割 一个点拆成两个 AddEdge(i,i+N,x); 原图中的每条边这样连 AddEdge(u+N,v,INF); AddEdge(v+N,u,INF); S是源点,t+N是汇点.最大流就是答案 ...
随机推荐
- 多个Excel文件快速导入到DB里面
1 . 文件比较多,需要把这么多的数据都导入到DB里面,一个个导入太慢了,能想到的是先把数据整个到一个Excel中,然后再导入 2. 第一步准备合并Excel,新建一个新的excel,命名为total ...
- test hypertext links for validity, accessibility, and recent modification
https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html The HEAD method is identical to GET except th ...
- Execel(导出新方法):
#region 新方法 //var sbHtml = new StringBuilder(); //sbHtml.Append("<table border='1' cellspaci ...
- Notification中使用Glide
之前一直在琢磨Glide自定义使用中的一些经验:今天简单的分享下Notification中使用Glide去加载icon方法: 我们都知道使用android通知时,一般可能会有如下代码: Notific ...
- JS操作cookie
JavaScript中的另一个机制:cookie,则可以达到真正全局变量的要求.cookie是浏览器 提供的一种机制,它将document 对象的cookie属性提供给JavaScript.可以由Ja ...
- spring 另开线程时的注入问题
spring web项目在启动的时候,就会完成各种组件的注入.在工作的过程中,遇到了这样一个问题: 一个serviceA中要新开一个线程来执行一项任务(假定这个任务是ClassA).ClassA中要用 ...
- 各种同步方法性能比较(synchronized,ReentrantLock,Atomic)
synchronized: 在资源竞争不是很激烈的情况下,偶尔会有同步的情形下,synchronized是很合适的.原因在于,编译程序通常会尽可能的进行优化synchronize,另外可读性非常好,不 ...
- Ubuntu 16.04 LTS发布
[Ubuntu 16.04 LTS发布]Ubuntu 16.04 LTS 发布日期已正式确定为 2016 年 4 月 21 日,代号为 Xenial Xerus.Ubuntu16.04 将是非常受欢迎 ...
- installing mysql,this may take a few minutes,hold on plz wdcp卡住解决办法
centos6安装wdcp时make in progress卡住的解决办法 今天在一台centos6的vps上安装wdcp出现的这个问题,到安装程序滚动至下面这里时出现"卡死". ...
- spark读取hdfs上的文件和写入数据到hdfs上面
def main(args: Array[String]): Unit = { val conf = new SparkConf() conf.set("spark.master" ...