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); Mis 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

 #include <iostream>
#include <vector>
using namespace std;
#define inf 999999999
//使用dijkstra
int N, M, S, D;
vector<int>tempPath, path;
struct Node
{
int dis = inf, w = inf;
}node;
int minW = inf;
void DFS(vector<vector<Node>>&city, vector<vector<int>>&father, int k)
{
tempPath.push_back(k);
if (k == S)
{
int tempW = ;
for (int i = tempPath.size() - ; i > ; --i)
tempW += city[tempPath[i]][tempPath[i - ]].w;
if (tempW < minW)
{
minW = tempW;
path = tempPath;
}
tempPath.pop_back();
return;
}
for (int i = ; i < father[k].size(); ++i)
DFS(city, father, father[k][i]);
tempPath.pop_back();
}
int main()
{
cin >> N >> M >> S >> D;
vector<vector<Node>>city(N, vector<Node>(N, node));
vector<vector<int>>father(N, vector<int>(, S));
for (int i = ; i < M; ++i)
{
int a, b;
cin >> a >> b >> node.dis >> node.w;
city[a][b] = city[b][a] = node;
}
vector<int>dis(N , inf);
vector<bool>visit(N, false);
dis[S] = ;
//Dijkstra
for (int i = ; i < N; ++i)
{
int index = -, minDis = inf;
for (int j = ; j < N; ++j)
{
if (visit[j]== false && minDis > dis[j])
{
index = j;
minDis = dis[j];
}
}
if (index == -)break;
visit[index] = true;
for (int j = ; j < N; ++j)
{
if (visit[j] == false && city[index][j].dis < inf)
{
if (dis[j] > dis[index] + city[index][j].dis)
{
dis[j] = dis[index] + city[index][j].dis;
father[j][] = index;
}
else if(dis[j] == dis[index] + city[index][j].dis)
father[j].push_back(index);
}
}
}
DFS(city, father, D);
for (int i = path.size() - ; i >= ; --i)
cout << path[i] << " ";
cout << dis[D] << " " << minW;
return ;
}

PAT甲级——A1030 Travel Plan的更多相关文章

  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

    https://pintia.cn/problem-sets/994805342720868352/problems/994805464397627392 A traveler's map gives ...

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

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

  4. A1030. Travel Plan

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

  5. 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 ...

  6. PAT_A1030#Travel Plan

    Source: PAT A1030 Travel Plan (30 分) Description: A traveler's map gives the distances between citie ...

  7. PAT甲级题解分类byZlc

    专题一  字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...

  8. PAT 1030 Travel Plan[图论][难]

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

  9. pat甲级题解(更新到1013)

    1001. A+B Format (20) 注意负数,没别的了. 用scanf来补 前导0 和 前导的空格 很方便. #include <iostream> #include <cs ...

随机推荐

  1. Mybatis笔记 - 原始Dao开发方法

    使用Mybatis开发Dao,通常有两个方法,即原始Dao开发方法和Mapper接口开发方法.原始Dao的开发方式是基于入门程序的基础上,对 控制程序 进行分层开发,程序员需要 编写 Dao接口 和 ...

  2. C语言进阶学习第一章

    1.在C语言里面使用scanf给某个变量赋值时候,如果成功返回1,失败返回0:测试代码如下: /***假如在键盘输入的不是整形数据,则输出0,否则输出1***/ void main() { int a ...

  3. CentOS中GDB提示Missing separate debuginfos解决办法

    安装debuginfo 修改文件 vi /etc/yum.repo.d/CentOS-Debuginfo.repo 修改enabled的值为1 使用debuginfo-install安装需要的文件

  4. java关键字之instanceof

    首先来看段测试代码 public class TestInstanceof{ public static void main(String[] args){ int a = 1; if(a insta ...

  5. vue 利用intersectionOberver实现全局appear/disappear事件

    搬运自:https://juejin.im/post/5cd10959f265da03a00fe5c6 效果: demo地址: https://codepen.io/deepkolos/pen/OYP ...

  6. pycharm IDE在导入自定义模块时提示有错,但实际没错

    在建立python项目时,有时为了区分资源和代码,如在项目文件夹下新建img和src两个文件夹,这时导入自定义模块会提示错误,结果没错但感觉别扭.如: 这是因为pycharm提示功能是从根目录上去寻找 ...

  7. 操作RDS数据库

  8. 利用HttpWebRequest模拟提交图片

    利用HttpWebRequest模拟提交图片 最近在做排量post工具, 以前做的都是提交文字 这次需要post图片过去,弄了半天终于弄好了: /// <summary> /// Post ...

  9. 转载 pep8安装

    一.前提准备 在Python安装了pip的情况下,命令行输入 pip install autopep8 二.PyCharm设置 成功之后,打开PyCharm,File-->setting--&g ...

  10. html+css简单的实现360搜索引擎首页面

    今天主要学习了是如何实现的,以及我在写这个页面的时候我所遇到的一些困难. 主要实现是用代码的,不说废话了,其实我是想说我走的坑有哪些. 1.代码的基础不好,元素的一些属性不熟悉,对于HTML和css还 ...