POJ 3057 Evacuation (二分匹配)
题意:给定一个图,然后有几个门,每个人要出去,但是每个门每个秒只能出去一个,然后问你最少时间才能全部出去。
析:初一看,应该是像搜索,但是怎么保证每个人出去的时候都不冲突呢,毕竟每个门每次只能出一个人,并不好处理,既然这样,我们可以把每个门和时间的做一个二元组,然后去对应每个人,这样的话,就是成了二分图的匹配,就能做了。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 10;
const int maxm = 1e5 + 10;
const int mod = 30007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} char s[15][15];
vector<P> door, peo;
int d[15][15][15][15]; void bfs(int r, int c){
d[r][c][r][c] = 0;
queue<P> q;
q.push(P(r, c)); while(!q.empty()){
P p = q.front(); q.pop();
for(int i = 0; i < 4; ++i){
int x = p.first + dr[i];
int y = p.second + dc[i];
if(!is_in(x, y) || d[r][c][x][y] <= d[r][c][p.fi][p.se] + 1 || s[x][y] != '.') continue;
d[r][c][x][y] = d[r][c][p.fi][p.se] + 1;
q.push(P(x, y));
}
}
} struct Edge{
int to, next;
};
Edge edge[maxn<<4];
int cnt, head[maxn]; void addEdge(int u, int v){
edge[cnt].to = v;
edge[cnt].next = head[u];
head[u]= cnt++;
} int match[maxn];
bool used[maxn]; bool dfs(int u){
used[u] = 1;
for(int i = head[u]; ~i; i = edge[i].next){
int v = edge[i].to, w = match[v];
if(w < 0 || !used[w] && dfs(w)){
match[u] = v;
match[v] = u;
return true;
}
}
return false;
} int main(){
int T; cin >> T;
while(T--){
scanf("%d %d", &n, &m);
door.cl; peo.cl;
for(int i = 0; i < n; ++i){
scanf("%s", s[i]);
}
ms(d, INF);
FOR(i, 0, n) for(int j = 0; j < m; ++j){
if(s[i][j] == '.') peo.push_back(P(i, j));
else if(s[i][j] == 'D'){
door.push_back(P(i, j));
bfs(i, j);
}
}
ms(head, -1); cnt = 0;
int sum = n * m, ss = door.sz * peo.sz;
FOR(i, 0, door.sz) for(int j = 0; j < peo.sz; ++j){
int tmp = d[door[i].fi][door[i].se][peo[j].fi][peo[j].se];
if(tmp == INF) continue;
for(int k = tmp; k <= sum; ++k){
addEdge((k-1)*door.sz + i, ss + j);
addEdge(ss + j, (k-1)*door.sz + i);
}
}
int ans = 0; ms(match, -1);
int res = -1;
for(int i = 0; i < ss; ++i) if(match[i] < 0){
ms(used, 0); if(dfs(i) && ++ans == peo.sz){ res = i / (int)door.sz + 1; break; }
}
if(res == -1) puts("impossible");
else printf("%d\n", res);
}
return 0;
}
POJ 3057 Evacuation (二分匹配)的更多相关文章
- TTTTTTTTTTTTT poj 3057 Evacuation 二分图匹配+bfs
题意:见挑战230页 #include <iostream> #include <cstdio> #include <cstring> #include <c ...
- POJ 3057 Evacuation 二分+最大流
Evacuation 题目连接: http://poj.org/problem?id=3057 Description Fires can be disastrous, especially when ...
- POJ 3057 Evacuation 二分图匹配
每个门每个时间只能出一个人,那就把每个门拆成多个,对应每个时间. 不断增加时间,然后增广,直到最大匹配. //#pragma comment(linker, "/STACK:10240000 ...
- POJ 3057 Evacuation 题解
题目 Fires can be disastrous, especially when a fire breaks out in a room that is completely filled wi ...
- POJ 3041 - 最大二分匹配
这道题实现起来还是比较简单的,但是理解起来可能有点困难. 我最开始想到的是贪心法,每次消灭当前小行星最多的一行或一列.然而WA了.Discuss区里已经有高人给出反例. 下面给出正确的解法 我们把行和 ...
- POJ 3057 Evacuation(二分匹配)
分析: 这是一个时间和门的二元组(t,d)和人p匹配的问题,当我们固定d0时,(t,d0)匹配的人数和t具有单调性. t增加看成是多增加了边就行了,所以bfs处理出p到每个d的最短时间,然后把(t,d ...
- POJ 3057 Evacuation(二分图匹配+BFS)
[题目链接] http://poj.org/problem?id=3057 [题目大意] 给出一个迷宫,D表示门,.表示人,X表示不可通行, 每个门每时间单位只允许一个人通过, 每个人移动一格的为一时 ...
- 【最大匹配+二分答案】POJ 3057 Evacuation
题目大意 POJ链接 有一个\(X×Y\)的房间,X代表墙壁,D是门,.代表人.这个房间着火了,人要跑出去,但是每一个时间点只有一个人可以从门出去. 问最后一个人逃出去的最短时间,如果不能逃出去,输出 ...
- poj 2446 Chessboard (二分匹配)
Chessboard Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12800 Accepted: 4000 Descr ...
随机推荐
- Brackets编辑器使用
常用快捷操作 Ctrl + b 当选中一个文本时,离该文本最近的相同的文本会被高亮显示,这样,相同的2个文本就全部获得了焦点,可以同时更改高亮文本.(对,只会找寻最近的且只找到一个就不找了!惰性查找. ...
- oracle之 关闭透明大页
方法一: 1.设置/etc/grub.conf文件,添加 transparent_hugepage=never ,在系统启动是禁用 [root@hbdw1 ~]# cat /etc/grub.conf ...
- List of LTE networks
https://en.wikipedia.org/wiki/List_of_LTE_networks
- emacs里面模拟vim按键操作的插件evil
emacsConfig/evil-setting.el (setq evil-mode t) (setq evil-shift-width ) ;; some modes aren't meant f ...
- 黄聪:VS2010启动程序提示文件加载 使用 简体中文(GB2312)编码加载文件解决办法
vs2010 错误提示框:文件加载 使用 简体中文(GB2312)编码加载文件C:\Users\Administrator\AppData\Local\Temp\nxhgjasi.5au \Temp\ ...
- 转转转--Java File和byte数据之间的转换
package cn.iworker.file; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; ...
- [转][ASP.NET]ASP.NET 预编译网站
[转自]https://msdn.microsoft.com/zh-cn/library/ms227430(v=vs.80).aspx C:\Windows\Microsoft.NET\Framewo ...
- Codeforces Round #474-E(树形dp)
一.题目链接 http://codeforces.com/contest/960/problem/B 二.题意 给定一棵$N$个节点的树,每个节点的权值$V$.定义树中两点$u_1$和$u_m$的权值 ...
- C++ 并发编程 01 线程api
1.使用多线程的好处: 提高性能,分离关注点 2. 多线程所在头文件 <thread> 3. 使用线程方式为std::thread(functioncall),如: #include & ...
- PDF预览之PDFObject.js总结
get from:PDF预览之PDFObject.js总结 PDFObject.js - 将PDF嵌入到一个div内,而不是占据整个页面(要求浏览器支持显示PDF,不支持,可配置PDF.js来实现 ...