UVALive - 3126 Taxi Cab Scheme (Bipartite Graph Minimum Path Coverage)

    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

The meaning of the question: Enter the starting departure time, starting point and ending point of n passengers. Each time you pick up a passenger, you must arrive one minute earlier, and ask how many taxis are required at least.

Idea: This is a DAG graph, that is, the minimum path coverage problem of a directed acyclic bipartite graph. If this passenger can be picked up by another passenger after delivery, the two sides will be established.

For the method of edge building, see the board article.

Code:

#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];//Record the start and end points
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])
		{
			show [i] = 1;
			if(!g[i]||find(g[i]))
			{
				g[i] = x;
				return 1;	
			}	
		}	
	}
	
	return 0;
}

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

intmain()
{
	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;//Calculate the start time
			e = s+abs(sx-ex)+abs(sy-ey);//termination time
			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); After sorting, it will be wa, I feel that sorting is nothing, is it caused by the instability of quicksort? It doesn't matter if it's unstable
		
		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;	
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325902962&siteId=291194637