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
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 5381   Accepted: 2256

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
2
08:00 10 11 9 16
08:07 9 16 10 11
2
08:00 10 11 9 16
08:06 9 16 10 11

Sample Output

1
2

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的更多相关文章

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

    Running a taxi station is not all that simple. Apart from the obvious demand for a centralised coord ...

  2. poj 2060 Taxi Cab Scheme (最小路径覆盖)

    http://poj.org/problem?id=2060 Taxi Cab Scheme Time Limit: 1000MS   Memory Limit: 30000K Total Submi ...

  3. HDU 1350 Taxi Cab Scheme

    Taxi Cab Scheme Time Limit: 10000ms Memory Limit: 32768KB This problem will be judged on HDU. Origin ...

  4. poj 2060 Taxi Cab Scheme (二分匹配)

    Taxi Cab Scheme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5710   Accepted: 2393 D ...

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

    Taxi Cab Scheme Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  6. 二分图最小路径覆盖--poj2060 Taxi Cab Scheme

    Taxi Cab Scheme 时间限制: 1 Sec  内存限制: 64 MB 题目描述 Running a taxi station is not all that simple. Apart f ...

  7. Taxi Cab Scheme UVALive - 3126 最小路径覆盖解法(必须是DAG,有向无环图) = 结点数-最大匹配

    /** 题目:Taxi Cab Scheme UVALive - 3126 最小路径覆盖解法(必须是DAG,有向无环图) = 结点数-最大匹配 链接:https://vjudge.net/proble ...

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

    UVA 1201 - Taxi Cab Scheme 题目链接 题意:给定一些乘客.每一个乘客须要一个出租车,有一个起始时刻,起点,终点,行走路程为曼哈顿距离,每辆出租车必须在乘客一分钟之前到达.问最 ...

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

    题意: 出租车公司有M个订单. 订单格式:     hh:mm  a  b  c  d 含义:在hh:mm这个时刻客人将从(a,b)这个位置出发,他(她)要去(c,d)这个位置. 规定1:从(a,b) ...

随机推荐

  1. hiho #1055 : 刷油漆

    上回说到,小Ho有着一棵灰常好玩的树玩具!这棵树玩具是由N个小球和N-1根木棍拼凑而成,这N个小球都被小Ho标上了不同的数字,并且这些数字都是处于1..N的范围之内,每根木棍都连接着两个不同的小球,并 ...

  2. weblogic重置密码

    1.备份DefaultAuthenticatorInit.ldift文件 cd /app/weblogic_cs/Oracle/Middleware/user_projects/domains/ntf ...

  3. Redis源码阅读笔记(1)——简单动态字符串sds实现原理

    首先,sds即simple dynamic string,redis实现这个的时候使用了一个技巧,并且C99将其收录为标准,即柔性数组成员(flexible array member),参考资料见这里 ...

  4. 动态规划——G 回文串

    G - 回文串 Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Stat ...

  5. javax.el.PropertyNotFoundException错误

    在J2EE项目的开发过程中,遇到了这个问题,报错如下: 错误原因为在我model里的Person类里定义了一个Name属性,但是读取属性的getter方法的,一般按照属性首字母小写来处理,所以把Nam ...

  6. 「书评」SAP内存计算——HANA

    因为工作关系,长期跟SAP打交道,所以去年就对HANA有了一些了解,只是公司目前的应用规模还较小,暂时没有上马HANA的打算,但是提前作一些学习还是很有必要的.正好清华大学出版社最近出版了这本< ...

  7. Solr系列二:Solr与mmseg4j的整合

    mmseg4j是一个很好的中文分词器,solr与mmseg4j的整合也非常简单.如下: 第一步:下载mmseg4j的jar包,网上搜索一下有很多下载地址,如下是csdn上的一个连接:http://do ...

  8. php如何同时连接多个数据库

    下面是一个函数能够保证连接多个数据库的下不同的表的函数,可以收藏一下,比较实用,测试过是有用的. function mysql_oper($oper,$db,$table,$where='1',$li ...

  9. DateTimePicker控件为空 分类: WinForm 2014-04-15 09:46 239人阅读 评论(0) 收藏

    设置属性:                 Format=Custom 加载事件:ValueChanged    private void dtpStart_ValueChanged(object s ...

  10. 使用Xcode8的Instruments检测解决iOS内存泄露(leak)

    在苹果没有出ARC(自动内存管理机制)时,我们几乎有一半的开发时间都耗费在这么管理内存上.后来苹果很人性的出了ARC,虽然在很大程度上,帮助我们开发者节省了精力和时间.但是我们在开发过程中,由于种种原 ...