POJ 2060(最小路径覆盖)

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

题目大意:在一座城市中,有n个乘客,他们都有滴滴的需求,然后,如果一辆滴滴可以提前一分钟到达乘客所在位置,那么就表示可以载这位乘客,然后问我们至少需要多少辆车才能让所有乘客都满足需求。

解题思路:如果我们载完一位乘客u之后可以去载另一位乘客v,那么我们就建一条u到v的边,然后问题就转换为求这个图中的最小路径覆盖,也就是需要多少条路径能够覆盖所有的点。
代码:

#pragma GCC optimize(2)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <bitset>
#include <queue>
//#include <random>
#include <time.h>
using namespace std;
//#define int long long
#define ull unsigned long long
#define ls root<<1
#define rs root<<1|1
const int maxn = 5e5 + 7;
//std::mt19937 rnd(time(NULL));
struct edge
{
    int v,next;
}e[maxn];
int match[510],head[510],cnt,used[510];
void add(int a,int b)
{
    e[++cnt]=edge{b,head[a]};
    head[a]=cnt;
}
bool dfs(int u)
{
    for(int i=head[u];i;i=e[i].next){
        int v=e[i].v;
        if(used[v])continue;
        used[v]=1;
        if(!match[v] || dfs(match[v])){
            match[v]=u;
            return true;
        }
    }
    return false;
}
//O(nm) n 510  m 2040  t 100
//
struct Node
{
	int t,sx,sy,ex,ey;
}node[510];
signed main()
{
    int t;
    scanf("%d",&t);
    while(t--){
    	int n;
    	scanf("%d",&n);
    	memset(head,0,sizeof head);
    	memset(match,0,sizeof match);
    	cnt=0;
    	for(int i=1;i<=n;i++){
    		int a,b,c,d,e,f;
    		scanf("%d:%d%d%d%d%d",&a,&b,&c,&d,&e,&f);
    		node[i]=Node{a*60+b,c,d,e,f};
		}
//		for(int i=1;i<=n;i++){
//			cout<<node[i].t<<' '<<node[i].sx<<' '<<node[i].sy<<' '<<node[i].ex<<' '<<node[i].ey<<endl;
//		}
		for(int i=1;i<=n;i++){//起点 
			for(int j=1;j<=n;j++){//终点 
				if(i==j)continue;
				int tmp=node[i].t+abs(node[i].ex-node[i].sx)+abs(node[i].ey-node[i].sy)+1+abs(node[j].sx-node[i].ex)+abs(node[j].sy-node[i].ey);
				//cout<<tmp<<' '<<node[j].t<<' '<<node[i].t<<endl;
				//cout<<abs(node[i].ex-node[i].sx)<<' '<<abs(node[i].ey-node[i].sy)<<' '<<abs(node[j].sx-node[i].ex)<<' '<<abs(node[j].sy-node[i].ey)<<endl;
				if(tmp<=node[j].t){
					add(i,j);
					//cout<<i<<' '<<j<<endl;
				}
			}
		}
		int ans=0;
		for(int i=1;i<=n;i++){
			memset(used,0,sizeof used);
			if(dfs(i))ans++;
		}
		//cout<<ans<<endl;
		printf("%d\n",n-ans);
	}
}

坑边闲话:写这道题的时候不知道脑子怎么了,写错了一个变量,然后就debug,这道题是之前暑假的时候培训做过的题,这次又做一遍感触还是不一样,之前对于二分图就没有深入学习,现在可能要重新看一遍了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值