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. 调用系统的loading界面

    //在状态栏显示一个圈圈转动  代表正在请求 [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

  2. 使用ZXingObjC扫描二维码横竖屏对应

    /** 根据屏幕的方向设置扫描的方向 * @author maguang * @param parameter * @return result */ - (void)showaCapture { C ...

  3. java.lang.reflect.UndeclaredThrowableException: null Caused by: org.apache.zookeeper.KeeperException$UnimplementedException: KeeperErrorCode = Unimplemented for

    java.lang.reflect.UndeclaredThrowableException: null    at org.springframework.util.ReflectionUtils. ...

  4. 去除 inline-block 元素间距

    案例重现 布局时经常能发现inline元素和inline-block元素水平呈现的元素间,会存在着一些意想不到的间距,举例: .inline-block { display: inline-block ...

  5. C语言程序设计(四) 键盘输入和屏幕输出

    第四章 键盘输入和屏幕输出 转义字符 \n 换行,光标移到下一行的起始位置 \r 回车(不换行),光标移到当前行的起始位置 \0 空字符 \t 水平制表 \v 垂直制表 \b 退格 \f 走纸换页 \ ...

  6. tersserorc的简单使用

    tesserocr 是 python 的一个 OCR 库,它是对 tesseract 做的一层 Python API 封装,所以他的核心是tesseract. tesseract 的安装见 https ...

  7. 【题解】NOIP 2015 子串

    淦!这题我做了三个月啊 题目描述 有两个仅包含小写英文字母的字符串 \(A\) 和 \(B\). 现在要从字符串 \(A\) 中取出 \(k\) 个互不重叠的非空子串,然后把这 \(k\) 个子串按照 ...

  8. Asp.net 的输入框的 Enabled属性 与 ReadOnly属性

    控件不管是设置 Enabled="false" 还是ReadOnly="true",后台都取不到前台的值,值为“空”: 在界面视觉上,Enabled=" ...

  9. Bugku流量分析题目总结

    flag被盗 题目链接:https://ctf.bugku.com/files/e0b57d15b3f8e6190e72987177da1ffd/key.pcapng 解题思路: 这个题目是比较基本的 ...

  10. 042.集群网络-flannel及calico

    一 Flannel组件 1.1 flannel介绍 Kubernetes的网络模型假定了所有Pod都在一个可以直接连通的扁平网络空间中.若需要实现这个网络假设,需要实现不同节点上的Docker容器之间 ...