Taxi Cab Scheme POJ && HDU
|
||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts |
Problems Submit Problem Online Status Prob.ID: |
Register Update your info Authors ranklist |
Current Contest Past Contests Scheduled Contests Award Contest |
zhongshijun Log Out Mail:0(0) Login Log Archive |
||||||
|
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 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 Sample Output 1 Source |
题意:
题目意思就是告诉你每个人的出发时间、出发地点、出发目的地,叫你求如何用最少的出租车。题目的输入格式是先给你一个开始时间,之后是出发坐标(x1,y1),然后是目的地坐标(x2,y2),则该出租车要到达目的地所需时间花费为time = |x1-x2|+|y1-y2|;
本题是一道典型的二分匹配图中的DAG的最小路径覆盖。
最小路径公式:answer = n - m(最大匹配数);
最小路径覆盖:就是在图上找尽量少的路径,使得每个节点恰好在一条路径上(换句话说,不同的路径不能有公共点)。注意,单独的节点也可以作为一条路径。
DAG最小路径覆盖的解法如下:把所有节点i拆为X结点i和Y结点i',如果图G中存在有向边i->j,则在二分图中引入边i->j'。设二分图的最大匹配数为m,则结果就是n-m;
下面给出用邻接矩阵和邻接表写得KM算法。邻接表可以快上好几倍啊!!!
邻接矩阵:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CL(x,v);memset(x,v,sizeof(x)); const int MAX = 500 + 10;
struct Point
{
int x1,y1,x2,y2;
int start,end;
}p[MAX];
int link[MAX],n;
bool graph[MAX][MAX],used[MAX]; int Find(int u)
{
for(int v= 1;v <= n;v++)
if(!used[v]&&graph[u][v])
{
used[v] = 1;
if(link[v] == -1||Find(link[v]))
{
link[v] = u;
return 1;
}
}
return 0;
} int KM()
{
int res = 0;
CL(link,-1);
for(int u = 1;u <= n;u++)
{
CL(used,0);
res += Find(u);
}
return res;
}
int main()
{
int T,i,j,HH,MM;
scanf("%d",&T);
while(T--)
{
CL(graph,0);
scanf("%d",&n);
for(i = 1;i <= n;i++)
{
scanf("%d:%d",&HH,&MM);
scanf("%d%d%d%d",&p[i].x1,&p[i].y1,&p[i].x2,&p[i].y2);
p[i].start = p[i].end = HH*60 + MM;
p[i].end += abs(p[i].x1-p[i].x2)+abs(p[i].y1-p[i].y2);
for(j = i-1;j > 0;j--)
{
int dist=abs(p[i].x1-p[j].x2)+abs(p[i].y1-p[j].y2);
if(dist+p[j].end < p[i].start)
graph[i][j] = 1;
}
}
printf("%d\n",n-KM());
}
return 0;
}
邻接表:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define CL(x,v);memset(x,v,sizeof(x)); const int MAX = 500 + 10;
struct Point
{
int x1,y1,x2,y2;
int start,end;
}p[MAX];
bool used[MAX];
int next[MAX*MAX],vex[MAX*MAX],head[MAX*MAX];
int top,n,link[MAX]; int Find(int u)
{
for(int i = head[u];i != -1;i = next[i])
{
int v = vex[i];
if(!used[v])
{
used[v] = 1;
if(link[v] == -1||Find(link[v]))
{
link[v] = u;
return 1;
}
}
}
return 0;
} int KM()
{
int res = 0;
CL(link,-1);
for(int u = 1;u <= n;u++)
{
CL(used,0);
res += Find(u);
}
return res;
}
int main()
{
int T,i,j,HH,MM;
scanf("%d",&T);
while(T--)
{
top = 0;
CL(head,-1);
scanf("%d",&n);
for(i = 1;i <= n;i++)
{
scanf("%d:%d",&HH,&MM);
p[i].start = HH*60 + MM;
scanf("%d%d%d%d",&p[i].x1,&p[i].y1,&p[i].x2,&p[i].y2);
int ntime = abs(p[i].x1-p[i].x2) + abs(p[i].y1-p[i].y2);
p[i].end = p[i].start + ntime;
for(j = i-1;j > 0;j--)
{
ntime = abs(p[i].x1-p[j].x2)+abs(p[i].y1-p[j].y2);
if(ntime+p[j].end < p[i].start)
{
next[top] = head[i];
vex[top] = j;
head[i] = top++;
}
}
}
printf("%d\n",n-KM());
}
return 0;
}
Taxi Cab Scheme POJ && HDU的更多相关文章
- Taxi Cab Scheme POJ - 2060 二分图最小路径覆盖
Running a taxi station is not all that simple. Apart from the obvious demand for a centralised coord ...
- poj 2060 Taxi Cab Scheme (最小路径覆盖)
http://poj.org/problem?id=2060 Taxi Cab Scheme Time Limit: 1000MS Memory Limit: 30000K Total Submi ...
- HDU 1350 Taxi Cab Scheme
Taxi Cab Scheme Time Limit: 10000ms Memory Limit: 32768KB This problem will be judged on HDU. Origin ...
- poj 2060 Taxi Cab Scheme (二分匹配)
Taxi Cab Scheme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5710 Accepted: 2393 D ...
- 【HDU1960】Taxi Cab Scheme(最小路径覆盖)
Taxi Cab Scheme Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 二分图最小路径覆盖--poj2060 Taxi Cab Scheme
Taxi Cab Scheme 时间限制: 1 Sec 内存限制: 64 MB 题目描述 Running a taxi station is not all that simple. Apart f ...
- Taxi Cab Scheme UVALive - 3126 最小路径覆盖解法(必须是DAG,有向无环图) = 结点数-最大匹配
/** 题目:Taxi Cab Scheme UVALive - 3126 最小路径覆盖解法(必须是DAG,有向无环图) = 结点数-最大匹配 链接:https://vjudge.net/proble ...
- UVA 1201 - Taxi Cab Scheme(二分图匹配+最小路径覆盖)
UVA 1201 - Taxi Cab Scheme 题目链接 题意:给定一些乘客.每一个乘客须要一个出租车,有一个起始时刻,起点,终点,行走路程为曼哈顿距离,每辆出租车必须在乘客一分钟之前到达.问最 ...
- poj 2060 Taxi Cab Scheme(DAG图的最小路径覆盖)
题意: 出租车公司有M个订单. 订单格式: hh:mm a b c d 含义:在hh:mm这个时刻客人将从(a,b)这个位置出发,他(她)要去(c,d)这个位置. 规定1:从(a,b) ...
随机推荐
- POJ-1151-Atlantis(线段树+扫描线+离散化)[矩形面积并]
题意:求矩形面积并 分析:使用线段树+扫描线...因为坐标是浮点数的,因此还需要离散化! 把矩形分成两条边,上边和下边,对横轴建树,然后从下到上扫描上去,用col表示该区间有多少个下边,sum代表该区 ...
- redis数据过期策略【转】
key的过期时间通常,Redis key被创建时不会自动关联过期时间,key将长久存在,除非通过DEL等命令显示的删除.EXPIRE命令簇可以为指定的key关联一个过期时间,代价是一点额外的内存开销. ...
- 线段树(区间修改、区间查询) HDU 1754 I Hate It
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- 【搜索】【并查集】Codeforces 691D Swaps in Permutation
题目链接: http://codeforces.com/problemset/problem/691/D 题目大意: 给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交 ...
- 使用sklearn进行数据预处理 —— 归一化/标准化/正则化
一.标准化(Z-Score),或者去除均值和方差缩放 公式为:(X-mean)/std 计算时对每个属性/每列分别进行. 将数据按期属性(按列进行)减去其均值,并除以其方差.得到的结果是,对于每个属 ...
- bzoj1588 [HNOI2002]营业额统计(Treap)
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 11485 Solved: 4062[Submit][Sta ...
- hdu 4619 二分图最大匹配 ——最大独立集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4619 #include <cstdio> #include <cmath> # ...
- HDU 1247
简单的字典树 - -,求一个单词是否由两个单词组成 #include<iostream> #include<cstring> #include<cstdio> us ...
- eclipse连接远程hadoop集群开发时0700问题解决方案
eclipse连接远程hadoop集群开发时报错 错误信息: Exception in thread "main" java.io.IOException:Failed to se ...
- 纠结的CLI C++与Native C++的交互
最近在写点东西,涉及到了CLR C++与Native C++的互相调用的问题,结果...........纠结啊. 交互原型 交互原型是这样的: void* avio_alloc_context( un ...


