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. Windows下用VMware安装Ubuntu虚拟机

    安装: http://jingyan.baidu.com/article/0320e2c1ef9f6c1b87507bf6.html 全屏: http://jingyan.baidu.com/arti ...

  2. web系统访问频率限制

    无论是spring mvc还是struts,都可以为controller或者aciton执行前,增加拦截器. 通过拦截器中的逻辑控制,可以实现访问频率的限制. 首先构造访问频率数据类 class Fr ...

  3. android必须要进行为不同分辨率设备切图

    以分辨率为1920×1080的android设备为例.在项目中加载资源的位置为xxhdpi文件夹: 例如将图片放入mdpi文件夹中就会出现,图片的横纵尺寸分别乘3被的后果,因为它认为在这个文件夹中是低 ...

  4. 【译】SQL Server索引进阶第八篇:唯一索引

    原文:[译]SQL Server索引进阶第八篇:唯一索引     索引设计是数据库设计中比较重要的一个环节,对数据库的性能其中至关重要的作用,但是索引的设计却又不是那么容易的事情,性能也不是那么轻易就 ...

  5. 解决界面有搜索栏时,点击TableView的空白界面,键盘不消失的问题

    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(h ...

  6. JMeter学习-039-JMeter 3.0 生成 dashboard HTML 报告图表中文乱码

    近期,经常有人问 JMeter 3.0 使用时,生成的 HTML 报告图表中的中文乱码问题.在此,简略的说一下解决的方法. 编码相关信息如下: 1.查看控制 csv.xml 等配置结果文件生成.读取的 ...

  7. SpringMVC常用配置-Controller返回格式化数据如JSON、XML的配置方式和机制

  8. Android中如何查看内存

    文章参照自:http://stackoverflow.com/questions/2298208/how-to-discover-memory-usage-of-my-application-in-a ...

  9. rplidar测试

    以下在虚拟机中完成的(ubuntu12.04-amd64-ros-exbot-h2-140520). 进入catkin_ws,下载代码: cd ~/catkin_ws/src/ git clone h ...

  10. 利用tween.js算法生成缓动效果

    在讲tween类之前,不得不提的是贝塞尔曲线了.首先,贝塞尔曲线是指依据四个位置任意的点坐标绘制出的一条光滑曲线.它在作图工具或动画中中运用得比较多,例如PS中的钢笔工具,firework中的画笔等等 ...