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是汇点.最大流就是答案 ...
随机推荐
- 关于string.format() 转
string.format()函数用来生成具有特定格式的字符串,这个函数有两个参数,第一个参数为格式化串:由指示符和控制格式的字符组成.第二个参数是对应格式中每个代号的各种数据. 格式字符串可能包含以 ...
- 开源代码分析之Android/iOS Hybrid JSBridge框架
Hybrid开发是现在的主流形式,对于业务快速迭代的公司尤其重要.曾将在鞋厂接触了很多关于Hybrid的理念,在这里分享一些Hybrid框架思想. Hybrid框架包括Native与H5的通信,Web ...
- Sina 新浪Ip归属地Api 很好用的,使用get请求
前言 这几天做了一个客服管理系统,需要根据游客的Ip获取他的归属地,刚开始使用了,www.ip138.com 抓取ip归属地,用这还可以,但是不显示国家:于是是找了这个新浪的Api,很不错啊 用法 1 ...
- Ueditor百度网页编辑器开发者版java utf-8的使用
index.jsp主要代码: <html> <head> <title>网页编辑器</title> <script type="text ...
- GIT使用笔记-fatal:multiple stage entries for merged file处理办法
该错误是在cherry-pick时出现 无法确定冲突原因 分支无法checkout ,reset等等全都失效 在网上给出的解决办法全部都是 rm .git/index git add -A git c ...
- Spring之Ioc
Spring的特性 >> 轻量级(Lightweight)相较于EJB而言Spring是轻量级的容器,不依赖任何web容器 >> 容器(Container) Spring本身不 ...
- [转]AS3 int uint Number
转自:http://luhantu.iteye.com/blog/1910301 AS3 int uint Number 博客分类: AS3 flex number 类型 1) int 类可使用表示 ...
- Page in/Page out/Page fault
Paging refers to writing portions, termed pages, of a process' memory to disk. Swapping, strictly sp ...
- cursor or set-based
标题可能和正文不太相符.我主要是记录工作中遇到使用游标的语句改成普通set-based operation,执行时间快了很多. 1.游标语句 declare @startDate dateTime d ...
- cocos2dx 3.x(获得父类的node型指针调用父类函数this->getParent())
void CenterLayer::zhanzheng(CCObject* pSender){ ((GameScene*)this->getParent())->showLayer(Gam ...