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(最小割)的更多相关文章

  1. HDU 4289 Control 最小割

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

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

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

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

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

  4. HDU 4289 Control (最小割 拆点)

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

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

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

  6. HDU 4971 (最小割)

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

  7. hdu 4859 海岸线 最小割

    海岸线 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4859 Description 欢迎来到珠海! 由于土地资源越来越紧张,使得许多海滨城市都只能 ...

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

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

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

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

  10. HDU 4289 Control

    最小割 一个点拆成两个 AddEdge(i,i+N,x); 原图中的每条边这样连 AddEdge(u+N,v,INF); AddEdge(v+N,u,INF); S是源点,t+N是汇点.最大流就是答案 ...

随机推荐

  1. JMeter之JMS接口测试

    JMeter是Apache开发的一款小巧易用的开源性能测试工具,由java语言开发.JMeter不仅免费开源而且功能强大.易于扩展,如果有一定Java开发基础的话还可以在JMeter上做扩展开发新的插 ...

  2. 带你玩转JavaWeb开发之六-mysql基本语法详解及实例(1)

    1.1.1    对数据库的表进行操作 1.1.1.1   对数据库中表进行创建 [语法:] create table 表名( 列名 列类型 [列约束], 列名 列类型 [列约束], 列名 列类型 [ ...

  3. snmp ubuntu/centos--

    软件安装 切换到系统管理员帐户 安装snmp确认snmp代理已安装rpm -q net-snmp如果未安装,安装snmpyum install net-snmp 设置开机自动运行snmp/sbin/c ...

  4. wordpres 自定义comment样式

    http://wange.im/diy-wordpress-comment-style.html function mytheme_comment($comment, $args, $depth) { ...

  5. css3知识

    一.box-sizing 属性 规定两个并排的带边框的框 二.align-items (适用于父类容器上) 设置或检索弹性盒子元素在侧轴(纵轴)方向上的对齐方式. 值: flex-start:弹性盒子 ...

  6. iOS推送遇到的问题

    1. 推送证书过期. 同事说iOS客户端不能推送消息,发现推送证书过期了,苹果的推送证书有效期是一年,推送证书过期后就不能使用推送服务了.解决办法:重新请求推送证书,导出p12文件,传给后台服务器就可 ...

  7. LeetCode Paint House

    原题链接在这里:https://leetcode.com/problems/paint-house/ 题目: There are a row of n houses, each house can b ...

  8. Swift: 在Swift中桥接OC文件(自己创建的类文件、第三方库文件)

    一.介绍 随着Swift的逐渐成熟,使用swift开发或者混合开发已经成为了一个趋势,本身苹果公司也十分推荐使用Swift这门新语言.目前Swift已经更新到了3.0,估计没有多久4.0就要出来了.那 ...

  9. oracle指令

    删除用户和用户下所有的表: drop user user_name cascade; 导入数据库: cd /home/oracle/app/admin/orcl/dpdump impdp   dire ...

  10. Windows Azure - Troubleshooting & Debugging: Role Recycling

    每年总会碰到几次Role Recycling,处理完记录下 :) 1. 和往常一样先排查系统日志,修复异常.> 没效果 :( 2. 排查Event Viewer中的Logs,没有发现比较奇怪Lo ...