首页 > ACM题库 > HDU-杭电 > hdu 1960 Taxi Cab Scheme-二分图-[解题报告]C++
2013
12-26

hdu 1960 Taxi Cab Scheme-二分图-[解题报告]C++

Taxi Cab Scheme

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

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

样例输出:

1
2

POJ-2060-Taxi Cab Scheme

http://poj.org/problem?id=2060

给出一些预约的起始时间,出发地和目的地,问至少需要多少车可以满足所有的预约

对于任何一对预约,如果前一预约的结束时刻加上到达下一个预约的所需时间小于下一个预约的起始时间,就在两个预约之间连一条边,题目就转化为求该图的最小路劲覆盖

最小路劲覆盖

转自 http://baike.baidu.com/view/2444809.htm

一个PXP的有向图中,路径覆盖就是在图中找一些路径,使之覆盖了图中的所有顶点,且任何一个顶点有且只有一条路径与之关联;(如果把这些路径中的每条路径从它的起始点走到它的终点,那么恰好可以经过图中的每个顶点一次且仅一次);如果不考虑图中存在回路,那么每条路径就是一个弱连通子集.

由上面可以得出:

1.一个单独的顶点是一条路径;

2.如果存在一路径p1,p2,……pk,其中p1 为起点,pk为终点,那么在覆盖图中,顶点p1,p2,……pk不再与其它的顶点之间存在有向边.

最小路径覆盖就是找出最小的路径条数,使之成为P的一个路径覆盖.

路径覆盖与二分图匹配的关系(必须是没有圈的有向图):

最小路径覆盖=|P|-其中最大匹配数的求法是把P中的每个顶点pi分成两个顶点pi’与pj”,如果在p中存在一条pi到pj的边,那么在二分图P’中就有一条连接pi’与pj”的无向边;这里pi’就是p中pi的出边,pj”就是p中pj的一条入边;

对于公式:最小路径覆盖=|P|-最大匹配数;可以这么来理解;

如果匹配数为零,那么P中不存在有向边,于是显然有:

最小路径覆盖=|P|-最大匹配数=|P|-0=|P|;即P的最小路径覆盖数为|P|;

P’中不在于匹配边时,路径覆盖数为|P|;

如果在P’中增加一条匹配边pi’-->pj”,那么在图P的路径覆盖中就存在一条由pi连接pj的边,也就是说pi与pj 在一条路径上,于是路径覆盖数就可以减少一个;

如此继续增加匹配边,每增加一条,路径覆盖数就减少一条;直到匹配边不能继续增加时,路径覆盖数也不能再减少了,此时就有了前面的公式;但是这里只 是说明了每条匹配边对应于路径覆盖中的一条路径上的一条连接两个点之间的有向边;下面来说明一个路径覆盖中的每条连接两个顶点之间的有向边对应于一条匹配 边;

与前面类似,对于路径覆盖中的每条连接两个顶点之间的每条有向边pi—>pj,我们可以在匹配图中对应做一条连接pi’与pj”的边, 显然这样做出来图的是一个匹配图(这一点用反证法很容易证明,如果得到的图不是一个匹配图,那么这个图中必定存在这样两条边pi’—pj” 及pi’ —-pk”,(j!=k),那么在路径覆盖图中就存在了两条边pi–>pj, pi—>pk ,那边从pi出发的路径就不止一条了,这与路径覆盖图是矛盾的;还有另外一种情况就是存在pi’—pj”,pk’—pj”,这种情况也类似可证);

至此,就说明了匹配边与路径覆盖图中连接两顶点之间边的一一对应关系,那么也就说明了前面的公式成立!

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
#define N 600
struct node
{
	int st,ed;
	int a,b,c,d;
}list[N*6];
int map[N][N];
int result[N];
int visit[N];
int n;
int cmp(const void *a,const void *b)
{
	return (*(struct node *)a).st-(*(struct node *)b).st;
}
int find(int a)
{
	int i;
	for(i=1;i<=n;i++)
	{
		if(!visit[i]&&map[a][i])
		{
			visit[i]=1;
			if(!result[i]||find(result[i]))
			{
				result[i]=a;
				return 1;
			}
		}
	}
	return 0;
}
int main()
{
	int t,i,j,temp;
	int hour,minute,ans;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(i=1;i<=n;i++)
		{
			scanf("%d:%d %d %d %d %d",&hour,&minute,&list[i].a,&list[i].b,&list[i].c,&list[i].d);
			list[i].st=hour*60+minute;
			list[i].ed=list[i].st+abs(list[i].a-list[i].c)+abs(list[i].b-list[i].d);
		}
		qsort(list+1,n,sizeof(list[0]),cmp);
		memset(map,0,sizeof(map));
		for(i=1;i<=n;i++)
		for(j=i+1;j<=n;j++)
		{
			temp=abs(list[i].c-list[j].a)+abs(list[i].d-list[j].b);
			if(list[i].ed+temp<list[j].st)
			map[i][j]=1;
		}
		ans=0;
		memset(result,0,sizeof(result));
		for(i=1;i<=n;i++)
		{
			memset(visit,0,sizeof(visit));
			if(find(i))
			ans++;
		}
		printf("%d\n",n-ans);
	}
	system("pause");
	return 0;
}

解题转自:http://blog.csdn.net/cambridgeacm/article/details/7904725


  1. Often We don’t set up on weblogs, but I would like to condition that this established up really forced me individually to do this! considerably outstanding publish

  2. 站长,你好!
    你创办的的网站非常好,为我们学习算法练习编程提供了一个很好的平台,我想给你提个小建议,就是要能把每道题目的难度标出来就好了,这样我们学习起来会有一个循序渐进的过程!