URAL 1277 - Cops and Thieves - [无向图点带权的最小点割]
题目链接:https://cn.vjudge.net/problem/URAL-1277
Input
Output
Example
input | output |
---|---|
10 |
NO |
10 |
YES |
至于为什么要这样拆边,假设in(k)==k',out(k)==k'',即我们要addedge(out(i), in(j), INF)和addedge(out(j), in(i), INF),
为什么要这样addedge呢?因为我们的最大流算法默认都是有向图,addedge加入的边都是有向边,
这样加入边确保了该点k所有的入弧都是连接到in(k),出弧都是连接在out(k);
这使得原本所有流经该点的flow,现在都必须流经edge(in(k), out(k), w),确保了算法的正确性(不是很清楚的话,可以手动画图拆点拆边试试)。
(原本我想当然的addedge(out(i), in(j), INF)和addedge(in(j), in(i), INF),果断WA了……)
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#define MAX 2*100+5
#define INF 0x3f3f3f3f
#define in(x) x
#define out(x) x+N
using namespace std;
int K,N,M,S,F;
struct Edge{
int u,v,c,f;
};
struct Dinic
{
int s,t;
vector<Edge> E;
vector<int> G[MAX];
bool vis[MAX];
int lev[MAX];
int cur[MAX];
void addedge(int from,int to,int cap)
{
E.push_back((Edge){from,to,cap,});
E.push_back((Edge){to,from,,});
G[from].push_back(E.size()-);
G[to].push_back(E.size()-);
}
bool bfs()
{
memset(vis,,sizeof(vis));
queue<int> q;
q.push(s);
lev[s]=;
vis[s]=;
while(!q.empty())
{
int now=q.front(); q.pop();
for(int i=,_size=G[now].size();i<_size;i++)
{
Edge edge=E[G[now][i]];
int nex=edge.v;
if(!vis[nex] && edge.c>edge.f)
{
lev[nex]=lev[now]+;
q.push(nex);
vis[nex]=;
}
}
}
return vis[t];
}
int dfs(int now,int aug)
{
if(now==t || aug==) return aug;
int flow=,f;
for(int& i=cur[now],_size=G[now].size();i<_size;i++)
{
Edge& edge=E[G[now][i]];
int nex=edge.v;
if(lev[now]+ == lev[nex] && (f=dfs(nex,min(aug,edge.c-edge.f)))>)
{
edge.f+=f;
E[G[now][i]^].f-=f;
flow+=f;
aug-=f;
if(!aug) break;
}
}
return flow;
}
int maxflow()
{
int flow=;
while(bfs())
{
memset(cur,,sizeof(cur));
flow+=dfs(s,INF);
}
return flow;
}
}dinic;
int main()
{
scanf("%d%d%d%d%d",&K,&N,&M,&S,&F);
for(int i=,x;i<=N;i++)
{
scanf("%d",&x);
dinic.addedge(in(i),out(i),x);
dinic.addedge(out(i),in(i),x);
}
for(int i=,a,b;i<=M;i++)
{
scanf("%d%d",&a,&b);
dinic.addedge(out(a),in(b),INF);
dinic.addedge(out(b),in(a),INF);
}
if(S==F){
printf("NO\n");
return ;
}
dinic.s=out(S), dinic.t=in(F);
if(dinic.maxflow()<=K) printf("YES\n");
else printf("NO\n");
return ;
}
URAL 1277 - Cops and Thieves - [无向图点带权的最小点割]的更多相关文章
- URAL 1277 Cops and Thieves
Cops and Thieves Time Limit: 1000ms Memory Limit: 16384KB This problem will be judged on Ural. Origi ...
- 【Ural1277】 Cops and Thieves 无向图点连通度问题
1277. Cops and Thieves Time limit: 1.0 secondMemory limit: 64 MB The Galaxy Police (Galaxpol) found ...
- URAL1277 Cops and Thieves(最小割)
Cops and Thieves Description: The Galaxy Police (Galaxpol) found out that a notorious gang of thieve ...
- URAL - 1003:Parity (带权并查集&2-sat)
Now and then you play the following game with your friend. Your friend writes down a sequence consis ...
- 带权图的最短路径算法(Dijkstra)实现
一,介绍 本文实现带权图的最短路径算法.给定图中一个顶点,求解该顶点到图中所有其他顶点的最短路径 以及 最短路径的长度.在决定写这篇文章之前,在网上找了很多关于Dijkstra算法实现,但大部分是不带 ...
- poj 2492 A Bug's Life【带权并查集】
就是给一个无向图判是否有奇环 用带权并查集来做,边权1表示连接的两个节点异性,否则同性,在%2意义下进行加法运算即可,最后判相同的时候也要%2,因为可能有负数 #include<iostream ...
- 有向网络(带权的有向图)的最短路径Dijkstra算法
什么是最短路径? 单源最短路径(所谓单源最短路径就是只指定一个顶点,最短路径是指其他顶点和这个顶点之间的路径的权值的最小值) 什么是最短路径问题? 给定一带权图,图中每条边的权值是非负的,代表着两顶点 ...
- 浅谈并查集&种类并查集&带权并查集
并查集&种类并查集&带权并查集 前言: 因为是学习记录,所以知识讲解+例题推荐+练习题解都是放在一起的qvq 目录 并查集基础知识 并查集基础题目 种类并查集知识 种类并查集题目 并查 ...
- 51nod1459(带权值的dijkstra)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1459 题意:中文题诶- 思路:带权值的最短路,这道题数据也没 ...
随机推荐
- Spring IOC-ContextLoaderListener
[spring version : 4.1.6.RELEASE] 使用spring的项目中,一般都会在web.xml中配置ContextLoaderListener,它就是spring ioc 的入口 ...
- PHP从数组中找到指定元素的位置
群里有人问,有个数组五个元素 分为1到5 现在要求 循环找出3元素的索引,怎么做性能才是最高. 我不知道哪个性能最高,但是我想提出可以用多种方式进行查找,然后进行比较选择. 我想,最简单最基础的 应 ...
- IDEA maven项目下测试mybatis例子,使用mappper class或package引入mapper映射文件,总是报错Invalid bound statement(所有配置完全正确)
困扰几个小时,终于查到解决办法及原因(可以直接到最后看解决方案) 环境就是用IDEA搭建的maven项目,主要jar包引入配置如下 <dependencies> <dependenc ...
- vmware 安装 Mac OS X 10.9 Mavericks
This guide shows how to install fresh OS X 10.9 Mavericks on VMware workstation with Windows 7 or Wi ...
- [XPath] XPath 与 lxml (四)XPath 运算符
XPath 中支持的运算符 # | 或: 返回所有 price 和 title 节点集合 >>> root.xpath('//price|//title') [<Element ...
- 分析JobInProgress中Map/Reduce任务分配
1.JobTracker能否决定给当前的TaskTracker节点分配一个Job的具体的哪一个任务? 2.什么是map本地任务? 3.nonRunningMapCache的作用是什么? 4.从Task ...
- 十款不错的Hybrid App移动开发框架
本文转载至http://www.pureasme.com/blog/2015/0419476.html ionic 是个高级的 HTML5 移动端应用框架,是个很漂亮的使用 HTML5 开发混合移动应 ...
- MyBatis批量更新for Mysql 实例
<update id="UpdatePwd" parameterType="java.util.List"> UPDATE FP_USER_BASE ...
- 非static成员函数通过类名::来调用,空指针调用成员方法不出错!
首先来看这一段代码: #include <iostream> using namespace std; class A{ public: int k; void p1(){ cout< ...
- U盘安装centos7:不能载入到安装界面
在用U盘安装centos7时,我们需要修改镜像位置: 选择第一项:Install CentOS 7 ,按 e(也有可能是tab键)键进入编辑界面. 将 vmlinuz initrd=initrd.im ...