UVALive - 7368 Airports DAG图的最小路径覆盖
题目链接:
http://acm.hust.edu.cn/vjudge/problem/356788
Airports
Time Limit: 3000MS
#### 问题描述
> An airline company offers flights out of n airports, conveniently labeled
> from 1 to n. The flight time tij from airport i to airport j is known for
> every i and j. It may be the case that tij ̸= tji, due to things like wind
> or geography. Upon landing at a given airport, a plane must be inspected
> before it can be flown again. This inspection time pi
> is dependent only
> on the airport at which the inspection is taking place and not where the
> previous flight may have originated.
> Given a set of m flights that the airline company must provide, determine
> the minimum number of planes that the company needs to purchase. The airline may add
> unscheduled flights to move the airplanes around if that would reduce the total number of planes
> needed.
#### 输入
> The input file contains several test cases, each of them as described below.
> The first line of input contains two space-separated integers n and m (1 ≤ n, m ≤ 500). The next
> line contains n space-separated integers p1, . . . , pn (0 ≤ pi ≤ 106
> ).
> Each of the next n lines contains n space-separated integers. The j-th integer in line i + 2 is tij
> (0 ≤ tij ≤ 106
> ). It is guaranteed that tii = 0 for all i. However, it may be the case that tij ̸= tji when
> i ̸= j.
> Each of the next m lines contains three space-separated integers, si
> , fi
> , and ti (1 ≤ si
> , fi ≤ n,
> si ̸= fi
> , 1 ≤ ti ≤ 106
> ), indicating that the airline company must provide a flight that flies out from
> airport si at exactly time ti
> , heading directly to airport fi
> .
#### 输出
> For each test case, print, on a single line, a single integer indicating the minimum number of planes the
> airline company must purchase in order to provide the m requested flights.
#### 样例
> **sample input**
> 2 2
> 1 1
> 0 1
> 1 0
> 1 2 1
> 2 1 1
> 2 2
> 1 1
> 0 1
> 1 0
> 1 2 1
> 2 1 3
> 5 5
> 72 54 71 94 23
> 0 443 912 226 714
> 18 0 776 347 810
> 707 60 0 48 923
> 933 373 881 0 329
> 39 511 151 364 0
> 4 2 174
> 2 1 583
> 4 3 151
> 1 4 841
> 4 3 993
>
> **sample output**
> 2
> 1
> 3
题意
给你一个有向图,有边权和点权,边权代表从u飞到v所需时间,点权p[i]表示如果飞机到达i点之后还要再起飞,那就需要p[i]时间的修整时间。
现在给你m趟航班要飞,每趟航班(u,v,t)表示从u飞到v,并且在u点的起飞时间是t。现在问你用最少的飞机跑完所有的航班。
题解
我们对每趟航班建一个点,如果飞机跑完一趟航班之后还能去跑另一趟航班,我们就连一条有向边,明显这样建出来的图是偏序图,也就是DAG,然后我们对这图跑一遍最小路径覆盖就可以了。
代码
#include<map>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define M (l+(r-l)/2)
#define bug(a) cout<<#a<<" = "<<a<<endl
using namespace std;
typedef __int64 LL;
const int maxn=555;
const int INF=0x3f3f3f3f;
int n,m;
int p[maxn];
int mat[maxn][maxn],f[maxn][maxn];
vector<int> G[maxn];
pair<int,int> a[maxn],b[maxn];
int vis[maxn],lef[maxn];
int match(int u){
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
if(!vis[v]){
vis[v]=1;
if(lef[v]==-1||match(lef[v])){
lef[v]=u;
return true;
}
}
}
return false;
}
int main() {
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",&p[i]);
//把点权直接合并到边权上
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
scanf("%d",&mat[i][j]);
if(i!=j) mat[i][j]+=p[j];
}
}
//预处理出最短路,用于判断两趟航班能不能用一架飞机跑
memcpy(f,mat,sizeof(mat));
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
f[i][j]=min(f[i][j],f[i][k]+f[k][j]);
}
}
}
//存每趟航班的两个属性
for(int i=1;i<=m;i++){
int u,v,t;
scanf("%d%d%d",&u,&v,&t);
a[i]=mkp(u,t);
b[i]=mkp(v,t+mat[u][v]);
}
//判断飞机跑完航班i,能不能继续飞航班j
for(int i=1;i<=m;i++){
for(int j=1;j<=m;j++){
if(i!=j&&b[i].Y+f[b[i].X][a[j].X]<=a[j].Y){
G[i].push_back(j);
}
}
}
//DAG的最小路径覆盖
memset(lef,-1,sizeof(lef));
int ans=0;
for(int i=1;i<=m;i++){
memset(vis,0,sizeof(vis));
if(match(i)) ans++;
}
ans=m-ans;
printf("%d\n",ans);
return 0;
}
Notes
最小路径覆盖只能跑没有交叉的,比如1->2->3,4->2->5,这样有交叉的就会出问题,不过可以在建图的时候处理出1->2->3,4->5这样的东西,否则会出问题。
UVALive - 7368 Airports DAG图的最小路径覆盖的更多相关文章
- poj 2060 Taxi Cab Scheme(DAG图的最小路径覆盖)
题意: 出租车公司有M个订单. 订单格式: hh:mm a b c d 含义:在hh:mm这个时刻客人将从(a,b)这个位置出发,他(她)要去(c,d)这个位置. 规定1:从(a,b) ...
- POJ 1442 Air Raid(DAG图的最小路径覆盖)
题意: 有一个城镇,它的所有街道都是单行(即有向)的,并且每条街道都是和两个路口相连.同时已知街道不会形成回路. 可以在任意一个路口放置一个伞兵,这个伞兵会顺着街道走,依次经过若干个路口. 问最少需要 ...
- hdu1151 Air Raid,DAG图的最小路径覆盖
点击打开链接 有向无环图的最小路径覆盖 = 顶点数- 最大匹配 #include <queue> #include <cstdio> #include <cstring& ...
- 有向无环图(DAG)的最小路径覆盖(转)
DAG的最小路径覆盖 定义:在一个有向图中,找出最少的路径,使得这些路径经过了所有的点. 最小路径覆盖分为最小不相交路径覆盖和最小可相交路径覆盖. 最小不相交路径覆盖:每一条路径经过的顶点各不相同.如 ...
- 有向无环图(DAG)的最小路径覆盖
DAG的最小路径覆盖 定义:在一个有向图中,找出最少的路径,使得这些路径经过了所有的点. 最小路径覆盖分为最小不相交路径覆盖和最小可相交路径覆盖. 最小不相交路径覆盖:每一条路径经过的顶点各不相同.如 ...
- Air Raid POJ - 1422 【有向无环图(DAG)的最小路径覆盖【最小不相交路径覆盖】 模板题】
Consider a town where all the streets are one-way and each street leads from one intersection to ano ...
- hdu - 1151 Air Raid(有向无环图的最小路径覆盖)
http://acm.hdu.edu.cn/showproblem.php?pid=1151 在一个城市里有n个地点和k条道路,道路都是单向的,并且不存在环.(DAG) 现在伞兵需要去n个地点视察,伞 ...
- 训练指南 UVALive - 3126(DAG最小路径覆盖)
layout: post title: 训练指南 UVALive - 3126(DAG最小路径覆盖) author: "luowentaoaa" catalog: true mat ...
- UVAlive3126 Taxi Cab Scheme(DAG的最小路径覆盖)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=32568 [思路] DAG的最小路径覆盖. 将每个人看做一个结点,如 ...
随机推荐
- Win2008R2PHP5.4环境加载Zend模块
1.需要2个文件 Zend Optimizer和Zend Guard Loade s 下载Zend Guard Loader包.(官方地址:http://www.zend.com/en/product ...
- C# 随机颜色的方法
public string GetRandomColor() { Random RandomNum_First = new Random((int)DateTime.Now.Ticks); // 对于 ...
- mysql存储引擎(mysql学习六)
存储引擎 现在只有InnoDB支持外键 上接着学习笔记五 class表中有外键,所以不能修改存储引擎 表类型 默认的服务器表类型,通过my.ini可以配置 Default-storage-e ...
- 亚信联创--java面试题目总结
这几天投简历,只有两家的HR表示感兴趣.易思卓越和亚信联创,不管怎样如果能有机会面试都一定尽力表现,所以找了找网上的面经,这里先把题目总结一下. 职位要求如下: ------------------- ...
- Python核心编程--学习笔记--2--Python起步(下)
16 文件和内建函数open(),file() 打开文件: fobj = open(filename, 'r') for eachLine in fobj: print eachLine, #由于每行 ...
- 关于Cygwin——包管理、替换默认终端、同MSYS的比较
(搬运自我在SegmentFault的博客) Cygwin 是一个用于 Windows 的类 UNIX shell 环境. 它由两个组件组成:一个 UNIX API 库,它模拟 UNIX 操作系统提供 ...
- opengl基础学习专题 (二) 点直线和多边形
题外话 随着学习的增长,越来越觉得自己很水.关于上一篇博文中推荐用一个 学习opengl的 基于VS2015的 simplec框架.存在 一些问题. 1.这个框架基于VS 的Debug 模式下,没有考 ...
- SQLite数据库与Contentprovider(2)
ContentProvider: 在创建ContentProvider时,需要首先使用数据库.文件系统或网络实现底层存储功能, 然后在继承ContentProvider的类中实现基本数据操作的接口函数 ...
- 九度oj 1523 从上往下打印二叉树
原题链接:http://ac.jobdu.com/problem.php?pid=1523 建树,再层次遍历bfs.为了找根方便些,加了father指针... 如下: #include<algo ...
- 【javascript】html5中使用canvas编写头像上传截取功能
[javascript]html5中使用canvas编写头像上传截取功能 本人对canvas很是喜欢,于是想仿照新浪微博头像上传功能(前端使用canvas) 本程序目前在谷歌浏览器和火狐浏览器测试可用 ...