UVa 11374 机场快线
https://vjudge.net/problem/UVA-11374
题意:
机场快线分为经济线和商业线两种,线路、速度和价格都不同。你有一张商业线车票,可以坐一站商业线,而其他时候只能乘坐经济线。你的任务是找一条去机场最快的线路。
思路:
因为商业线只能坐一站,所有可以枚举坐的是哪一站,用dijkstra算出起点到每个点的最短时间f(x)和终点到每个点的最短时间g(x),则总时间为f(a)+T(a,b)+g(b),其中T(a,b)为从a坐一站商业线到达b的时间。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std; const int INF = ;
const int maxn = + ; struct Edge
{
int from, to, dist;
Edge(int u, int v, int d) :from(u), to(v), dist(d){}
}; struct HeapNode
{
int d, u;
HeapNode(int x, int y) :d(x), u(y){}
bool operator < (const HeapNode& rhs) const{
return d > rhs.d;
}
}; struct Dijkstra
{
int n, m; //点数和边数
vector<Edge> edges; //边列表
vector<int> G[maxn]; //每个结点出发的边编号(从0开始编号)
bool done[maxn]; //是否已永久标号
int d[maxn]; //s到各个点的距离
int p[maxn]; //最短路中的上一条边 void init(int n)
{
this->n = n;
for (int i = ; i < n; i++) G[i].clear();
edges.clear();
} void AddEdges(int from, int to, int dist)
{
edges.push_back(Edge(from,to,dist));
m = edges.size();
G[from].push_back((m - ));
} void dijkstra(int s)
{
priority_queue<HeapNode> Q;
for (int i = ; i < n; i++) d[i] = INF;
d[s] = ;
memset(done, , sizeof(done));
Q.push(HeapNode(,s));
while (!Q.empty())
{
HeapNode x = Q.top(); Q.pop();
int u = x.u;
if (done[u]) continue;
done[u] = true;
for (int i = ; i < G[u].size(); i++)
{
Edge& e = edges[G[u][i]];
if (d[e.to] > d[u] + e.dist)
{
d[e.to] = d[u] + e.dist;
p[e.to] = e.from;
Q.push(HeapNode(d[e.to],e.to));
}
}
}
} void getpath(int s, int e, vector<int>& path)
{
int pos = e;
while (true)
{
path.push_back(pos);
if (pos == s)
break;
pos = p[pos];
}
} }t[]; int N, S, E;
vector<int> path; int main()
{
//freopen("D:\\input.txt", "r", stdin);
int time, kase = ;
while (scanf("%d%d%d", &N, &S, &E) != EOF)
{
S--; E--;
if (kase != ) printf("\n");
kase++;
t[].init(N);
t[].init(N);
path.clear();
int M, K;
int u, v, d;
scanf("%d", &M);
while (M--)
{
scanf("%d%d%d", &u, &v, &d);
u--; v--;
t[].AddEdges(u, v, d);
t[].AddEdges(u, v, d);
t[].AddEdges(v, u, d);
t[].AddEdges(v, u, d);
}
t[].dijkstra(S);
t[].dijkstra(E);
int ks = -, ke = -;
time = t[].d[E];
scanf("%d", &K);
while (K--)
{
scanf("%d%d%d", &u, &v, &d);
u--;v--;
if (d + t[].d[u] + t[].d[v] < time){
time = d + t[].d[u] + t[].d[v];
ks = u; ke = v;
}
if (d + t[].d[v] + t[].d[u] < time){
time = d + t[].d[v] + t[].d[u];
ks = v; ke = u;
}
}
if (ks == -)
{
t[].getpath(S, E, path);
reverse(path.begin(), path.end());
for (int i = ; i < path.size() - ; i++)
printf("%d ", path[i]+);
printf("%d\n", E+);
printf("Ticket Not Used\n");
printf("%d\n", time);
}
else
{
t[].getpath(S, ks, path);
reverse(path.begin(), path.end());
t[].getpath(E, ke, path);
for (int i = ; i < path.size() - ; i++)
printf("%d ", path[i]+ );
printf("%d\n", E+);
printf("%d\n", ks + );
printf("%d\n", time);
}
}
return ;
}
UVa 11374 机场快线的更多相关文章
- 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板)
layout: post title: 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板) author: "luowentaoaa" catalo ...
- UVA 11374 Airport Express SPFA||dijkstra
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVA - 11374 - Airport Express(堆优化Dijkstra)
Problem UVA - 11374 - Airport Express Time Limit: 1000 mSec Problem Description In a small city c ...
- uva 11374 最短路+记录路径 dijkstra最短路模板
UVA - 11374 Airport Express Time Limit:1000MS Memory Limit:Unknown 64bit IO Format:%lld & %l ...
- UVA 11374 Airport Express 机场快线(单源最短路,dijkstra,变形)
题意: 给一幅图,要从s点要到e点,图中有两种无向边分别在两个集合中,第一个集合是可以无限次使用的,第二个集合中的边只能挑1条.问如何使距离最短?输出路径,用了第二个集合中的哪条边,最短距离. 思路: ...
- UVA 11374 Airport Express(最短路)
最短路. 把题目抽象一下:已知一张图,边上的权值表示长度.现在又有一些边,只能从其中选一条加入原图,使起点->终点的距离最小. 当加上一条边a->b,如果这条边更新了最短路,那么起点st- ...
- UVA - 11374 Airport Express (Dijkstra模板+枚举)
Description Problem D: Airport Express In a small city called Iokh, a train service, Airport-Express ...
- uva 11374
Problem D: Airport Express In a small city called Iokh, a train service, Airport-Express, takes resi ...
- UVA 11374 Halum (差分约束系统,最短路)
题意:给定一个带权有向图,每次你可以选择一个结点v 和整数d ,把所有以v为终点的边权值减少d,把所有以v为起点的边权值增加d,最后要让所有的边权值为正,且尽量大.若无解,输出结果.若可无限大,输出结 ...
随机推荐
- Scala 泛型类型和方法
abstract class Stack[A] { def push(x: A): Stack[A] = new NonEmptyStack[A](x, this) def isEmpty: Bool ...
- Linux系统下 MongoDB安装搭建
1.下载linux的mongodb 2.在目录usr/local下创建文件夹mongodb,把安装包解压到该文件夹中 # mkdir mongodb # tar -zxvf mongodb-3.4.2 ...
- [NGINX] - 配置文件优化 - NGINX.CONF
Nginx 本文主要针对公司的Nginx负载均衡配置进行解释,配置文件在最下方.因为公司没有使用PHP,所以NGINX里面并没有太多facgi模块相关优化 NGINX.CONF user 语 ...
- vs 开发常用快捷键
alt+shift+enter 编辑区最大化ctrl+] 括号匹配 ctrl+j 强迫智能感知ctrl+shift+空格 强迫智能感知(参数) ctrl+k+d ...
- 磁盘 I/O 性能监控的指标
指标 1:每秒 I/O 数(IOPS 或 tps) 对于磁盘来说,一次磁盘的连续读或者连续写称为一次磁盘 I/O, 磁盘的 IOPS 就是每秒磁盘连续读次数和连续写次数之和.当传输小块不连续数据时,该 ...
- 自旋锁原理及java自旋锁
一.自旋锁的概念 首先是一种锁,与互斥锁相似,基本作用是用于线程(进程)之间的同步.与普通锁不同的是,一个线程A在获得普通锁后,如果再有线程B试图获取锁,那么这个线程B将会挂起(阻塞):试想下,如果两 ...
- 【css flex】将多个<div>放在同一行
使用style里的flex属性 默认情况下,一个div独占一行 使用css选择器给外层div加上以下flex属性,则该div的子div可以在同一行中显示, .runs-paginator-flex-c ...
- UVA796 - Critical Links(Tarjan求桥)
In a computer network a link L, which interconnects two servers, is considered critical if there are ...
- Python开发【Django】:分页、Cookie和Session
分页 1.简单分页 涉及xss攻击,需要用到mark_safe方法,使用此方法字符串传输到后端后,已html形式显示,而非字符串 HTML文件: <!DOCTYPE html> <h ...
- 【python】BeautifulSoup的应用
from bs4 import BeautifulSoup#下面的一段HTML代码将作为例子被多次用到.这是 爱丽丝梦游仙境的 的一段内容(以后内容中简称为 爱丽丝 的文档): html_doc = ...