POJ - 2060 Taxi Cab Scheme 二分图 最小路径覆盖

题目大意:有n项任务,给出每项任务的出发时间,出发地点和目的地。

当一个任务完成之后,如果 当前时间 + 到达另一个任务的出发地的时间 <= 另一个任务的出发时间 - 1 的话,那么就可以让这个人接下去完成这个任务了

问至少需要派多少人才可以完成任务

解题思路:这题和 poj-3216 Repairing Company很像

两个点集都是任务,如果一个任务完成了可以接下来完成另一个任务的话,那么这两个任务之间就存在关系了,这样二分图就建成了

因为要把所有的任务都完成,也就是说要覆盖所有点,这就变成了二分图的最小路径覆盖了

#include<cstdio>
#include<cstring>
#include<vector>
#include<cstdlib>
using namespace std;
const int N = 510;
struct Time {
    int h, m, a, b, c, d;
}time[N];
int vis[N], link[N], n;
vector<int> g[N];
bool judge(int i, int j) {
    int t = abs(time[i].a - time[i].c) + abs(time[i].b - time[i].d) + abs(time[j].a - time[i].c) + abs(time[j].b - time[i].d);

    if(time[j].h * 60 + time[j].m - (time[i].h * 60 + time[i].m + t)  >= 1)
        return true;
    return false;
}

bool dfs(int u) {
    for(int i = 0; i < g[u].size(); i++) {
        int v = g[u][i];
        if(vis[v])
            continue;
        vis[v] = 1;
        if(link[v] == -1 || dfs(link[v])) {
            link[v] = u;
            return true;
        }
    }
    return false;
}

void hungary() {
    int ans = 0;
    for(int i = 0; i < n; i++) {
        memset(vis, 0, sizeof(vis));
        if(dfs(i))
            ans++;
    }
    printf("%d\n", n - ans);
}

void init() {

    scanf("%d", &n);
    for(int i = 0; i < n; i++) {
        scanf("%d:%d%d%d%d%d",&time[i].h, &time[i].m, &time[i].a, &time[i].b, &time[i].c, &time[i].d);
    }

    for(int i = 0; i < n; i++) {
        g[i].clear();
        for(int j = 0; j < n; j++)  {
            if(i != j && judge(i,j)) {
                g[i].push_back(j);
            }
        }
    }
    memset(link, -1, sizeof(link));
}

int main() {
    int test;
    scanf("%d", &test);
    while(test--) {
        init();
        hungary();
    }
    return 0;
}
时间: 2024-08-26 03:05:00

POJ - 2060 Taxi Cab Scheme 二分图 最小路径覆盖的相关文章

POJ 2060 Taxi Cab Scheme【最小路径覆盖】

T - Taxi Cab Scheme Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2060 Appoint description:  System Crawler  (2014-08-22) Description Running a taxi station is not all that simple. Apart from

【HDU1960】Taxi Cab Scheme(最小路径覆盖)

Taxi Cab Scheme Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 231    Accepted Submission(s): 142 Problem Description Running a taxi station is not all that simple. Apart from the obvious deman

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

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 ta

poj 2060 Taxi Cab Scheme (二分匹配)

Taxi Cab Scheme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5710   Accepted: 2393 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

poj 2060 Taxi Cab Scheme 最小路径覆盖

//二分匹配的最小路径覆盖 //对于第i次ride,如果在第i次ride结束后还能在第j次ride出发前赶到第j次的出发点 //那么i到j就有一条边 //根据最小路径覆盖 = N - 最大匹配即可得到答案 #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> using namespace std; const int maxn  = 510; int line[max

poj 2060 Taxi Cab Scheme(DAG图的最小路径覆盖)

题意: 出租车公司有M个订单. 订单格式:     hh:mm  a  b  c  d 含义:在hh:mm这个时刻客人将从(a,b)这个位置出发,他(她)要去(c,d)这个位置. 规定1:从(a,b)到(c,d)所花的时间为:abs(a-c)+abs(b-d). 规定2:一辆出租车如果要接单,必须在客人出发前1分钟(包括)以前接单. 问,最少派出多少辆出租车,可以完成所有任务. 思路: 把每一笔单看成一个点,如果完成第i个单后可以接到第j个单,则 i->j连上线. 则题为:求这个DAG图的最小路

Taxi Cab Scheme POJ - 2060 二分图最小路径覆盖

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 whi

poj 3020 二分图最小路径覆盖

二分图最小路径覆盖=|v|-最大匹配.此题为有向图,切所有边正反向存了两遍,所以结果匹配数要除以2 // // main.cpp // poj3020 // // Created by Fangpin on 15/5/29. // Copyright (c) 2015年 FangPin. All rights reserved. // #include <iostream> #include <cstdio> #include <vector> #include <

POJ3216 Repairing Company【二分图最小路径覆盖】【Floyd】

题目链接: http://poj.org/problem?id=3216 题目大意: 有Q个地点,告诉你Q个地点之间的相互距离(从i地点赶到j地点需要的时间).有M项任务, 给你M项任务所在的地点block.开始时间start和任务完成需要时间time.一个工人只有在 他准备完成的下一项任务开始之前完成手上的任务,然后在下一项任务开始之前赶到下一项 任务的地点,才能完成这两项任务.问:最少需要多少个工人来完成这M项任务. 思路: 先用Floyd算出Q个地点之间相互最短距离.然后建立一个二分图,每