POJ 3057 Evacuation (二分匹配)
阿新 • • 發佈:2017-10-08
turn next urn != sca first cnblogs eva tmp
題意:給定一個圖,然後有幾個門,每個人要出去,但是每個門每個秒只能出去一個,然後問你最少時間才能全部出去。
析:初一看,應該是像搜索,但是怎麽保證每個人出去的時候都不沖突呢,畢竟每個門每次只能出一個人,並不好處理,既然這樣,我們可以把每個門和時間的做一個二元組,然後去對應每個人,這樣的話,就是成了二分圖的匹配,就能做了。
代碼如下:
#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 (二分匹配)