POJ-2060-Taxi Cab Scheme

Source: Internet
Author: User

POJ-2060-Taxi Cab Scheme

Http://poj.org/problem? Id = 2060

Provide the starting time, departure location, and destination of the reservation, and ask how many vehicles are required to meet all the reservation requirements.

For any one-to-one reservation, if the end time of the previous reservation plus the time required to arrive at the next reservation is less than the start time of the next reservation, an edge is connected between the two appointments, the question is converted to the minimum path overhead of the graph.

Least Effort coverage

From http://baike.baidu.com/view/2444809.htm

In a PXP directed graph, path overwrite is to find some paths in the graph to overwrite all vertices in the graph, and any vertex has only one path associated with it; (If each path in these paths is taken from its start point to its end point, it can go through each vertex in the graph once and only once); if there is a loop in the graph, each path is a subset of weak connections.

We can conclude from the above:

1. A separate vertex is a path;

2. if there is a path p1, p2 ,...... pk, where p1 is the start point and pk is the end point. In the overwrite graph, the vertices p1, p2 ,...... pk no longer has a directed edge with other vertices.

The minimum path overwrite is to find the minimum number of paths to make it a path overwrite of P.

The relationship between path coverage and bipartite graphs (must be a directed graph without loops ):

Minimum path overwrite = | P |-the method for finding the maximum number of matches is to divide each vertex pi in P into two vertex pi 'and pj '', if there is a pi-to-pj edge in p, then in the Bipartite Graph P, there is a undirected edge connecting pi 'and pj; here pi 'is the outbound edge of pi in p, and pj ''is an inbound edge of pj in p;

For the formula: Minimum path overwrite = | P |-Maximum number of matches, this can be understood;

If the number of matches is zero, there is no directed edge in P, so obviously there are:

Minimum path overwrite = | P |-Maximum number of matches = | P |-0 = | P |; that is, the minimum path overwrite count of P |;

The number of path overwrites when the P 'does not match the edge | P |;

If a matched edge pi '--> pj ''is added to P, an edge connecting pj by pi exists in the path overwrite of P, that is to say, pi and pj are in one path, so the number of path overwrites can be reduced;

As a result, the number of path overwrites is reduced for each added matching edge. The number of path overwrites cannot be reduced until the matching edge cannot be increased, in this case, the previous formula is available. However, it only shows that each matching edge corresponds to a directed edge connecting two points on one path in the path overwrite; next, a directed edge connecting two vertices in a path overwrite corresponds to a matching edge;

Similar to the above, for each directed edge pi connecting two vertices in the path overwrite ---> pj, we can create an edge connecting pi 'and pj ''in the matching graph. Obviously, this graph is a matching graph (this is easily proved by the reverse verification method, if the obtained graph is not a matching graph, the two sides pi '--- pj ''and pi' ---- pk'' must exist in this graph. (j! = K), there will be two sides in the path overwrite graph --> pj, pi ---> pk, and there will be more than one path starting from pi, this is in conflict with the path overwrite diagram. Another situation is that there is pi '--- pj '', pk' --- pj'', which is similar to verifiable );

At this point, we have explained the one-to-one correspondence between the matching edge and the edge of the two vertices in the path overlay diagram. This also shows that the previous formula is true!

#include<iostream>#include<cstring>#include<cstdio>#include<cmath>using namespace std;#define N 600struct 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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.