天天看點

HDU 1350 & HDU 1960 & POJ 2060 Taxi Cab Scheme【二分圖之最小路徑覆寫,經典】 Taxi Cab Scheme

Taxi Cab Scheme

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 310    Accepted Submission(s): 195

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
        

Source NWERC2004

原題連結:http://acm.hdu.edu.cn/showproblem.php?pid=1960

題意:樣例1中: 8:00的時候要把第一位乘客從(10,11)送到(9,16),花費時間為6min(即曼哈頓距離)。

這輛車可以在(9,16)等到08:07 的時候送第二名乘客到達目的地。

而樣例2中,第二名乘客08:06就要出發,而第一輛車08:06才到,是以要重新派一輛車。因為題目中說"The booked rides in each scenario are sorted in order of increasing departure time."

這道題很難想到是二分比對的最小路徑覆寫。

想到的話建圖也是關鍵。

再寫一遍公式:

最小路徑覆寫=頂點數 - 最大比對數。

具體細節看代碼吧。

AC代碼:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
using namespace std;
int n;
const int maxn=500+5;
bool a[maxn][maxn];
bool vis[maxn];
int link[maxn];
struct Node
{
    int sx,sy;//起點
    int ex,ey;//終點
    int s,e;//到達時間,離開時間
}node[maxn];
bool Find(int x)
{
    for(int i=0;i<n;i++)
    {
        if(!vis[i]&&a[x][i])
        {
            vis[i]=true;
            if(!link[i]||Find(link[i]))
            {
                link[i]=x;
                return true;
            }
        }
    }
    return false;
}
int Hungery()
{
    int ans=0;
    memset(link,0,sizeof(link));
    for(int i=0;i<n;i++)
    {
        memset(vis,false,sizeof(vis));
        if(Find(i))
            ans++;
    }
    return ans;
}
int main()
{
    int T;
    //freopen("data/1350.txt","r",stdin);
    //freopen("data/1350_me.txt","w",stdout);
    cin>>T;
    while(T--)
    {
        cin>>n;
        int h,m,sx,sy,ex,ey;
        for(int i=0;i<n;i++)
        {
            scanf("%d:%d",&h,&m);
            node[i].s=h*60+m;
            cin>>sx>>sy>>ex>>ey;
            node[i].sx=sx,node[i].sy=sy;
            node[i].ex=ex,node[i].ey=ey;
            node[i].e=node[i].s+abs(sx-ex)+abs(sy-ey);
        }
        memset(a,false,sizeof(a));
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                int t=abs(node[i].ex-node[j].sx)+abs(node[i].ey-node[j].sy);
                //第i個點的結束時間+到達第j個點的時間<第j個點的開始時間
                if(i!=j&&node[i].e+t<node[j].s)
                {
                    a[i][j]=true;
                }
            }
        }
        cout<<n-Hungery()<<endl;
    }
    return 0;
}
           

尊重原創,轉載請注明出處:http://blog.csdn.net/hurmishine