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的最小路径覆盖. 将每个人看做一个结点,如 ...
随机推荐
- [leetcode]_Pascal's Triangle II
题目:Pascal三角的变形,要求只用O(K)的额外空间. 思路:由于Pascal三角中,tri[n][i] = tri[n - 1][i] + tri[n-1][i-1],(通常情况下) 如果已经获 ...
- excel上传和下载
需要注意的地方: 1.js构造表单并提交 2.js中文传参encodeURI(encodeURI("中文")),action接收并转换value = URLDecoder.deco ...
- ASP.net UrlRewrite的防盗链功能
ASP.net中如何实现基于UrlRewrite的防盗链. ASP.net中最快实现UrlRewrite的方法这篇文章中说了如何做UrlRewrite,那只是一个最简单的应用 其实利用UrlRewri ...
- C语言-sizeof()与strlen()的区别【转】
先看看sizeof() 一.sizeof的概念 sizeof是C语言的一种单目操作符,如C语言的其他操作符++.--等.它并不是函数.sizeof操作符以字节形式给出了其操作数的存储大小.操作数可以是 ...
- flask程序部署在openshift上的一些注意事项
https://www.openshift.com/blogs/how-to-install-and-configure-a-python-flask-dev-environment-deploy-t ...
- 【6.24-AppCan移动开发大会倒计时】科大讯飞来了!
6.24 AppCan移动开发者大会进入倒计时,报名通道即将关闭! 50多家移动圈服务商将出席此次大会,讯飞开放平台也将作为参展商,为参会者带去前沿的语音技术.参会者可现场体验最新连续语音识别技术,识 ...
- CoverFlow效果
1. 成员函数 mCamera是用来做类3D效果处理,比如z轴方向上的平移,绕y轴的旋转等 mMaxRotationAngle是图片绕y轴最大旋转角度,也就是屏幕最边上那两张图片的旋转角度 mMaxZ ...
- JVM学习总结三——垃圾回收器
整两天再看调优分析的部分,发现实际运行环境下,还是要考虑配置垃圾回收器,所以这里就加一小章介绍一下. 首先来看一下HotSpot所支持回收期的关系图: 图中可以看到一共有7中垃圾回收器,以中间绿线为界 ...
- linux安装R语言
系统:centos 6.4 64bit 安装可以使用rpm包安装,也可以用源码安装. 但是rpm安装,各种依赖比较麻烦.所以我采用源码安装. 下载:http://www.r-project.org/ ...
- mysql查看日志
工具:mysqlbinlog, 在bin目录中日志在data目录中 日志过滤:mysqlbinlog mysql-bin.000011 | less mysqlbinlog mysql-bin.000 ...