A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (≤) is the number of cities (and hence the cities are numbered from 0 to N−1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:

City1 City2 Distance Cost

where the numbers are all integers no more than 500, and are separated by a space.

Output Specification:

For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.

Sample Input:

4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20

Sample Output:

0 2 3 3 40

题目分析:单源最短路
#define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
int N, M, S, D;
int a[][];
int b[][];
int Dist[];
int Path[];
int Collected[];
int Cost[];
void Dijsktra()
{
Dist[S] = ;
Path[S] = -;
Cost[S] = ;
for (int i = ; i < N; i++)
{
int minv=-, min = INT_MAX;
for (int v = ; v < N; v++)
{
if (!Collected[v] && min > Dist[v])
{
min = Dist[v];
minv = v;
}
}
Collected[minv] = ;
for (int j = ; j < N; j++)
{
if (!Collected[j] && a[minv][j] != INT_MAX)
{
if (Dist[minv] + a[minv][j] < Dist[j])
{
Dist[j] = Dist[minv] + a[minv][j];
Cost[j] = Cost[minv] + b[minv][j];
Path[j] = minv;
}
else if (Dist[minv] + a[minv][j] == Dist[j])
{
if (Cost[minv] + b[minv][j] < Cost[j])
{
Cost[j] = Cost[minv] + b[minv][j];
Path[j] = minv;
}
}
}
}
}
}
int main()
{
cin >> N >> M >> S >> D;
int c1, c2, d, c;
fill(a[],a[]+*,INT_MAX);
fill(b[], b[]+* , INT_MAX);
fill(Dist, Dist + , INT_MAX);
fill(Cost, Cost + , INT_MAX);
for (int i = ; i < M; i++)
{
cin >> c1 >> c2 >> d >> c;
a[c1][c2] = a[c2][c1] = d;
b[c1][c2] = b[c2][c1] = c;
}
Dijsktra();
int temp = D;
stack<int> st;
while (temp!=-)
{
st.push(temp);
temp = Path[temp];
}
while (!st.empty())
{
cout << st.top()<<" ";
st.pop();
}
cout << Dist[D] <<" "<<Cost[D];
return ;
}

1030 Travel Plan (30分)(dijkstra 具有多种决定因素)的更多相关文章

  1. PAT 甲级 1030 Travel Plan (30 分)(dijstra,较简单,但要注意是从0到n-1)

    1030 Travel Plan (30 分)   A traveler's map gives the distances between cities along the highways, to ...

  2. 【PAT甲级】1030 Travel Plan (30 分)(SPFA,DFS)

    题意: 输入N,M,S,D(N,M<=500,0<S,D<N),接下来M行输入一条边的起点,终点,通过时间和通过花费.求花费最小的最短路,输入这条路径包含起点终点,通过时间和通过花费 ...

  3. PAT-1030 Travel Plan (30 分) 最短路最小边权 堆优化dijkstra+DFS

    PAT 1030 最短路最小边权 堆优化dijkstra+DFS 1030 Travel Plan (30 分) A traveler's map gives the distances betwee ...

  4. [图算法] 1030. Travel Plan (30)

    1030. Travel Plan (30) A traveler's map gives the distances between cities along the highways, toget ...

  5. 1030 Travel Plan (30)(30 分)

    A traveler's map gives the distances between cities along the highways, together with the cost of ea ...

  6. PAT Advanced 1030 Travel Plan (30) [Dijkstra算法 + DFS,最短路径,边权]

    题目 A traveler's map gives the distances between cities along the highways, together with the cost of ...

  7. 1030. Travel Plan (30)

    时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A traveler's map gives the dista ...

  8. PAT A 1030. Travel Plan (30)【最短路径】

    https://www.patest.cn/contests/pat-a-practise/1030 找最短路,如果有多条找最小消耗的,相当于找两次最短路,可以直接dfs,数据小不会超时. #incl ...

  9. PAT甲题题解-1030. Travel Plan (30)-最短路+输出路径

    模板题最短路+输出路径如果最短路不唯一,输出cost最小的 #include <iostream> #include <cstdio> #include <algorit ...

随机推荐

  1. Java原来还可以这么学:如何搞定面试中必考的集合类

    原创声明 本文作者:黄小斜 转载请务必在文章开头注明出处和作者. 系列文章介绍 本文是<五分钟学Java>系列文章的一篇 本系列文章主要围绕Java程序员必须掌握的核心技能,结合我个人三年 ...

  2. 树莓派上搭建唤醒词检测引擎 Snowboy

    Snowboy 是一款高度可定制的唤醒词检测引擎,可以用于实时嵌入式系统,并且始终监听(即使离线).当前,它可以运行在 Raspberry Pi.(Ubuntu)Linux 和 Mac OS X 系统 ...

  3. Redis集群搭建及选举原理

    redis集群简述 哨兵模式中如果主从中master宕机了,是通过哨兵来选举出新的master,在这个选举切换主从的过程,整个redis服务是不可用的.而且哨兵模式中只有一个主节点对外提供服务,因此没 ...

  4. C++ 随笔练习 求和

    #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> int main() { int ...

  5. JS的3种核心设计模式

    一.单例模式 1.保证一个类仅有一个实例,并提供一个访问它的全局访问点 2.设计思路:如果存在,不创建,直接返回,不存在才创建. 在类的constructor方法里添加一个判断条件属性,并且让创建的实 ...

  6. css3特性简要概括

    ---恢复内容开始--- css3新增核心知识 背景和边框 文本效果 2d/3d转换 过渡和动画 多列布局 弹性盒模型 媒体查询 增强选择器 css3浏览器兼容性 css3在线工具 css3gener ...

  7. C++ json解决方案

    前段时间用到C++来封装com 因此从数据转换上我采用的Json来当两种语言的传递方式,现做下json的序列化与反序列化方案的总结: Rapidjson 文档地址:http://rapidjson.o ...

  8. Event loops秒懂

    Event loops秒懂 简介 JS是一种单线程脚本语言,为什么要设计成单线程? 举例说明,假设JS是多线程脚本语言,A线程修改了DOM,B线程删除了DOM,一旦B线程先执行完,DOM被删除了,A线 ...

  9. Spring Cloud - Nacos注册中心入门单机模式及集群模式

    近几年微服务很火,Spring Cloud提供了为服务领域的一整套解决方案.其中Spring Cloud Alibaba是我们SpringCloud的一个子项目,是提供微服务开发的一站式解决方案. 包 ...

  10. Python3学习之路~9.3 GIL、线程锁之Lock\Rlock\信号量、Event

    一 Python GIL(Global Interpreter Lock) 全局解释器锁 如果一个主机是单核,此时同时启动10个线程,由于CPU执行了上下文的切换,让我们宏观上看上去它们是并行的,但实 ...