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. UVA-699-The Falling Leaves-二叉树+递归

    Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on ...

  2. 如何在VUE项目中使用SCSS

    首先要了解什么是CSS 预处理器? SCSS是一种CSS预处理语言 定义了一种新的专门的编程语言,编译后形成正常的css文件,为css增加一些编程特性,无需考虑浏览器的兼容性(完全兼容css3),让c ...

  3. 【LGP5350】序列

    题目 可能\(\operatorname{fhq\ treap}\)能做,但是珂朵莉树显然更好写 珂朵莉树是个很玄学的东西啊,就是直接使用\(\operatorname{std::set}\)维护每一 ...

  4. P3338 [ZJOI2014]力 /// FFT 公式转化翻转

    题目大意: https://www.luogu.org/problemnew/show/P3338 题解 #include <bits/stdc++.h> #define N 300005 ...

  5. Java工具类NumberUtils使用

    int数据类型和long数据类型 int占32位,long占64位,long表示的数据更大:public static int toInt(String str) NumberUtils.toInt( ...

  6. mysqldump使用记录

    #导出单个数据库:结构 无数据 [root@localhost ~]#mysqldump -h127.0.0.1 -uroot -p --opt --no-data db_name >~/db_ ...

  7. JS规则 较量较量(比较操作符) 两个操作数通过比较操作符进行比较,得到值为真(true)和假(false)。【>; <; >=; <=; !=;==】

    较量较量(比较操作符) 我们先来做道数学题,数学考试成绩中,小明考了90分,小红考了95分,问谁考的分数高? 答: 因为"95 > 90",所以小红考试成绩高. 其中大于号& ...

  8. 全网最全乌云drops文章下载(epub)

    前几天搞得epub格式的,为了方便kindle才做的,没想到站关了. 链接: http://pan.baidu.com/s/1eRIoJC2 密码: b6aq

  9. 网站时间显示——基于Date

    网站时间显示 代码实现如下: =============css样式=================== <style> #show{ width: 460px; height: 100p ...

  10. wordpress 插件语法分析器

    在通过查看 apply_filters( 'ap_addon_form_args', array $form_args ) 的html body class中发现wp-parser 字样,就googl ...