1. 程式人生 > >Poj-2060 Taxi Cab Scheme 二分圖最小路徑覆蓋

Poj-2060 Taxi Cab Scheme 二分圖最小路徑覆蓋

題目連結

計程車公司有n個預約, 每個預約有時間和地點, 地點分佈在二維整數座標系上, 地點之間的行駛時間為兩點間的曼哈頓距離(|x1 - x2| + |y1 - y2|)。一輛車可以在運完一個乘客後運另一個乘客, 條件是此車要在預約開始前一分鐘之前到達出發地, 問最少需要幾輛車搞定所有預約。



#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 505;
const int Mod = 1000000007;
const double inf = 1<<30;
int n;
int map[maxn][maxn];
struct node
{
	int sta,t;
	int sx,sy,ex,ey;
}book[maxn];
int cx[maxn],cy[maxn];
bool vis[maxn];
bool FindPath( int u )
{
	for( int i = u+1; i <= n; i ++ )
	{
		if( !vis[i] && map[u][i] )
		{
			vis[i] = true;
			if( cy[i] == -1 || FindPath( cy[i] ) )
			{
				cy[i] = u;
				cx[u] = i;
				return true;
			}
		}
	}
	return false;
}
int MaxMatch()
{
	int ans = 0;
	memset( cx,-1,sizeof(cx) );
	memset( cy,-1,sizeof(cy) );
	for( int i = 1; i <= n; i++ )
	{
		if( cx[i] == -1 )
		{
			memset( vis,0,sizeof(vis) );
			ans += FindPath( i );
		}
	}
	return ans;
}
int main()
{
	#ifndef ONLINE_JUDGE   
	freopen("data.txt","r",stdin);   
	#endif
	int cas,h,m;
	scanf("%d",&cas);
	while( cas -- )
	{
		scanf("%d",&n);
		memset( map,0,sizeof(map) );
		for( int i = 1; i <= n; i ++ )
		{
			scanf("%d:%d%d%d%d%d",&h,&m,&book[i].sx,&book[i].sy,&book[i].ex,&book[i].ey);
			book[i].sta = h*60 + m;
			book[i].t = abs( book[i].sx - book[i].ex ) + abs( book[i].sy - book[i].ey );
		}
		for( int i = 1; i <= n; i ++ )
		{
			for( int j = i+1; j <= n; j ++ )
			{
				int dis = abs( book[j].sx - book[i].ex ) + abs( book[j].sy - book[i].ey );
				if( book[i].t + dis + 1 <= book[j].sta - book[i].sta )
					map[i][j] = 1;
			}
		}
		printf("%d\n",n - MaxMatch());
	}
	return 0;
}