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) ...
随机推荐
- zabbix 部分item采集间隔调整
- c# 绘图常用对象和方法
//BitMap 位图,常用的方法, Save:主要方式有:(1)保存在图像文件里,可以指定格式[gif,bmp]:(2) 保存在流中,以指定格式[gif,bmp] //gra ...
- zoj3229 Shoot the Bullet(有源汇有上下界的最大流)
题意: 一个屌丝给m个女神拍照,计划拍照n天,每一天屌丝给给定的C个女神拍照,每天拍照数不能超过D张,而且给每个女神i拍照有数量限制[Li,Ri],对于每个女神n天的拍照总和不能少于Gi,如果有解求屌 ...
- CentOS 6.3上搭建PPTP VPN
系统版本:CentOS 6.3_x86_64 eth0:172.16.10.72(实验环境当公网IP使用) eth1:192.168.100.50 1.检测是否支持ppp模块 # cat /dev/p ...
- 【转】Android4.3 蓝牙BLE初步
原文网址:http://www.cnblogs.com/savagemorgan/p/3722657.html 一.关键概念: Generic Attribute Profile (GATT) 通过B ...
- (转载)php数组添加、删除元素的方法
(转载)http://www.phpgs.com/html/php/phpjichu/20120130440.html 带来一篇php 数组 添加元素.删除元素的方法的文章,有需要的php学习者参考下 ...
- 【数学相关、规律】Codeforces 696B Puzzles
题目链接: http://codeforces.com/problemset/problem/696/B 题目大意: 给一棵树,从根节点开始递归,time=1,每次递归等概率随机访问这个节点的子节点, ...
- [JAVA关键字] synchronized
synchronized, Example: public synchronized void XXX() {} 参考 http://wenku.baidu.com/link?url=ecb1Zivf ...
- [Locked] Zigzag Iterator
Zigzag Iterator Given two 1d vectors, implement an iterator to return their elements alternately. Fo ...
- poj1026
题目大意:暗号 Bod 和 Alice 计划使用一种全新的编码方案,令人惊讶的是这不是一个公开的公匙密码,但是他们的编码基于密匙,在Philadelphia on February 16th他们的会议 ...


