码迷,mamicode.com
首页 > 其他好文 > 详细

poj2060——Taxi Cab Scheme(最小路径覆盖)

时间:2016-05-07 09:25:40      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

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

题意是说有n个出车安排,一辆车能接到这个安排的条件是:1、这辆车第一次发车;2、这辆车接了上一个安排,回到这个安排的起点的时间正好是这个安排的前一分钟或者更早
每一次安排有五个输入数据,第一个是发车时间,2、3是起点位置,4、5是终点位置,因此计算每两个安排之间的时间差可以用第一个的最后两个数和第二个的第二和三个数。我一开始就是这里没明白才不知道怎么算两个安排之间的关系
接下来就是用二分图,把每个安排都放在二分图的两个点集上,显然两个相同的任务之间不会有边,只有符合题意的两个不同的任务可以连一条边

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <vector>
#include <iostream>
#include <set>
#include <cstring>
#include <string>
#define MAXN 510
#define inf 0xffffffff
using namespace std;
struct Node
{
    int a,b,c,d;
    int begin;
};
Node s[MAXN];
int n;
int get_map(int i,int j)
{
    if(i==j)
        return 0;
    int tmp=abs(s[i].a-s[i].c)+abs(s[i].b-s[i].d)+s[i].begin;
    tmp+=abs(s[i].c-s[j].a)+abs(s[i].d-s[j].b);
    if(tmp<s[j].begin)
        return 1;
    else
        return 0;
}
bool vis[MAXN];
int map[MAXN][MAXN],pre[MAXN];  //匹配路径;
int find(int cur)
{
    for(int i=1; i<=n; ++i)
    {
        if(!vis[i]&&map[cur][i])
        {
            vis[i] = true;
            if(pre[i] == 0 || find(pre[i]))
            {
                pre[i] = cur;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int t,h,m;
    scanf("%d",&t);
    while(t--)
    {
        memset(map,0,sizeof(map));
        scanf("%d",&n);
        for(int i=1; i<=n; ++i)
        {
            scanf("%d:%d",&h,&m);
            s[i].begin=h*60+m;
            scanf("%d%d%d%d",&s[i].a,&s[i].b,&s[i].c,&s[i].d);
        }
        for(int i=1; i<=n; ++i)
            for(int j=1; j<=n; ++j)
                map[i][j]=get_map(i,j);
        memset(pre,0,sizeof(pre));
        int num=0;
        for(int i=1; i<=n; ++i)
        {
            memset(vis,0,sizeof(vis));
            if(find(i))
                num++;
        }
        printf("%d\n",n-num);
    }
    return 0;
}

poj2060——Taxi Cab Scheme(最小路径覆盖)

标签:

原文地址:http://blog.csdn.net/blue_skyrim/article/details/51331383

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!