题目链接:

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图的最小路径覆盖的更多相关文章

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

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

  2. POJ 1442 Air Raid(DAG图的最小路径覆盖)

    题意: 有一个城镇,它的所有街道都是单行(即有向)的,并且每条街道都是和两个路口相连.同时已知街道不会形成回路. 可以在任意一个路口放置一个伞兵,这个伞兵会顺着街道走,依次经过若干个路口. 问最少需要 ...

  3. hdu1151 Air Raid,DAG图的最小路径覆盖

    点击打开链接 有向无环图的最小路径覆盖 = 顶点数- 最大匹配 #include <queue> #include <cstdio> #include <cstring& ...

  4. 有向无环图(DAG)的最小路径覆盖(转)

    DAG的最小路径覆盖 定义:在一个有向图中,找出最少的路径,使得这些路径经过了所有的点. 最小路径覆盖分为最小不相交路径覆盖和最小可相交路径覆盖. 最小不相交路径覆盖:每一条路径经过的顶点各不相同.如 ...

  5. 有向无环图(DAG)的最小路径覆盖

    DAG的最小路径覆盖 定义:在一个有向图中,找出最少的路径,使得这些路径经过了所有的点. 最小路径覆盖分为最小不相交路径覆盖和最小可相交路径覆盖. 最小不相交路径覆盖:每一条路径经过的顶点各不相同.如 ...

  6. Air Raid POJ - 1422 【有向无环图(DAG)的最小路径覆盖【最小不相交路径覆盖】 模板题】

    Consider a town where all the streets are one-way and each street leads from one intersection to ano ...

  7. hdu - 1151 Air Raid(有向无环图的最小路径覆盖)

    http://acm.hdu.edu.cn/showproblem.php?pid=1151 在一个城市里有n个地点和k条道路,道路都是单向的,并且不存在环.(DAG) 现在伞兵需要去n个地点视察,伞 ...

  8. 训练指南 UVALive - 3126(DAG最小路径覆盖)

    layout: post title: 训练指南 UVALive - 3126(DAG最小路径覆盖) author: "luowentaoaa" catalog: true mat ...

  9. UVAlive3126 Taxi Cab Scheme(DAG的最小路径覆盖)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=32568 [思路] DAG的最小路径覆盖. 将每个人看做一个结点,如 ...

随机推荐

  1. 3种方式实现Java多线程

    java中实现多线程的方法有两种:继承Thread类和实现runnable接口. 1.继承Thread类,重写父类run()方法 public class thread1 extends Thread ...

  2. Jquery数组操作技巧

    Jquery对数组的操作技巧. 1. $.each(array, [callback]) 遍历[常用]  解释: 不同于例遍 jQuery 对象的 $.each() 方法,此方法可用于例遍任何对象(不 ...

  3. Mysql常用数据类型详细说明及实例说明(学习笔记一)

    1.Mysql 在windows下 Net start mysql[启动] Net stop mysql[停止] Quit[退出mysql命令行] \c[取消输入的命令] Select version ...

  4. 使用WIF实现单点登录Part III —— 正式实战

    我们接下来的demo将包括以下的工程: SiteA —— 基于.net framework 4.5的MVC 4程序,使用WIF 4.5的SDK,第一个RP SiteB —— 基于.net framew ...

  5. django-url调度器-高级篇

    我们在中级篇中学会了如何进行反向解析,但是有这样一个问题,在为 url 命名的时候,名字不能重复,否则会导致各种各样的问题.在 url 还少的时候保证不重名还是比较简单的,但是 url 多起来以后就比 ...

  6. <bootstrap>bs2和3的区别</bootstrap>

    实验室的list网站开始动工了,准备打算用bootstrap作布局. 大前天去本部停了长html5峰会大连站的讲演,着急往回赶,很多感兴趣的东西都没有听到,但是还是了解了一些html5的新特性 电脑端 ...

  7. 命令行工具cmder

    1.下载地址 http://bliker.github.io/cmder/ 分为两个版本:mini版和Full版 2.快捷命令配置: 比如,快速启动canssandra/redis数据库服务和查询工具 ...

  8. ruby on rails 实战(一)

    通过ruby on rails 一步一步搭建个人站点,涉及到的技术有:ruby,rails,javascript,jquery 操作系统:win7 IDE: rubymine 5.4. 第一步,下载安 ...

  9. SQL Server 一些关键字详解(二)

    1.LEFT JOIN 容易让人误解的地方 背景:因为在网上搜了下 LEFT JOIN 和 OUTER APPLY 的区别,时发现,有的网友解释为: 1) A   left  join  B  的连接 ...

  10. 转载: android 学习架构

    http://www.cnblogs.com/forlina/archive/2011/06/29/2093332.html 引言 通过前面两篇: Android 开发之旅:环境搭建及HelloWor ...