hdu-1350-最小路径覆盖(二分图)

来源:互联网 发布:网络拨号图标不见了 编辑:程序博客网 时间:2024/06/01 07:13

Taxi Cab Scheme


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
2208:00 10 11 9 1608:07 9 16 10 11208:00 10 11 9 1608:06 9 16 10 11
 

Sample Output
12
 

Source
Northwestern Europe 2004
 
题意:已知每人的出发时间,出发地点和目的地,你的任务是用尽量少的出租车送他们,使得每次出租车接客人时,至少能提前一分钟到达他所在的位置,城区是网格型的,地址用坐标(x,y)表示。出租车从(x1,y1)处到(x2,y2)处需要行驶|x1-x2|+|y1-y2|分钟。

分析:本题的模型是DAG(有向无环图)的最小路径覆盖。所谓最小路径覆盖,就是在图中找尽量少的路径,使得每个结点恰好在一条路径上(不同的路径不能有公共点)。单独的结点也可以作为一条路径。

解法:把所有结点i拆为X结点i和Y结点i',如果图G中存在有向边i->j,则在二分图中引入边i->j'。设二分图的最大匹配数为m,则结果就是n-m。

杭电好像不支持fabs,要用abs

#include<stdio.h>#include<string.h>#include<math.h>bool map[505][505],visit[505];int match[505];int M,s[505][10];bool dfs(int u){    int i;    for(i=1;i<=M;i++)    {        if(i!=u&&map[u][i]==true&&!visit[i])        {            visit[i]=true;            if(!match[i]||dfs(match[i]))            {                match[i]=u;                return true;            }        }    }    return false;}int main(){    int i,N,h,f,j,time1,ans;    scanf("%d",&N);    while(N--)    {        ans=0;        scanf("%d",&M);        memset(match,0,sizeof(match));        memset(map,0,sizeof(map));        memset(s,0,sizeof(s));        for(i=1;i<=M;i++)        {            scanf("%d:%d",&s[i][0],&s[i][1]);            s[i][6]=s[i][0]*60+s[i][1];         //初始时间            scanf("%d%d%d%d",&s[i][2],&s[i][3],&s[i][4],&s[i][5]);            s[i][7]=fabs(s[i][2]-s[i][4])+fabs(s[i][3]-s[i][5])+s[i][6];//从起点到终点的时间+初始时间        }        for(i=1;i<M;i++)        {            for(j=i+1;j<=M;j++)            {                time1=fabs(s[j][2]-s[i][4])+fabs(s[j][3]-s[i][5]);  //上一个到下一个所用的时间                if(s[i][7]+time1+1<=s[j][6])                    map[i][j]=true;            }        }        for(i=1;i<=M;i++)        {            memset(visit,0,sizeof(visit));            if(dfs(i))            ans++;        }        printf("%d\n",M-ans);    }    return 0;}


0 0
原创粉丝点击