uva 1201 最小路径覆盖 最大匹配
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 pos-sible, 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 bthat are the coordinates of the source address and two integers c dthat 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
最小路径覆盖数p = 结点数n - 最大匹配数m , 这个公式应用条件是路径不相交。
相交路径情况,这里有说明: http://www.cnblogs.com/ka200812/archive/2011/07/31/2122641.html
先拆点:如果某出租车送完客人i后可以送客人j,则连边i到j',这样转化为求二分图最小路径覆盖, 由上公式,等价于求最大匹配。
简单说明:因为拆点后路径不相交,某条路径中每个非结尾节点(即不是路径最后的那个点)都对应一条以其出发的匹配边, 所以匹配数等于非结尾节点数那么
结尾节点数 = n - 非结尾节点数 = n - 最大匹配数m.
AC代码:
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<vector> #include<cstdlib> #include<algorithm> using namespace std; #define LL long long #define ULL unsigned long long #define UINT unsigned int #define MAX_INT 0x7fffffff #define MAX_LL 0x7fffffffffffffff #define MAX(X,Y) ((X) > (Y) ? (X) : (Y)) #define MIN(X,Y) ((X) < (Y) ? (X) : (Y)) #define MAXN 555 vector<int> g[MAXN]; bool t[MAXN]; int n, lft[MAXN]; struct point{ int time, x1, y1, x2, y2; }p[MAXN]; bool ok(const point &t1, const point &t2){ int time=t1.time + (abs(t1.x1 - t1.x2) + abs(t1.y1 - t1.y2)) + (abs(t1.x2 - t2.x1) + abs(t1.y2 - t2.y1)); return time<t2.time; } bool match(int u){ for(int i=0; i<g[u].size(); i++){ int v=g[u][i]; if(!t[v]){ t[v]=true; if(lft[v]==-1 || match(lft[v])){ lft[v]=u; return true; } } } return false; } void solve(){ int i,j; for(i=1; i<=n; i++) g[i].clear(); for(i=0; i<n; i++) for(j=i+1; j<n; j++) if(ok(p[i], p[j])) g[i+1].push_back(j+1); int ans=0; memset(lft, -1, sizeof(lft)); for(i=1; i<=n; i++){ memset(t, 0, sizeof(t)); if(match(i)) ans++; } printf("%d\n", n-ans); } int main(){ // freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin); int T; scanf(" %d", &T); while(T--){ int i; int h, m, a, b, c, d; scanf(" %d", &n); for(i=0; i<n; i++){ scanf(" %d:%d %d %d %d %d", &h, &m, &a, &b, &c, &d); p[i]=(point){h*60+m, a, b, c, d}; } solve(); } return 0; }