Description
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
Output
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
这道题是一个最小路径覆盖:我们把满足条件的点进行建图:先求出最大二分匹配,n-最大二分匹配就是答案
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
#define mx 1000
struct node
{
int time;
int cost;
int x[2],y[2];
}ff[mx];
int ma[mx][mx];
bool use[mx];
int d[mx];
int mm;
int n;
int fin(int id)
{
int i;
for(i=1;i<=n;i++)
{
if(ma[id][i]==1&&use[i]==false)
{
use[i]=true;
if(fin(d[i])||d[i]==0)
{
d[i]=id;
return 1;
}
}
}
return 0;
}
int add(int x1,int y1,int x2,int y2)
{
int dd=abs(x1-x2)+abs(y1-y2);
return dd;
}
int main()
{
int i,j;
int t, a,b;
int k;
int mid[2];
int hh;
while(scanf("%d",&k)!=EOF)
{
for(hh=1;hh<=k;hh++)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
{
t=0;
scanf("%d:%d %d %d %d %d",&a,&b,&ff[i].x[0],&ff[i].y[0],&ff[i].x[1],&ff[i].y[1]);
t=a*60+b;
ff[i].time=t; //将时间转化为分钟,这样方便比较
mid[0]=abs(ff[i].x[0]-ff[i].x[1]);
mid[1]=abs(ff[i].y[0]-ff[i].y[1]);
ff[i].cost=mid[0]+mid[1];
}
//建图
memset(ma,0,sizeof(ma));
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if( ff[i].time + ff[i].cost + add(ff[i].x[1],ff[i].y[1],ff[j].x[0],ff[j].y[0]) <ff[j].time) //这里要注意,不光要计算从起点到终点的时间,还要加上从当前终点到下一个起点的时间,我就是错在这里::
{
ma[i][j]=1;
}
}
}
int cnt=0;
memset(d,0,sizeof(d));
for(i=1;i<=n;i++)
{
memset(use,false,sizeof(use));
// mm=ff[i].time+ff[i].cost;
// printf("%d****\n",mm);
if(fin(i))
cnt++;
}
printf("%d\n",n-cnt);
}
}
}