题意:给定一个图,然后有几个门,每个人要出去,但是每个门每个秒只能出去一个,然后问你最少时间才能全部出去。

析:初一看,应该是像搜索,但是怎么保证每个人出去的时候都不冲突呢,毕竟每个门每次只能出一个人,并不好处理,既然这样,我们可以把每个门和时间的做一个二元组,然后去对应每个人,这样的话,就是成了二分图的匹配,就能做了。

代码如下:

#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 (二分匹配)的更多相关文章

  1. TTTTTTTTTTTTT poj 3057 Evacuation 二分图匹配+bfs

    题意:见挑战230页 #include <iostream> #include <cstdio> #include <cstring> #include <c ...

  2. POJ 3057 Evacuation 二分+最大流

    Evacuation 题目连接: http://poj.org/problem?id=3057 Description Fires can be disastrous, especially when ...

  3. POJ 3057 Evacuation 二分图匹配

    每个门每个时间只能出一个人,那就把每个门拆成多个,对应每个时间. 不断增加时间,然后增广,直到最大匹配. //#pragma comment(linker, "/STACK:10240000 ...

  4. POJ 3057 Evacuation 题解

    题目 Fires can be disastrous, especially when a fire breaks out in a room that is completely filled wi ...

  5. POJ 3041 - 最大二分匹配

    这道题实现起来还是比较简单的,但是理解起来可能有点困难. 我最开始想到的是贪心法,每次消灭当前小行星最多的一行或一列.然而WA了.Discuss区里已经有高人给出反例. 下面给出正确的解法 我们把行和 ...

  6. POJ 3057 Evacuation(二分匹配)

    分析: 这是一个时间和门的二元组(t,d)和人p匹配的问题,当我们固定d0时,(t,d0)匹配的人数和t具有单调性. t增加看成是多增加了边就行了,所以bfs处理出p到每个d的最短时间,然后把(t,d ...

  7. POJ 3057 Evacuation(二分图匹配+BFS)

    [题目链接] http://poj.org/problem?id=3057 [题目大意] 给出一个迷宫,D表示门,.表示人,X表示不可通行, 每个门每时间单位只允许一个人通过, 每个人移动一格的为一时 ...

  8. 【最大匹配+二分答案】POJ 3057 Evacuation

    题目大意 POJ链接 有一个\(X×Y\)的房间,X代表墙壁,D是门,.代表人.这个房间着火了,人要跑出去,但是每一个时间点只有一个人可以从门出去. 问最后一个人逃出去的最短时间,如果不能逃出去,输出 ...

  9. poj 2446 Chessboard (二分匹配)

    Chessboard Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12800   Accepted: 4000 Descr ...

随机推荐

  1. 华为OJ:2199 推断输入字符串中的括号匹配

    依据不同的括号有个计数器.在遍历时.当计数器小于0则返回false或者当遍历完后,计数器仍旧不为零,也返回false. import java.util.Scanner; public class b ...

  2. 【linux】linux权限管理

    一.权限的基本概念                                                   权限:访问计算机资源或服务的访问能力. Linux中,每一个资源或者服务的权限, ...

  3. ReportViewer 2010 打印预览,用鼠标快速切换显示比例时报错:存储空间不足,不能处理此命令

    CreateCompatibleDIB 存储空间不足 无法处理此命令 安装 ReportViewer 2010 sp1 即可.

  4. java scanner工具类

    import java.util.Scanner; public class ScannerTest { public static void main(String[] args) { Scanne ...

  5. Bootstrap-Other:HTML编码规范

    ylbtech-Bootstrap-Other:HTML编码规范 1.返回顶部 1. Bootstrap HTML编码规范 语法 用两个空格来代替制表符(tab) -- 这是唯一能保证在所有环境下获得 ...

  6. sort+函数指针、sort+比较器对象、qsort速度比较

    一.上代码 #include<bits/stdc++.h> using namespace std; #define MAXN 50000000 struct TS { int a, b, ...

  7. 使用Javamail发送邮件Util

    maven: <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artif ...

  8. 好记性不如烂笔头-linux学习笔记2kickstart自动化安装和cacti

    kickstart自动化安装的逻辑梳理 主要是安装tftp nfs dhcp 然后配置kickstart 原来就是先安装tftp 可实现不同机器的文件下载 然后在安装nfs 就是主服务器的文件系统 然 ...

  9. php在线编辑本地文件方法共享

    public function testfile() { $cfile='F:\phpStudy\WWW\thinkphp5practise\NNWinLoseConfig.ini'; $cfileh ...

  10. Group By 和Having总结

    1.Group By 概述 “Group By”从字面意义上理解就是根据“By”指定的规则对数据进行分组 所谓的分组就是将一个“数据集”划分成若干个“小区域”,然后针对若干个“小区域”进行数据处理. ...