UVALive - 3126 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.
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

题意:输入n个乘客的起始出发时间,起始地点和终点,每次接一个乘客必须提前一分钟到达,问最少需要多少出租车.

思路:这是一个DAG图,即有向无环二分图最小路径覆盖问题,如果送完这个乘客可以接到另一个乘客则两者建边,

建边方法参板子文章.

代码:

#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 1e5+5;
const double esp = 1e-7;
const int ff = 0x3f3f3f3f;
map<int,int>::iterator it;

struct node
{
	int st,et;
} c[520];

int n;
int mp[520][520];
int coor[520][5];//记录起点终点 
int vis[520],g[520];

bool cmp(node a,node b)
{
	return a.st< b.st;
}

int find(int x)
{
	for(int i = 1;i<= n;i++)
	{
		if(mp[x][i]&&!vis[i])
		{
			vis[i] = 1;
			if(!g[i]||find(g[i]))
			{
				g[i] = x;
				return 1;	
			}	
		}	
	}
	
	return 0;
}

void init()
{
	mem(mp,0);
	mem(g,0);
}

int main()
{
	int t;
	cin>>t;
	
	while(t--)
	{
		init();	
		scanf("%d",&n);
		
		for(int i = 1;i<= n;i++)
		{
			int s,e,sx,sy,ex,ey,o,m;	
			scanf("%d:%d %d %d %d %d",&o,&m,&sx,&sy,&ex,&ey);
			
			s = o*60+m;//计算起始时间 
			e = s+abs(sx-ex)+abs(sy-ey);//终止时间 
			c[i].st = s;
			c[i].et = e;
			coor[i][1] = sx;
			coor[i][2] = sy;
			coor[i][3] = ex;
			coor[i][4] = ey;
		}	
		
	
//		sort(c+1,c+n+1,cmp);  排序之后就会wa,感觉排个序也没什么呀, 难道是快排不稳定造成的?即使不稳定也没什么影响嘛 
		
		for(int i = 1;i<= n;i++)
		{
			for(int j = i+1;j<= n;j++)
			{
				if(abs(coor[i][3]-coor[j][1])+abs(coor[i][4] - coor[j][2])+c[i].et<= c[j].st-1)
				{
					mp[i][j] = 1;
				}
			}		
		}
		
		int sum = 0;
		for(int i = 1;i<= n;i++)
		{
			mem(vis,0);
			if(find(i))
				sum++;
		}
		
		printf("%d\n",n-sum);
	}
	
	return 0;	
}

猜你喜欢

转载自blog.csdn.net/nka_kun/article/details/79914022
今日推荐