码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3057 Evacuation (二分匹配)

时间:2017-10-08 20:29:20      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:set   bool   inf   blog   一个人   name   ons   txt   double   

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

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

代码如下:

#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 (二分匹配)

标签:set   bool   inf   blog   一个人   name   ons   txt   double   

原文地址:http://www.cnblogs.com/dwtfukgv/p/7638268.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!