POJ 3057 Evacuation(二分匹配)
分析:
这是一个时间和门的二元组(t,d)和人p匹配的问题,当我们固定d0时,(t,d0)匹配的人数和t具有单调性。
t增加看成是多增加了边就行了,所以bfs处理出p到每个d的最短时间,然后把(t,d)和p连边,按t从小到大
枚举点增广就好了。无解的情况只有一种,某个人无论如何都无法出去。
/*********************************************************
* --Sakura hirahira mai orite ochite-- *
* author AbyssalFish *
**********************************************************/
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
using namespace std; typedef long long ll; const int XY = , MAX_D = , MAX_P = ;//..MAX_T
char maz[XY][XY+]; const int maxv = MAX_D*MAX_P, maxe = maxv*MAX_P;
int hd[maxv],to[maxe],nx[maxe],ec;
#define eachEage int i = hd[u]; ~i; i = nx[i]
void add(int u,int v)
{
nx[ec] = hd[u];
to[ec] = v;
hd[u] = ec++;
} #define PB push_back
#define resv(x,n) x.reserve(n);
#define PS push
vector<int> pX, pY, dX, dY; const int dx[] = {,,-,}, dy[] = {,-,,}; int dist[XY][XY][XY][XY];
int X,Y; void bfs(int x,int y)
{
int (* const d)[XY] = dist[x][y];
memset(d,0xff,sizeof(dist[x][y])); queue<int> qx, qy;
d[x][y] = ;
qx.PS(x); qy.PS(y);
while(qx.size()){
x = qx.front(); qx.pop();
y = qy.front(); qy.pop();
for(int k = ; k--;){
int nx = x+dx[k], ny = y+dy[k];
if(<=nx && nx<X && <=ny && ny<Y && maz[nx][ny] == '.' && d[nx][ny]<){
d[nx][ny] = d[x][y]+;
qx.PS(nx); qy.PS(ny);
}
}
}
} int link[MAX_P];
int vis[maxv], clk; bool aug(int u)
{
vis[u] = clk;
for(eachEage){
int v = to[i], w = link[v];
if(w< || (vis[w] != clk && aug(w))){
link[v] = u;
return true;
}
}
return false;
} //#define LOCAL
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif
resv(pX,MAX_P) resv(pX,MAX_P) resv(dX,MAX_D) resv(dY,MAX_D)
int T; cin>>T;
while(T--){
scanf("%d%d",&X,&Y);
pX.clear(); pY.clear();
dX.clear(); dY.clear();
for(int i = ; i < X; i++){
scanf("%s", maz[i]);
for(int j = ; j < Y; j++){
if(maz[i][j] == 'D'){
dX.PB(i); dY.PB(j);
}
else if(maz[i][j] == '.'){
pX.PB(i); pY.PB(j);
}
}
}
int d = dX.size(), p = pX.size();
if(p == ){ puts(""); continue; }
for(int i = ; i < d; i++){
bfs(dX[i],dY[i]);
}
int n = (X-)*(Y-);
bool fail = false;
memset(hd,0xff,sizeof(int)*n*d); ec = ;
for(int i = ; i < p; i++){
bool escape = false;
for(int j = ; j < d; j++){
if(dist[dX[j]][dY[j]][pX[i]][pY[i]] > ){
if(!escape) escape = true;
for(int k = dist[dX[j]][dY[j]][pX[i]][pY[i]]-; k < n; k++){
add(k*d+j, i);
}
}
}
if(!escape){
fail = true; break;
}
}
if(fail){ puts("impossible"); continue; }
memset(link,-,sizeof(int)*p);
int cnt = , ans;
for(int i = ; i < n*d; i++){
clk++;
if(aug(i) && ++cnt == p) {
ans = i/d+;
}
}
printf("%d\n", ans);
}
return ;
}
POJ 3057 Evacuation(二分匹配)的更多相关文章
- 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(二分图匹配+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 ...
随机推荐
- cf873F(xjb+二分)
题目链接:http://codeforces.com/problemset/problem/837/F 题意:给出一个大小为 n 的数组 a 和一个数 k,每次操作后的到一个 a' 数组,a'i 为 ...
- Openjudge jubeeeeeat
jubeeeeeat 题目链接 总时间限制: 1000ms 内存限制: 256000kB 描述 众所周知,LZF很喜欢打一个叫Jubeat的游戏.这是个音乐游戏,游戏界面是4×4的方阵,会根据音乐 ...
- 清北刷题冲刺 11-03 a.m
纸牌 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> ...
- Nacos深入浅出(十)
基本上到第9篇,整个请求的一套就结束了,感觉这里跳跳绕绕很多东西,下面我们来做个总结:从Nacos配置平台修改,到Client请求更新,事件触发去取值返回给客户端,整个过程感觉只分析到了4.5层的深度 ...
- Codeforces Round #499 (Div. 1)部分题解(B,C,D)
Codeforces Round #499 (Div. 1) 这场本来想和同学一起打\(\rm virtual\ contest\)的,结果有事耽搁了,之后又陆陆续续写了些,就综合起来发一篇题解. B ...
- 洛谷P3078 [USACO13MAR]扑克牌型Poker Hands
题目描述 Bessie and her friends are playing a unique version of poker involving a deck with \(N\) (\(1 \ ...
- npm install 权限问题
npm ERR! Error: EACCES: permission denied, access '/Users/Lobin/work/note-vue/node_modules/@babel/hi ...
- linux下ssh key秘钥登陆远程服务器设置
本地的用户名需要和ssh服务器的用户名一致 1.在Server服务器上加载私钥文件ssh-add wang_rsa 2.如果系统提示:could not open a connection to yo ...
- 74th LeetCode Weekly Contest Valid Tic-Tac-Toe State
A Tic-Tac-Toe board is given as a string array board. Return True if and only if it is possible to r ...
- 1105 Spiral Matrix(25 分)
This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...