POJ 2060 最小路徑覆蓋 二分圖

  這題是一道簡單的最小路徑覆蓋題目,最小路徑覆蓋的數目等於點的數目減去最大匹配數。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#define maxn 505
#define mem(a) memset(a, 0, sizeof(a))
using namespace std;

bool map[maxn][maxn], vis[maxn];
int lk[maxn], num;

struct point
{
	int st, sx, sy, ex, ey, dis;
}po[maxn];

bool dfs(int a)
{
	int i;
	for(i = 0;i < num;i++)
	{
		if(!vis[i]&&map[a][i])
		{
			vis[i] = 1;
			if(lk[i] == -1||dfs(lk[i]))
			{
				lk[i] = a;
				return true;
			}
		}
	}
	return false;
}

int res()
{
	int i;
	int ress = 0;
	memset(lk, -1, sizeof(lk));
	for(i = 0;i < num;i++)
	{
		mem(vis);
        if(dfs(i))
        ress++;
	}
	return ress;
}

int main(int argc, char *argv[])
{
    int cas, i, j, h, m, tmp, ans;
    scanf("%d", &cas);
    while(cas--)
    {
    	ans = 0;
    	mem(map);
    	mem(po);
    	scanf("%d", &num);
    	for(i = 0;i < num;i++)
    	{
	    	scanf("%d:%d%d%d%d%d", &h, &m, &po[i].sx, &po[i].sy, &po[i].ex, &po[i].ey);
	    	po[i].st = h * 60 + m;
	    	po[i].dis = abs(po[i].sx - po[i].ex) + abs(po[i].sy - po[i].ey);
	    }
	    for(i = 0;i < (num - 1);i++)
	    {
    		for(j = (i + 1);j < num;j++)
    		{
    			tmp = abs(po[i].ex - po[j].sx) + abs(po[i].ey - po[j].sy);
		    	if((po[i].st + po[i].dis + tmp) < po[j].st)
		    	map[i][j] = 1;
		    }
    	}
    	ans = res();
    	printf("%d\n", num - ans);
    }
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章