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 机场快线的更多相关文章

  1. 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板)

    layout: post title: 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板) author: "luowentaoaa" catalo ...

  2. UVA 11374 Airport Express SPFA||dijkstra

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  3. UVA - 11374 - Airport Express(堆优化Dijkstra)

    Problem    UVA - 11374 - Airport Express Time Limit: 1000 mSec Problem Description In a small city c ...

  4. uva 11374 最短路+记录路径 dijkstra最短路模板

    UVA - 11374 Airport Express Time Limit:1000MS   Memory Limit:Unknown   64bit IO Format:%lld & %l ...

  5. UVA 11374 Airport Express 机场快线(单源最短路,dijkstra,变形)

    题意: 给一幅图,要从s点要到e点,图中有两种无向边分别在两个集合中,第一个集合是可以无限次使用的,第二个集合中的边只能挑1条.问如何使距离最短?输出路径,用了第二个集合中的哪条边,最短距离. 思路: ...

  6. UVA 11374 Airport Express(最短路)

    最短路. 把题目抽象一下:已知一张图,边上的权值表示长度.现在又有一些边,只能从其中选一条加入原图,使起点->终点的距离最小. 当加上一条边a->b,如果这条边更新了最短路,那么起点st- ...

  7. UVA - 11374 Airport Express (Dijkstra模板+枚举)

    Description Problem D: Airport Express In a small city called Iokh, a train service, Airport-Express ...

  8. uva 11374

    Problem D: Airport Express In a small city called Iokh, a train service, Airport-Express, takes resi ...

  9. UVA 11374 Halum (差分约束系统,最短路)

    题意:给定一个带权有向图,每次你可以选择一个结点v 和整数d ,把所有以v为终点的边权值减少d,把所有以v为起点的边权值增加d,最后要让所有的边权值为正,且尽量大.若无解,输出结果.若可无限大,输出结 ...

随机推荐

  1. 一步步从Spring Framework装配掌握SpringBoot自动装配

    目录 Spring Framework模式注解 Spring Framework@Enable模块装配 Spring Framework条件装配 SpringBoot 自动装配 本章总结 Spring ...

  2. Requested bean is currently in creation: Is there an unresolvable circular reference?

    spring容器初始化报错:循环依赖,错误信息如下: Requested bean is currently in creation: Is there an unresolvable circula ...

  3. 防止独立IP被其它恶意域名恶意解析

    一:什么是恶意域名解析 一般情况下,要使域名能访问到网站需要两步,第一步,将域名解析到网站所在的主机,第二步,在web服务器中将域名与相应的网站绑定.但是,如果通过主机IP能直接访问某网站,那么把域名 ...

  4. 阿里云ecs禁止ping,禁止telnet

    现在的中小型企业服务器大多是云比较多,因此,可能会面临着服务器ping不通,或者是端口telnet不通的情况,但是服务器上的服务仍然是正常的情况,这个时候我们就要考虑是不是云上配置了访问规则了.废话不 ...

  5. 02Del.ashx(删除班级)

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using WebHelper ...

  6. IIS 6.0上部署ASP.NET MVC2.0

    在IIS7.5及8.0上部署都没有成功,对于身份验证会出现问题,据说是要安装什么东西,在这里说下IIS6.0的配置吧,下面是使用.net 4.0,自己可以选择所需的版本. 再此之前先确定web是用到了 ...

  7. Only a type can be imported. classname resolves to a package的解决

    Only a type can be imported. l1.l2.MyClass resolves to a package ==========这里是解决方案=============== 把生 ...

  8. Python实现Table To Point代码 分类: Python 2015-07-31 18:45 3人阅读 评论(0) 收藏

    </pre><pre name="code" class="python"><span style="font-fami ...

  9. Redis 缓存穿透,缓存击穿,缓存雪崩的解决方案分析

    设计一个缓存系统,不得不要考虑的问题就是:缓存穿透.缓存击穿与失效时的雪崩效应. 一.什么样的数据适合缓存? 分析一个数据是否适合缓存,我们要从访问频率.读写比例.数据一致性等要求去分析.  二.什么 ...

  10. uchome登录验证

    Uchome采用cookie+数据库的方式来进行用户登录验证的 一.登录 1:登录表单由source/do_login.php 处理 2:然后验证用户名以及密码的正确性,不正确则跳转并提示登录失败 3 ...