HDU 1350(最小路径覆盖)

Taxi Cab Scheme

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 87    Accepted Submission(s): 46

Problem 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 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

#include <iostream> #include <vector> #include <math.h> #include <algorithm> using namespace std; #define MAX 1005 struct node { int time; int x1,y1,x2,y2; int sum; }; vector< vector<int> > map; int mark[MAX]; bool flag[MAX]; int nm,num; node elem[MAX]; bool cmp(node a,node b) { return a.time < b.time; } int len_time(int x1,int y1,int x2,int y2) { return abs(x1 - x2) + abs(y1 - y2); } bool dfs(int pos) //搜pos点是否存在增广路 { int i,pre,tp; int len=map[pos].size(); for(i=0;i<len;i++) { tp=map[pos][i]; if(!flag[tp]) { flag[tp]=true; pre=mark[tp]; mark[tp]=pos; if(pre==-1 || dfs(pre)) return true; //如果没被访问过或者存在增广路,pos到该点就存在增广路 mark[tp]=pre; //否则pos到该点就不存在增广路 } } return false; } int main() { int n,t,i,j; char str[10]; scanf("%d",&t); while (t--) { scanf("%d",&n); num=0; nm=n*2; map.clear(); map.resize(nm+10); memset(mark,-1,sizeof(mark)); for(i = 1;i <= n;i ++) { scanf("%s",str); scanf("%d%d%d%d",&elem[i].x1,&elem[i].y1,&elem[i].x2,&elem[i].y2); elem[i].time = (str[0] - '0')*10*60 + (str[1] - '0')*60 + (str[3] - '0')*10 + (str[4] - '0'); elem[i].sum = len_time(elem[i].x1,elem[i].y1,elem[i].x2,elem[i].y2); } sort(elem+1,elem+n+1,cmp); for (i = 1;i < n;i ++) { for (j = i+1;j <= n;j ++) { if(elem[j].time > (elem[i].time + elem[i].sum + len_time(elem[i].x2,elem[i].y2,elem[j].x1,elem[j].y1))) { map[i].push_back(j+n); } } } for (i=1;i<=n;i++) { memset(flag,0,sizeof(flag)); if(dfs(i)) num++; } printf("%d/n",n-num); } return 0; }

你可能感兴趣的:(struct,vector,Integer,input,each,output)