UVa563_Crimewave(网络流/最大流)(小白书图论专题)
解题报告
思路:
要求抢劫银行的伙伴(想了N多名词来形容,强盗,贼匪,小偷,sad。都认为不合适)不在同一个路口相碰面,能够把点拆成两个点,一个入点。一个出点。
再设计源点s连向银行位置。再矩阵外围套上一圈。连向汇点t
矩阵内的点,出点和周围的点的出点相连。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#define M 500000
#define N 10000
#define inf 0x3f3f3f3f
using namespace std;
int n,m,h,w,head[N],pre[N],l[N],mmap[N][N],cnt,s,t;
struct node {
int v,w,next;
} edge[M];
int dx[]= {-1,0,1,0};
int dy[]= {0,1,0,-1};
void add(int u,int v,int w) {
edge[cnt].v=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt++; edge[cnt].v=u;
edge[cnt].w=0;
edge[cnt].next=head[v];
head[v]=cnt++;
}
int bfs() {
memset(l,-1,sizeof(l));
l[s]=0;
int i,u,v;
queue<int >Q;
Q.push(s);
while(!Q.empty()) {
u=Q.front();
Q.pop();
for(i=head[u]; i!=-1; i=edge[i].next) {
v=edge[i].v;
if(l[v]==-1&&edge[i].w) {
l[v]=l[u]+1;
Q.push(v);
}
}
}
return l[t]>0;
}
int dfs(int u,int f) {
int a,flow=0;
if(u==t)return f;
for(int i=head[u]; i!=-1; i=edge[i].next) {
int v=edge[i].v;
if(l[v]==l[u]+1&&edge[i].w&&(a=dfs(v,min(f,edge[i].w)))) {
edge[i].w-=a;
edge[i^1].w+=a;
flow+=a;
f-=a;
if(!f)break;
}
}
if(!flow)l[u]=-1;
return flow;
}
int main() {
int i,j,T,k,x,y;
scanf("%d",&T);
while(T--) {
cnt=k=0;
memset(head,-1,sizeof(head));
scanf("%d%d%d",&h,&w,&m);
h+=2;
w+=2;
for(i=0; i<h; i++) {
for(j=0; j<w; j++) {
mmap[i][j]=++k;
}
}
s=0;
t=k*2+1;
for(i=1; i<h-1; i++) {
for(j=1; j<w-1; j++) {
add(mmap[i][j],mmap[i][j]+k,1);
for(int l=0; l<4; l++) {
int x=i+dx[l];
int y=j+dy[l];
if(x>=1&&x<h-1&&y>=1&&y<w-1)
add(mmap[i][j]+k,mmap[x][y],1);
}
}
}
for(i=1; i<w-1; i++) {
add(mmap[1][i]+k,mmap[0][i],1);
add(mmap[0][i],t,1);
add(mmap[h-2][i]+k,mmap[h-1][i],1);
add(mmap[h-1][i],t,1);
}
for(i=1; i<h-1; i++) {
add(mmap[i][1]+k,mmap[i][0],1);
add(mmap[i][0],t,1);
add(mmap[i][w-2]+k,mmap[i][w-1],1);
add(mmap[i][w-1],t,1);
}
for(i=0; i<m; i++) {
scanf("%d%d",&x,&y);
add(s,mmap[x][y],1);
}
int ans=0,a;
while(bfs())
while(a=dfs(s,inf))
ans+=a;
if(ans==m)
printf("possible\n");
else printf("not possible\n");
}
return 0;
}
| Crimewave |
Nieuw Knollendam is a very modern town. This becomes clear already when looking at the layout of its map, which is just a rectangular grid of streets and avenues. Being an important trade centre, Nieuw Knollendam
also has a lot of banks. Almost on every crossing a bank is found (although there are never two banks at the same crossing). Unfortunately this has attracted a lot of criminals. Bank hold-ups are quite common, and often on one day several banks are robbed.
This has grown into a problem, not only to the banks, but to the criminals as well. After robbing a bank the robber tries to leave the town as soon as possible, most of the times chased at high speed by the police. Sometimes two running criminals pass the
same crossing, causing several risks: collisions, crowds of police at one place and a larger risk to be caught.
To prevent these unpleasant situations the robbers agreed to consult together. Every Saturday night they meet and make a schedule for the week to come: who is going to rob which bank on which day? For every day they try to plan the get-away routes, such that
no two routes use the same crossing. Sometimes they do not succeed in planning the routes according to this condition, although they believe that such a planning should exist.
Given a grid of
and the crossings where the banks to be robbed are located, find out whether or not it is possible to plan a
get-away route from every robbed bank to the city-bounds, without using a crossing more than once.
Input
The first line of the input contains the number of problems p to be solved.
- The first line of every problem contains the number s of streets (
), followed by the number a of avenues
(
), followed by the number b (
)
of banks to be robbed. - Then b lines follow, each containing the location of a bank in the form of two numbers x (the number of the street) andy (the number of the avenue). Evidently
and
.
Output
The output file consists of p lines. Each line contains the text possible or not
possible. If it is possible to plan non-crossing get-away routes, this line should contain the word: possible. If this is not possible, the line
should contain the words not possible.
Sample Input
2
6 6 10
4 1
3 2
4 2
5 2
3 4
4 4
5 4
3 6
4 6
5 6
5 5 5
3 2
2 3
3 3
4 3
3 4
Sample Output
possible
not possible

Miguel A. Revilla
1998-03-10
UVa563_Crimewave(网络流/最大流)(小白书图论专题)的更多相关文章
- UVa753/POJ1087_A Plug for UNIX(网络流最大流)(小白书图论专题)
解题报告 题意: n个插头m个设备k种转换器.求有多少设备无法插入. 思路: 定义源点和汇点,源点和设备相连,容量为1. 汇点和插头相连,容量也为1. 插头和设备相连,容量也为1. 可转换插头相连,容 ...
- UVa10099_The Tourist Guide(最短路/floyd)(小白书图论专题)
解题报告 题意: 有一个旅游团如今去出游玩,如今有n个城市,m条路.因为每一条路上面规定了最多可以通过的人数,如今想问这个旅游团人数已知的情况下最少须要运送几趟 思路: 求出发点到终点全部路其中最小值 ...
- UVa567_Risk(最短路)(小白书图论专题)
解题报告 option=com_onlinejudge&Itemid=8&category=7&page=show_problem&problem=508"& ...
- UVa10048_Audiophobia(最短路/floyd)(小白书图论专题)
解题报告 题意: 求全部路中最大分贝最小的路. 思路: 类似floyd算法的思想.u->v能够有另外一点k.通过u->k->v来走,拿u->k和k->v的最大值和u-&g ...
- UVa10397_Connect the Campus(最小生成树)(小白书图论专题)
解题报告 题目传送门 题意: 使得学校网络互通的最小花费,一些楼的线路已经有了. 思路: 存在的线路当然全都利用那样花费肯定最小,把存在的线路当成花费0,求最小生成树 #include <ios ...
- UVa409_Excuses, Excuses!(小白书字符串专题)
解题报告 题意: 找包括单词最多的串.有多个按顺序输出 思路: 字典树爆. #include <cstdio> #include <cstring> #include < ...
- (通俗易懂小白入门)网络流最大流——EK算法
网络流 网络流是模仿水流解决生活中类似问题的一种方法策略,来看这么一个问题,有一个自来水厂S,它要向目标T提供水量,从S出发有不确定数量和方向的水管,它可能直接到达T或者经过更多的节点的中转,目前确定 ...
- 正睿OI国庆DAY2:图论专题
正睿OI国庆DAY2:图论专题 dfs/例题 判断无向图之间是否存在至少三条点不相交的简单路径 一个想法是最大流(后来说可以做,但是是多项式时间做法 旁边GavinZheng神仙在谈最小生成树 陈主力 ...
- POJ 1459-Power Network(网络流-最大流-ISAP)C++
Power Network 时间限制: 1 Sec 内存限制: 128 MB 题目描述 A power network consists of nodes (power stations, cons ...
随机推荐
- css中常见几种float方式以及倒计时(刷新页面不清)
css中常见几种float方式 http://jingyan.baidu.com/article/72ee561a670269e16138dfd5.html <script type=" ...
- mongo 3.4分片集群系列之二:搭建分片集群--哈希分片
这个系列大致想跟大家分享以下篇章: 1.mongo 3.4分片集群系列之一:浅谈分片集群 2.mongo 3.4分片集群系列之二:搭建分片集群--哈希分片 3.mongo 3.4分片集群系列之三:搭建 ...
- (转)版本管理工具介绍——SVN篇(二)
http://blog.csdn.net/yerenyuan_pku/article/details/72620498 上一篇文章我介绍了一下SVN,以及SVN服务器的安装,相信大家都安装了,接下来我 ...
- CAD绘制单行文字(网页版)
在CAD设计时,需要绘制文字,用户可以设置设置绘制文字的高度等属性. 主要用到函数说明: _DMxDrawX::DrawText 绘制一个单行文字.详细说明如下: 参数 说明 DOUBLE dPosX ...
- jekyll本地环境搭建(Windows)
序:最近一直在搞Github建站,所以一直没机会写文章,那边的环境虽然搞好了,但是网站的界面却是个问题,不想用别人的,总想自己设计个,却感觉没经验吧,就一直耽搁了.所以也就没心情在那边写文章,很久没写 ...
- acm学习指引
acm学习心得及书籍推荐 一般要做到50行以内的程序不用调试.100行以内的二分钟内调试成功.acm主要是考算法的,主要时间是花在思考算法上,不是花在写程序与debug上. 下面给个计划练练: 第 ...
- Luogu P1540 机器翻译
思路 大水题,只需要静下心来模拟就行.我一开始做的时候,首先想到滚动数组但是写完之后发现并不符合题目要求.题目要求新加入的单词作为最新的,在时间上属于最后一个.但是如果用滚动数组的话,新加入的单词就成 ...
- ZOJ - 3993 - Safest Buildings (数学)
参考:https://blog.csdn.net/KuHuaiShuXia/article/details/78408194 题意: 描述了吃鸡刷圈的问题,给出楼的坐标点,和两次刷圈的半径R和r,现在 ...
- Python-组合数据类型
集合类型及操作 >集合类型定义 集合是多个元素的无序组合 -集合类型与数学中的集合概念一致 -集合元素之间无序,每个元素唯一,不存在相同元素 -集合元素不可更改,不能是可变数据类型 -集合用大括 ...
- 关于vuex自己理解的三幅图