UVALive3126 Taxi Cab Scheme —— 最小路径覆盖

题目链接:https://vjudge.net/problem/UVALive-3126

题解:

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <string>
 6 #include <vector>
 7 #include <map>
 8 #include <set>
 9 #include <queue>
10 #include <sstream>
11 #include <algorithm>
12 using namespace std;
13 const int INF = 2e9;
14 const int MOD = 1e9+7;
15 const int MAXN = 1e3+10;
16
17 int n;
18 int M[MAXN][MAXN], link[MAXN];
19 bool vis[MAXN];
20
21 struct Node
22 {
23     int time[2], sor[2], des[2];
24 }a[MAXN];
25
26 bool dfs(int u)
27 {
28     for(int i = 1; i<=n; i++)
29     if(M[u][i] && !vis[i])
30     {
31         vis[i] = true;
32         if(link[i]==-1 || dfs(link[i]))
33         {
34             link[i] = u;
35             return true;
36         }
37     }
38     return false;
39 }
40
41 int hungary()
42 {
43     int ret = 0;
44     memset(link, -1, sizeof(link));
45     for(int i = 1; i<=n; i++)
46     {
47         memset(vis, 0, sizeof(vis));
48         if(dfs(i)) ret++;
49     }
50     return ret;
51 }
52
53 bool judge(Node x, Node y)
54 {
55     int t1 = x.time[0]*60+x.time[1];
56     int t2 = y.time[0]*60+y.time[1];
57     int dis1 = abs(x.des[0]-x.sor[0]) + abs(x.des[1]-x.sor[1]);
58     int dis2 = abs(y.sor[0]-x.des[0]) + abs(y.sor[1]-x.des[1]);
59     return (t1+dis1+dis2+1<=t2);
60 }
61
62 int main()
63 {
64     int T;
65     scanf("%d", &T);
66     while(T--)
67     {
68         scanf("%d", &n);
69         for(int i = 1; i<=n; i++)
70         {
71             scanf("%d:%d", &a[i].time[0], &a[i].time[1]);
72             scanf("%d%d%d%d", &a[i].sor[0], &a[i].sor[1], &a[i].des[0], &a[i].des[1]);
73         }
74
75         memset(M, 0, sizeof(M));
76         for(int i = 1; i<=n; i++)
77         for(int j = 1; j<=n; j++)
78             if(i!=j && judge(a[i], a[j]))
79                 M[i][j] = 1;
80
81         int cnt = hungary();
82         printf("%d\n", n-cnt);
83     }
84 }

时间: 2024-08-29 10:17:54

UVALive3126 Taxi Cab Scheme —— 最小路径覆盖的相关文章

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

hdu1350Taxi Cab Scheme (最小路径覆盖)

Taxi Cab Scheme Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 712 Accepted Submission(s): 337 Problem Description Running a taxi station is not all that simple. Apart from the obvious demand f

UVAlive3126 Taxi Cab Scheme(DAG的最小路径覆盖)

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=32568 [思路] DAG的最小路径覆盖. 将每个人看做一个结点,如果时间允许到达就连边,则问题转化为DAG上的最小路径覆盖问题,即找到最少的路径使得每个点位于一条路径上. 算法:将DAG中的每个结点u拆分成2个为u1,u2,如果DAG中有边uv则连边u1-v2.如果该二分图的最大匹配数为ans,则答案为n-ans.可以这样想:在一条路径中除尾结点外其他结点都有且仅

UVALive-3126 Taxi Cab Scheme (DAG的最小路径覆盖)

题目大意:要给n个人安排车,已知每个人的出发时间和起点与终点,问最少需要安排几辆车才能完成任务. 题目分析:最小路径覆盖.如果送完a到目的地后能在b出发之前赶来接b,那么连一条有向边a->b,最终将得到一个DAG.最少路径覆盖数便是答案.解法:把所有节点 i 拆成 i 和 i’,如果 i 和 j 之间连有一条边,那么则在二分图中连接 i->j’.最少路径覆盖数便是 n-最大匹配数. 代码如下: # include<iostream> # include<cstdio>

UVA 1201 - Taxi Cab Scheme(二分图匹配+最小路径覆盖)

UVA 1201 - Taxi Cab Scheme 题目链接 题意:给定一些乘客,每个乘客需要一个出租车,有一个起始时刻,起点,终点,行走路程为曼哈顿距离,每辆出租车必须在乘客一分钟之前到达,问最少需要几辆出租车 思路:如果一辆车载完一个乘客a,能去载乘客b,就连一条有向边,这样做完整个图形成一个DAG,然后要求的最少数量就是最小路径覆盖,利用二分图最大匹配去做,把每个点拆成两点,如果有边就连边,做一次最大匹配,n - 最大匹配数就是答案 代码: #include <cstdio> #inc

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

Taxi Cab Scheme 时间限制: 1 Sec  内存限制: 64 MB 题目描述 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

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