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,最后要让所有的边权值为正,且尽量大.若无解,输出结果.若可无限大,输出结 ...
随机推荐
- select标签的onchange事件
/** @1.onchange事件 ==> select选择事件* @2.obj.options ==> 选择option集合* @3.obj.selectedIndex ==> 选 ...
- 【HTTP header】【Access-Control-Allow-Credentials】跨域Ajax请求时是否带Cookie的设置
1. 无关Cookie跨域Ajax请求 客户端 以 Jquery 的 ajax 为例: $.ajax({ url : 'http://remote.domain.com/corsrequest', d ...
- javascript飞机大战-----008积分
/* 创建敌机: */ function Enemy(blood,speed,imgs,scroe){ //敌机left this.left = 0; //敌机top this.top = 0; // ...
- shell 输出文件内容
cat $filepath | while read line; do echo $line ; done #!/bin/bash #filepath=/opt/jenkins_home/worksp ...
- HDU 5618 Jam's problem again(三维偏序,CDQ分治,树状数组,线段树)
Jam's problem again Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- 转载:Linux内核调试方法
转载文章请注明作者和二维码及全文信息. 转自:http://blog.csdn.net/swingwang/article/details/72331196 不会编程的程序员,不是好的架构师,编程和内 ...
- mysql 删除表
删除表 DROP TABLE 表名;
- 关于RxJava背压
http://flyou.ren/2017/04/05/%E5%85%B3%E4%BA%8ERxJava%E8%83%8C%E5%8E%8B/?utm_source=tuicool&utm_m ...
- Elasticsearch查询规则(一)match和term
es种有两种查询模式,一种是像传递URL参数一样去传递查询语句,被称为简单搜索或查询字符串(query string)搜索,比如 GET /megacorp/employee/_search //查询 ...
- [华为]输出单向链表中倒数第k个结点
输入一个单向链表,输出该链表中倒数第k个结点,链表的倒数第1个结点为链表的尾指针. 链表结点定义如下: struct ListNode { int m_nKey; ListNode* m_ ...