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) ...
随机推荐
- Unity NGUI根据高度自适应屏幕分辨率
Unity版本:4.5.1 NGUI版本:3.6.5 本文内容纯粹转载,转载保留参考链接和作者 参考链接:http://blog.csdn.net/asd237241291/article/detai ...
- ♫【HTML5 敏捷实践】第1章 使用语义化的方式实现
<!DOCTYPE html> 向后兼容的HTML5<doctype>标签.HTML5规范规定<doctype>对大小写不敏感:然而,之前版本的HTML需要< ...
- POJ 3468 A Simple Problem with Integers(详细题解) 线段树
这是个线段树题目,做之前必须要有些线段树基础才行不然你是很难理解的. 此题的难点就是在于你加的数要怎么加,加入你一直加到叶子节点的话,复杂度势必会很高的 具体思路 在增加时,如果要加的区间正好覆盖一个 ...
- 西安Uber优步司机奖励政策(1月18日~1月24日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- Spring 基础知识
Spring架构简单描述 原文:https://www.shiyanlou.com/courses/document/212 Spring 概述 1. Spring 是什么 Spring是一个开源的轻 ...
- JVM调优之jstack找出发生死锁的线程
1.执行死锁程序 2.执行 jstack -l 21733 | more 结果如下: 死锁程序: public static void main(String[] args) { // TODO Au ...
- QML官方系列教程——QML Applications
附网址:http://qt-project.org/doc/qt-5/qmlapplications.html 假设你对Qt的官方demo感兴趣,能够參考本博客的另一个系列Qt5官方demo解析集 每 ...
- ubuntu sublime安装及配置
安装sublime-text-2: sudo add-apt-repository ppa:webupd8team/sublime-text-2 sudo apt-get update sudo ap ...
- [PWA] 0. Introduce to Offline First
Why offline first? Imagin you are visiting a website, it is fine if wifi connection is good. It migh ...
- [Angular 2] Create a simple search Pipe
This lesson shows you how to create a component and pass its properties as it updates into a Pipe to ...