POJ 3057 Evacuation(一道很好可以加强对二分图理解的题目)

这是挑战上面的一道题。

一开始我按照自己思路,先跑一遍bfs,得到每个人到门的距离,然后每分钟都建立一次二分图,跑一遍二分图,用总人数减去跑出来得到的结果,直到0才停止。
但是,这样的做法是错误的。我自己拿了网上别人AC的代码然后自己造数据发现,下面这一组数据我跑出来的有问题
6 6
XDXXXX
D....D
X.X..X
X.XX.X
X...XX
XXXDXX
答案应该是4但是我的却是5,原因在于在第二秒的时候第二行第三个点被第二行最右边的D匹配掉了,后面无法更正这个结果,导致了第二行右边的D接管了靠近他的五个点。
所以正确的做法依然是,一个门在每个时间点都建立,然后左边是人,右边是门来跑二分图,这样他才会更正上面不是最优的结果。


参考了挑战上面的解法。

代码如下:

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<utility>
#include<stack>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
typedef pair<int, int> pii;
const int maxn = 1005;
int n, m;
int p_x[maxn], p_y[maxn], d_x[maxn], d_y[maxn], p, d;
char graph[maxn][maxn];
bool vst[15][15];
int dis[15][15][15][15], dir[][2] = {0, 1, 0, -1, 1, 0, -1, 0};

int uN, vN;  //u,v数目
vector <int> G[maxn];
int linker[4805];
bool used[4805];
bool dfs(int u)
{
    for(int i = 0; i < G[u].size(); i++) {
    	int v = G[u][i];
        if(!used[v])
        {
            used[v] = true;
            if(linker[v] == -1 || dfs(linker[v]))
            {
                linker[v] = u;
                return true;
            }    
        }  
    }
    return false;  
}    
int hungary()
{
    int res = 0;
    int u;
    memset(linker, -1, sizeof(linker));
    for(u = 0; u < uN; u++)
    {
        memset(used, 0, sizeof(used));
        if(dfs(u))  
			res++;
    } 
    return res;   
}     

int solve() {
	uN = p;
	vN = 0;
	for(int t = 1; t < 105; t++) {
		vN = t * d;
		for(int i = 0; i < p; i++) {
			for(int j = 0; j < d; j++) {
				int tmp = dis[ p_x[i] ][ p_y[i] ][ d_x[j] ][ d_y[j] ];
				if(tmp != -1 && tmp <= t) {
					G[i].push_back((t - 1) * d + j);
				}
			}
		}
		int res = hungary();
		if(res == p)
			return t;
	}
	return -1;
}

struct node{
	int x, y, step;
	node(){}
	node(int a, int b, int c) {
		x = a;
		y = b;
		step = c;
	}
};

inline bool Check(int x, int y) {
	if(x >= 0 && x < n && y >= 0 && y < m && !vst[x][y] && graph[x][y] == '.')
		return 1;
	return 0;
}

inline void bfs(int sx, int sy) {
	node now, next;
	memset(vst, 0, sizeof(vst));
	queue <node> q;
	q.push(node(sx, sy, 0));
	vst[sx][sy] = 1;
	while(!q.empty()) {
		now = q.front();
		q.pop();
		next.step = now.step + 1;
		for(int i = 0; i < 4; i++) {
			next.x = now.x + dir[i][0];
			next.y = now.y + dir[i][1];
			if(Check(next.x, next.y)) {
				q.push(next);
				vst[next.x][next.y] = 1;
				dis[next.x][next.y][sx][sy] = next.step;
			}
		}
	}
}



int main() {
#ifndef ONLINE_JUDGE
	freopen("poj_in.txt", "r", stdin);
//    freopen("poj_out.txt", "w", stdout);
#endif
	int T;
	cin >> T;
	while(T--) {
		cin >> n >> m;
		p = d = 0;
		memset(dis, -1, sizeof(dis));
		for(int i = 0; i < maxn; i++)
			G[i].clear();
		for(int i = 0; i < n; i++) 
			scanf("%s", &graph[i]);
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < m; j++) {
				if(graph[i][j] == 'D') {
					d_x[d] = i;
					d_y[d++] = j;
					bfs(i, j);
				} else if(graph[i][j] == '.') {
					p_x[p] = i;
					p_y[p++] = j;
				}
			}
		}
		int ans = solve();
		if(ans != -1)
			printf("%d\n", ans);
		else
			printf("impossible\n");
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值