poj 3057

2024-05-27 13:58
文章标签 poj 3057

本文主要是介绍poj 3057,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

       这道题是一道用二分图最大匹配算法解决的问题,关键在于如何构图,是一道难题。

       题目里说一个门每秒只能通过一个人,这一限制条件给解题造成很大的难度。解决这道题时,要把门和时间结合起来考虑,也就是说要区分1s时刻的门和2s时刻的门,即一个人在时间为1的情况下通过门和时间为2的情况下通过同一扇门是不同的匹配。

        简单来说,构图就是把人作为X集合中的点,把门和时间一起考虑,作为Y集合中的点,如果任何情况下人不能全被匹配,说明有一些人无法逃脱,否则肯定有一个最短时间能让所有人逃脱。而为了找到最短的时间,所以从时间1s开始,构出一个二分图,求最大匹配,如果能匹配所有的点,那么这就是最短的时间,否则时间自增1s,重新构图,再求最大匹配,再判断。如果超过时间上界,我的代码里取时间上界为n*m(n为行数,m为列数),那么则无法让所有人都逃脱。由于最大匹配算法可以从一个初始匹配开始运行,所以当我们在求时间为 t s二分图的最大匹配时,可以把 t-1 s时求得的最大匹配作为一个初始匹配。

       顺便提一下,这道题算法复杂度比较高,从题目给出的范围也可以感觉到,所以要尽可能节约时间,而不能图方便做多余的操作。从我写这道题的过程来看,开始写出来TLE,后来把MAX范围从25改到15,就AC了,时间是297ms,然后再少开一些空间,用以减少memeset函数赋值的时间,AC时间降到94ms,然后减少一个memeset函数的使用,时间又降到79ms,这足以说明一些小细节对于减少这道题通过时间甚至是否AC的重要性,这也是第一次见到MAX范围大了也TLE的情况,以后要多加注意。当然,由于没有多次提交实验,肯定有误差,请不要计较细节。

       这题在《挑战程序设计竞赛》( 人民邮电出版社) P230也有详细解题过程,而我也是参考这个写这道题的,书中对于多维数组作为参数传递的方式上值得学习,虽然不能提高效率,但很简洁,下面的代码也有体现。

代码(C++):

#include <cstdlib>
#include <iostream>
#include <queue>
#include <vector>#define MAX 15
#define INF (1<<30)
using namespace std;//#define LOCALtypedef pair<int,int> pii;
pii p[MAX*MAX],d[MAX*MAX];vector<int> G[MAX*MAX];const int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int dist[MAX][MAX][MAX][MAX],cx[MAX*MAX],cy[2*MAX*MAX*MAX];
int n,m,cp,cd,ans;
bool vis[2*MAX*MAX*MAX];
char map[MAX][MAX];void bfs(int x,int y,int dist[MAX][MAX])
{int a,b,i;queue<pii> qi;pii tmp;dist[x][y]=0;qi.push(make_pair(x,y));while(!qi.empty()){tmp=qi.front();qi.pop();     for(i=0;i<4;i++){a=dir[i][0]+tmp.first;b=dir[i][1]+tmp.second; if(a>=0&&a<n&&b>=0&&b<m&&map[a][b]=='.'&&dist[a][b]==-1) {dist[a][b]=dist[tmp.first][tmp.second]+1;qi.push(make_pair(a,b));                   }          }        }      
}void add_edge(int a,int b)
{G[a].push_back(b);
}bool dfs(int u)
{int i,v;for(i=0;i<G[u].size();i++){v=G[u][i];if(!vis[v]){vis[v]=true;if(cy[v]==-1||dfs(cy[v])){cx[u]=v;cy[v]=u;return true;                     }       }                      }return false;
}void Hungary(int k)
{int i,j;for(i=0;i<cp;i++){if(cx[i]==-1){//memset(vis,false,sizeof(vis));for(j=0;j<cd*k;j++) vis[j]=false;if(dfs(i)) ans++;         }             } 
}int main(int argc, char *argv[])
{
#ifdef LOCALfreopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
#endifint t,i,j,k;scanf("%d",&t);while(t--){scanf("%d %d",&n,&m);   for(i=0;i<n;i++) scanf("%s",map[i]); cp=cd=0;memset(dist,-1,sizeof(dist));for(i=0;i<n;i++){for(j=0;j<m;j++){if(map[i][j]=='.') p[cp++]=make_pair(i,j);else if(map[i][j]=='D'){  d[cd++]=make_pair(i,j);bfs(i,j,dist[i][j]);                                }            }            } if(cp==0){printf("0\n");continue;     }ans=0;memset(cx,-1,sizeof(cx));memset(cy,-1,sizeof(cy));for(k=1;k<n*m;k++){for(i=0;i<cp;i++){for(j=0;j<cd;j++){if(dist[d[j].first][d[j].second][p[i].first][p[i].second]!=-1&&dist[d[j].first][d[j].second][p[i].first][p[i].second]<=k) add_edge(i,j+cd*(k-1));         }             }Hungary(k);if(ans==cp){printf("%d\n",k);  break;} } if(k==n*m) printf("impossible\n");       for(i=0;i<cp;i++) G[i].clear(); }system("PAUSE");return EXIT_SUCCESS;
}

题目( http://poj.org/problem?id=3057):

Evacuation
Time Limit: 1000MS Memory Limit: 65536K
   

Description

Fires can be disastrous, especially when a fire breaks out in a room that is completely filled with people. Rooms usually have a couple of exits and emergency exits, but with everyone rushing out at the same time, it may take a while for everyone to escape. 

You are given the floorplan of a room and must find out how much time it will take for everyone to get out. Rooms consist of obstacles and walls, which are represented on the map by an 'X', empty squares, represented by a '.' and exit doors, which are represented by a 'D'. The boundary of the room consists only of doors and walls, and there are no doors inside the room. The interior of the room contains at least one empty square. 

Initially, there is one person on every empty square in the room and these persons should move to a door to exit. They can move one square per second to the North, South, East or West. While evacuating, multiple persons can be on a single square. The doors are narrow, however, and only one person can leave through a door per second. 

What is the minimal time necessary to evacuate everybody? A person is evacuated at the moment he or she enters a door square.


Input

The first line of the input contains a single number: the number of test cases to follow. Each test case has the following format: 
One line with two integers Y and X, separated by a single space, satisfying 3 <= Y, X <= 12: the size of the room 
Y lines with X characters, each character being either 'X', '.', or 'D': a valid description of a room


Output

For every test case in the input, the output should contain a single line with the minimal evacuation time in seconds, if evacuation is possible, or "impossible", if it is not. 


Sample Input

3
5 5
XXDXX
X...X
D...X
X...D
XXXXX
5 12
XXXXXXXXXXXX
X..........D
X.XXXXXXXXXX
X..........X
XXXXXXXXXXXX
5 5
XDXXX
X.X.D
XX.XX
D.X.X
XXXDX


Sample Output

3
21
impossible

这篇关于poj 3057的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1007673

相关文章

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

poj 1511 Invitation Cards(spfa最短路)

题意是给你点与点之间的距离,求来回到点1的最短路中的边权和。 因为边很大,不能用原来的dijkstra什么的,所以用spfa来做。并且注意要用long long int 来存储。 稍微改了一下学长的模板。 stack stl 实现代码: #include<stdio.h>#include<stack>using namespace std;const int M

poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)

poj 3259: 题意:John的农场里n块地,m条路连接两块地,w个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。 任务是求你会不会在从某块地出发后又回来,看到了离开之前的自己。 判断树中是否存在负权回路就ok了。 bellman代码: #include<stdio.h>const int MaxN = 501;//农场数const int

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

poj 1287 Networking(prim or kruscal最小生成树)

题意给你点与点间距离,求最小生成树。 注意点是,两点之间可能有不同的路,输入的时候选择最小的,和之前有道最短路WA的题目类似。 prim代码: #include<stdio.h>const int MaxN = 51;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int P;int prim(){bool vis[MaxN];

poj 2349 Arctic Network uva 10369(prim or kruscal最小生成树)

题目很麻烦,因为不熟悉最小生成树的算法调试了好久。 感觉网上的题目解释都没说得很清楚,不适合新手。自己写一个。 题意:给你点的坐标,然后两点间可以有两种方式来通信:第一种是卫星通信,第二种是无线电通信。 卫星通信:任何两个有卫星频道的点间都可以直接建立连接,与点间的距离无关; 无线电通信:两个点之间的距离不能超过D,无线电收发器的功率越大,D越大,越昂贵。 计算无线电收发器D

poj 1502 MPI Maelstrom(单源最短路dijkstra)

题目真是长得头疼,好多生词,给跪。 没啥好说的,英语大水逼。 借助字典尝试翻译了一下,水逼直译求不喷 Description: BIT他们的超级计算机最近交货了。(定语秀了一堆词汇那就省略吧再见) Valentine McKee的研究顾问Jack Swigert,要她来测试一下这个系统。 Valentine告诉Swigert:“因为阿波罗是一个分布式共享内存的机器,所以它的内存访问

uva 10061 How many zero's and how many digits ?(不同进制阶乘末尾几个0)+poj 1401

题意是求在base进制下的 n!的结果有几位数,末尾有几个0。 想起刚开始的时候做的一道10进制下的n阶乘末尾有几个零,以及之前有做过的一道n阶乘的位数。 当时都是在10进制下的。 10进制下的做法是: 1. n阶位数:直接 lg(n!)就是得数的位数。 2. n阶末尾0的个数:由于2 * 5 将会在得数中以0的形式存在,所以计算2或者计算5,由于因子中出现5必然出现2,所以直接一

poj 3159 (spfa差分约束最短路) poj 1201

poj 3159: 题意: 每次给出b比a多不多于c个糖果,求n最多比1多多少个糖果。 解析: 差分约束。 这个博客讲差分约束讲的比较好: http://www.cnblogs.com/void/archive/2011/08/26/2153928.html 套个spfa。 代码: #include <iostream>#include <cstdio>#i