|
||||||||||
Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts |
Problems Submit Problem Online Status Prob.ID: |
Register Update your info Authors ranklist |
Current Contest Past Contests Scheduled Contests Award Contest |
zhongshijun Log Out Mail:0(0) Login Log Archive |
Taxi Cab Scheme
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 Sample Output 1 Source |
题意:
题目意思就是告诉你每个人的出发时间、出发地点、出发目的地,叫你求如何用最少的出租车。题目的输入格式是先给你一个开始时间,之后是出发坐标(x1,y1),然后是目的地坐标(x2,y2),则该出租车要到达目的地所需时间花费为time = |x1-x2|+|y1-y2|;
本题是一道典型的二分匹配图中的DAG的最小路径覆盖。
最小路径公式:answer = n - m(最大匹配数);
最小路径覆盖:就是在图上找尽量少的路径,使得每个节点恰好在一条路径上(换句话说,不同的路径不能有公共点)。注意,单独的节点也可以作为一条路径。
DAG最小路径覆盖的解法如下:把所有节点i拆为X结点i和Y结点i',如果图G中存在有向边i->j,则在二分图中引入边i->j'。设二分图的最大匹配数为m,则结果就是n-m;
下面给出用邻接矩阵和邻接表写得KM算法。邻接表可以快上好几倍啊!!!
邻接矩阵:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CL(x,v);memset(x,v,sizeof(x)); const int MAX = 500 + 10;
struct Point
{
int x1,y1,x2,y2;
int start,end;
}p[MAX];
int link[MAX],n;
bool graph[MAX][MAX],used[MAX]; int Find(int u)
{
for(int v= 1;v <= n;v++)
if(!used[v]&&graph[u][v])
{
used[v] = 1;
if(link[v] == -1||Find(link[v]))
{
link[v] = u;
return 1;
}
}
return 0;
} int KM()
{
int res = 0;
CL(link,-1);
for(int u = 1;u <= n;u++)
{
CL(used,0);
res += Find(u);
}
return res;
}
int main()
{
int T,i,j,HH,MM;
scanf("%d",&T);
while(T--)
{
CL(graph,0);
scanf("%d",&n);
for(i = 1;i <= n;i++)
{
scanf("%d:%d",&HH,&MM);
scanf("%d%d%d%d",&p[i].x1,&p[i].y1,&p[i].x2,&p[i].y2);
p[i].start = p[i].end = HH*60 + MM;
p[i].end += abs(p[i].x1-p[i].x2)+abs(p[i].y1-p[i].y2);
for(j = i-1;j > 0;j--)
{
int dist=abs(p[i].x1-p[j].x2)+abs(p[i].y1-p[j].y2);
if(dist+p[j].end < p[i].start)
graph[i][j] = 1;
}
}
printf("%d\n",n-KM());
}
return 0;
}
邻接表:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define CL(x,v);memset(x,v,sizeof(x)); const int MAX = 500 + 10;
struct Point
{
int x1,y1,x2,y2;
int start,end;
}p[MAX];
bool used[MAX];
int next[MAX*MAX],vex[MAX*MAX],head[MAX*MAX];
int top,n,link[MAX]; int Find(int u)
{
for(int i = head[u];i != -1;i = next[i])
{
int v = vex[i];
if(!used[v])
{
used[v] = 1;
if(link[v] == -1||Find(link[v]))
{
link[v] = u;
return 1;
}
}
}
return 0;
} int KM()
{
int res = 0;
CL(link,-1);
for(int u = 1;u <= n;u++)
{
CL(used,0);
res += Find(u);
}
return res;
}
int main()
{
int T,i,j,HH,MM;
scanf("%d",&T);
while(T--)
{
top = 0;
CL(head,-1);
scanf("%d",&n);
for(i = 1;i <= n;i++)
{
scanf("%d:%d",&HH,&MM);
p[i].start = HH*60 + MM;
scanf("%d%d%d%d",&p[i].x1,&p[i].y1,&p[i].x2,&p[i].y2);
int ntime = abs(p[i].x1-p[i].x2) + abs(p[i].y1-p[i].y2);
p[i].end = p[i].start + ntime;
for(j = i-1;j > 0;j--)
{
ntime = abs(p[i].x1-p[j].x2)+abs(p[i].y1-p[j].y2);
if(ntime+p[j].end < p[i].start)
{
next[top] = head[i];
vex[top] = j;
head[i] = top++;
}
}
}
printf("%d\n",n-KM());
}
return 0;
}