1. 程式人生 > >POJ 2060 最小路徑覆蓋

POJ 2060 最小路徑覆蓋

題目:
Taxi Cab Scheme
Time Limit: 1000MSMemory Limit: 30000K
Total Submissions: 4432Accepted: 1868
Description


Running a taxi station is not all that simple. Apart from the obvious demand for a centralised coordination of the cabs in order to pick up the customers calling to get a cab as soon as possible,there is also a need to schedule all the taxi rides which have been booked in advance.Given a list of all booked taxi rides for the next day, you want to minimise the number of cabs needed to carry out all of the rides. 
For the sake of simplicity, we model a city as a rectangular grid. An address in the city is denoted by two integers: the street and avenue number. The time needed to get from the address a, b to c, d by taxi is |a - c| + |b - d| minutes. A cab may carry out a booked ride if it is its first ride of the day, or if it can get to the source address of the new ride from its latest,at least one minute before the new ride's scheduled departure. Note that some rides may end after midnight.
Input


On the first line of the input is a single positive integer N, telling the number of test scenarios to follow. Each scenario begins with a line containing an integer M, 0 < M < 500, being the number of booked taxi rides. The following M lines contain the rides. Each ride is described by a departure time on the format hh:mm (ranging from 00:00 to 23:59), two integers a b that are the coordinates of the source address and two integers c d that are the coordinates of the destination address. All coordinates are at least 0 and strictly smaller than 200. The booked rides in each scenario are sorted in order of increasing departure time.
Output


For each scenario, output one line containing the minimum number of cabs required to carry out all the booked taxi rides.
Sample Input


2
2
08:00 10 11 9 16
08:07 9 16 10 11
2
08:00 10 11 9 16
08:06 9 16 10 11
Sample Output


1
2
Source


Northwestern Europe 2004


題意:


有一些計程車,分別要從一個地方去往另一個地方載客,從一個地方駛向另一個地方的時間為這兩個地方的行座標之差的絕對值+列座標之差的絕對值,如果一個計程車從一個地方達到另一個地方然後駛向另一個起點時間足夠的話,可以取代另一個計程車,這樣就可以少用一個計程車,題目求的是最多可以少用幾輛計程車。
給定測試資料組數,每組給定出租車數量,然後是沒個計程車的出發時間,起點座標,終點座標。


原始碼:


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;


struct texi
{
int s_time;
int e_time;
int br;
int bc;
int er;
int ec;
};


int n;
texi tx[600];
int a[600][600];
int a1[600];
int a2[600];
bool fl[600];


int inline Abs(int a)
{
return a>0?a:-a;
}


bool search(int v)
{
for(int i=1;i<=n;i++)
{
if(a[v][i]&&!fl[i])
{
fl[i]=1;
if(a2[i]==-1||search(a2[i]))
{
a2[i]=v;
a1[v]=i;
return 1;
}
}
}
return 0;
}


int maxmatch()
{
memset(a1,-1,sizeof(a1));
memset(a2,-1,sizeof(a2));
int ret=0;
for(int i=1;i<=n;i++)
{
if(a1[i]==-1)
{
memset(fl,0,sizeof(fl));
if(search(i))
{
ret++;
}
}
}
return ret;
}


int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
int h,m;
scanf("%d:%d%d%d%d%d",&h,&m,&tx[i].br,&tx[i].bc,&tx[i].er,&tx[i].ec);
int tim=Abs(tx[i].br-tx[i].er)+Abs(tx[i].bc-tx[i].ec);
tx[i].s_time=h*60+m;
tx[i].e_time=tx[i].s_time+tim;
}
memset(a,0,sizeof(a));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(j==i)continue;
if(tx[j].s_time>tx[i].e_time+Abs(tx[i].er-tx[j].br)+Abs(tx[i].ec-tx[j].bc))
{
a[i][j]=1;
}
}
}
/*
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
printf("%d ",a[i][j]);
}printf("\n");
}
*/
int res=maxmatch();
printf("%d\n",n-res);
}
return 0;
}