Taxi Cab Scheme(最小路径覆盖)

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
题目大意:出租车司机必须在送完一个乘客后才能接下一个乘客,问最少要多少个出租车。首先就求最大匹配,用总数n-最大匹配=最小路径覆盖。在符合的时间范围的两个点建立一条单向边。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<math.h>
using namespace std;
const int sz=2*1e6+7;
typedef long long ll;
const int INF=0x3f3f3f3f;
const ll mod=1e9+7;
map<int,int>mp;
char maps[700][700];
int vis[700],f[700],sum[700][700];
int dir[4][2]={1,0,0,-1,-1,0,0,1};
int cnt,n;
struct node
{
    int h,m,x1,y1,x2,y2,sum;
}pos[700];
vector<int>v[700];
bool cmp(node a,node b)
{
    return a.sum<b.sum;
}
int Find(int x)
{
    int i,y;
    for(i=0;i<v[x].size();i++)
    {
        y=v[x][i];
        if(!vis[y])
        {
            vis[y]=1;
            if(f[y]==-1||Find(f[y]))
            {
                f[y]=x;
                return 1;
            }
        }
    }
    return 0;
}
int Match()
{
    int i,ans=0;
    memset(f,-1,sizeof(f));
    for(i=0;i<n;i++)
    {
        memset(vis,0,sizeof(vis));
        ans+=Find(i);
    }
    return ans;
}
int judge(int x,int y)
{
    int sum;
    sum=pos[x].sum+abs(pos[x].x1-pos[x].x2)+abs(pos[x].y1-pos[x].y2)+abs(pos[x].x2-pos[y].x1)+abs(pos[x].y2-pos[y].y1);
    if(sum<pos[y].sum)
        return 1;
    return 0;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        int i,j;
        for(i=0;i<610;i++)
            v[i].clear();
        for(i=0;i<n;i++)
            {
                scanf("%d:%d %d %d %d %d",&pos[i].h,&pos[i].m,&pos[i].x1,&pos[i].y1,&pos[i].x2,&pos[i].y2);
                pos[i].sum=pos[i].h*60+pos[i].m;

            }
        sort(pos,pos+n,cmp);
        for(i=0;i<n;i++)
            for(j=i+1;j<n;j++)
                if(judge(i,j))
                    v[i].push_back(j);
        printf("%d\n",n-Match());
    }
    return 0;
}

#include<stdio.h>
#include<string.h>
#include<bitset>
#include<string>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<vector>
using namespace std;
typedef long long ll;
const int mod=1000000007;
const ll Max=9999999999;
int dp[25600],date[110];
stack<int>ss;
set<int>s;
map<int,int>mp;
vector<int>v[400000];
int vis[400000],f[400000],cnt;
int dir[4][2]={1,0,-1,0,0,-1,0,1};
char maps[700][700];
int head[700];
struct node
{
    int v;
    int next;
}edge[300000];
struct aa
{
    int x1,y1,x2,y2,time;
}sum[600];
bool cmp(aa a,aa b)
{
    return a.time<b.time;
}
int Find(int x)
{
    int i,y;
    for(i=head[x];i!=-1;i=edge[i].next)
    {
        y=edge[i].v;
        if(!vis[y])
        {
            vis[y]=1;
            if(f[y]==0||Find(f[y]))
            {
                f[y]=x;
                return 1;
            }
        }
    }
    return 0;
}
int match(int n)
{
    memset(f,0,sizeof(f));
    int i,ans=0;
    for(i=1;i<=n;i++)
    {
        memset(vis,0,sizeof(vis));
        ans+=Find(i);
    }
    return ans;
}
int judge(int i,int j)
{
    int total;
    total=sum[i].time+abs(sum[i].x1-sum[i].x2)+abs(sum[i].y1-sum[i].y2)+abs(sum[i].x2-sum[j].x1)+abs(sum[i].y2-sum[j].y1);
    if(total<sum[j].time)
        return 1;
    return 0;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,i,j;
        cnt=0;
        memset(head,-1,sizeof(head));
        scanf("%d",&n);
        int h,m;
        for(i=1;i<=n;i++)
        {
            scanf("%d:%d %d %d %d %d",&h,&m,&sum[i].x1,&sum[i].y1,&sum[i].x2,&sum[i].y2);
            sum[i].time=h*60+m;
        }
        sort(sum+1,sum+1+n,cmp);
        for(i=1;i<n;i++)
        {
            for(j=i+1;j<=n;j++)
            {
                if(judge(i,j))
                {
                    edge[cnt].v=i;
                    edge[cnt].next=head[j];
                    head[j]=cnt++;
                }
            }
        }
        int car=match(n);
        printf("%d\n",n-car);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值