hdu-1350-最小路径覆盖(二分图)
来源:互联网 发布:网络拨号图标不见了 编辑:程序博客网 时间:2024/06/01 07:13
Taxi Cab Scheme
Problem 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.
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
2208:00 10 11 9 1608:07 9 16 10 11208:00 10 11 9 1608:06 9 16 10 11
Sample Output
12
Source
Northwestern Europe 2004
分析:本题的模型是DAG(有向无环图)的最小路径覆盖。所谓最小路径覆盖,就是在图中找尽量少的路径,使得每个结点恰好在一条路径上(不同的路径不能有公共点)。单独的结点也可以作为一条路径。
解法:把所有结点i拆为X结点i和Y结点i',如果图G中存在有向边i->j,则在二分图中引入边i->j'。设二分图的最大匹配数为m,则结果就是n-m。
杭电好像不支持fabs,要用abs
#include<stdio.h>#include<string.h>#include<math.h>bool map[505][505],visit[505];int match[505];int M,s[505][10];bool dfs(int u){ int i; for(i=1;i<=M;i++) { if(i!=u&&map[u][i]==true&&!visit[i]) { visit[i]=true; if(!match[i]||dfs(match[i])) { match[i]=u; return true; } } } return false;}int main(){ int i,N,h,f,j,time1,ans; scanf("%d",&N); while(N--) { ans=0; scanf("%d",&M); memset(match,0,sizeof(match)); memset(map,0,sizeof(map)); memset(s,0,sizeof(s)); for(i=1;i<=M;i++) { scanf("%d:%d",&s[i][0],&s[i][1]); s[i][6]=s[i][0]*60+s[i][1]; //初始时间 scanf("%d%d%d%d",&s[i][2],&s[i][3],&s[i][4],&s[i][5]); s[i][7]=fabs(s[i][2]-s[i][4])+fabs(s[i][3]-s[i][5])+s[i][6];//从起点到终点的时间+初始时间 } for(i=1;i<M;i++) { for(j=i+1;j<=M;j++) { time1=fabs(s[j][2]-s[i][4])+fabs(s[j][3]-s[i][5]); //上一个到下一个所用的时间 if(s[i][7]+time1+1<=s[j][6]) map[i][j]=true; } } for(i=1;i<=M;i++) { memset(visit,0,sizeof(visit)); if(dfs(i)) ans++; } printf("%d\n",M-ans); } return 0;}
0 0
- hdu-1350-最小路径覆盖(二分图)
- hdu 4160 二分图最小路径覆盖
- hdu 1151二分图最小路径覆盖
- hdu 1151 二分图最小路径覆盖
- hdu Matrix(二分图的最小路径覆盖)
- hdu 1350 Taxi Cab Scheme(二分图最小路径覆盖)
- 二分图(最小路径覆盖)
- 最小路径覆盖问题(二分图)
- 最小路径覆盖(二分图)
- 二分图最小路径覆盖
- [二分图]最小路径覆盖
- hdu 4160 Dolls 二分图最小路径覆盖
- HDU 1151 Air Raid 二分图的最小路径覆盖
- hdu 1151 Air Raid(二分图匹配--最小路径覆盖)
- HDU 1350 & HDU 1960 & POJ 2060 Taxi Cab Scheme【二分图之最小路径覆盖,经典】
- HDU 1350(最小路径覆盖)
- HDU OJ 1350 Taxi Cab Scheme 【二分图匹配之最小路径覆盖】
- 杭电 hdu 1350 和 1960 Taxi Cab Scheme (二分匹配 + 最小路径覆盖)
- android studio 的糟心配置问题
- 深入理解UIView 和UIViewController
- 黑马程序员——OC基础语法
- 考试系统之批量删除
- eclipse提交项目到github
- hdu-1350-最小路径覆盖(二分图)
- Oracle EBS里,如何通过脚本查看客户针对某个Form的做了哪些Personalization(个性化)
- 安装MAMP后的控制台访问mysql问题
- java多线程—消费者生产者模式
- 工作队列(2)
- tcpdump命令
- 如何使用dos命令运行sqlserver
- python 模块
- Java enum的用法详解