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. 尚学python课程---13、python基础语法

    尚学python课程---13.python基础语法 一.总结 一句话总结: legend2系统使我能够快速掌握一门语法,特别有用 pass 语句:空语句:是为了保持程序结构的完整性  :作用:比如: ...

  2. Activity详解三 启动activity并返回结果 转载 https://www.cnblogs.com/androidWuYou/p/5886991.html

    首先看演示: 1 简介 .如果想在Activity中得到新打开Activity 关闭后返回的数据,需要使用系统提供的startActivityForResult(Intent intent, int ...

  3. POJ-3255-Roadblocks POJ-Dijkstra+邻接表

    今天写了这一题,一开始跑了两个Dijkstra,但是造成内存超限,所以现在在学习邻接表,打算明天用邻接表和优先队列写,目前还在学习中,所以题目啥也还没上传,先上传了今晚对于邻接表的理解(见上图),明天 ...

  4. SpringBoot--springboot启动类和controller的配置

    作为一个springboot初学者,在探索过程中难免遇到一些坑,边看书边动手,发现书本中的版本是1.0,而我使用的是最新版2.0,所以有些东西不能完全按照书本进行操作,因为2.0中已经不支持1.0中的 ...

  5. 一次性安装python常用模块

    链接:https://pan.baidu.com/s/1fuIxRUnkJJfzgrbQ8kIgvw 提取码:d1r6 电脑必须是win64才可以安装 Anaconda3安装完成后,不需要自己添加环境 ...

  6. Python - Virtualenv 创建虚拟环境

    Virtualenv 回到顶部 为了解决各个项目的共同依赖同一个环境,造成版本冲突等,virtualenv创建一个干净的环境,在这个环境下,进行Python项目的开发等,就成为一个个独立的项目,从而避 ...

  7. <数据可视化>Matplotlib(2D+3D)

    1.Matplotlib介绍(2D) Matplotlib 是 Python 2D-绘图领域使用最广泛的套件.它能让使用者很轻松地将数据图形化,并且提供多样化的输出格式. pylab 是 matplo ...

  8. 图片上传的ImageIO工具类

    ImageIO类说明 最近的项目中遇到ImageIO,因此记录下这个类的用法 一.ImageIO: 这个类中的方法都是静态方法,可以用来进行简单的图片IO操作 1.读入的三种方法 public sta ...

  9. JavaWeb实现文件下载

    1. 编写文件上传Servlet public class FileUpload1 extends HttpServlet { @Override protected void doGet(HttpS ...

  10. 廖雪峰Java12maven基础-2maven进阶-2模块管理

    1. 把大项目拆分为模块是降低软件复杂度的有效方法 在Java项目中,我们通常会会把一个项目分拆为模块,这是为了降低软件复杂度. 例如:我们可以把一个大的项目氛围module-a, module-b, ...