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.
InputOn 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.
OutputFor 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<algorithm> #include<cstring> #include<cstdio> #include<sstream> #include<vector> #include<stack> #include<deque> #include<cmath> #include<map> #define ms(x) memset(x,0,sizeof x); using namespace std; typedef long long ll; const int maxn=1e3+6; const int INF=1e9+7; int mat[maxn],n; bool vis[maxn]; struct node { int h,m; int eh,em; int a1,a2,c1,c2; int te; }a[maxn]; vector<int> to[maxn]; bool cmp1(struct node a,struct node b) {
//判断时间是否冲突 int m=a.em+abs(a.a2-b.a1)+abs(a.c2-b.c1); int h=a.eh+m/60; m%=60; if(h==b.h) return m<b.m; return h<b.h; } bool find(int x) { vis[x]=1; int i; if(!to[x].empty()) for(i=0;i<to[x].size();i++) { int y=to[x][i]; if(!mat[y]) { mat[y]=x; return 1; } else { int nxt=mat[y]; if(!vis[nxt]) { if(find(nxt)) { mat[y]=x; return 1; } } } } return 0; } int main() { int T; cin>>T; while(T--) { ms(mat); for(int i=1;i<=n;i++) to[i].clear(); scanf("%d",&n); for(int i=1;i<=n;i++) { char tim[10]; int a1,a2,c1,c2; scanf("%s %d %d %d %d",tim,&a1,&c1,&a2,&c2); a[i].a1=a1,a[i].a2=a2,a[i].c1=c1,a[i].c2=c2; a[i].h=(tim[0]-'0')*10+(tim[1]-'0'); a[i].m=(tim[3]-'0')*10+(tim[4]-'0'); int add=a[i].m+abs(a1-a2)+abs(c1-c2); a[i].em=add%60; a[i].eh=a[i].h+add/60; } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(i==j) continue; if(cmp1(a[i],a[j])) { to[i].push_back(j); } } } int ans=0; for(int i=1;i<=n;i++) { ms(vis); if(find(i)) ans++; } cout<<n-ans<<endl; } return 0; }