1030. Travel Plan (30)

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 (<=500) 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
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std; const int maxn=;
const int INF=1e9; int n,m,s,den;
int G[maxn][maxn],cost[maxn][maxn];
int d[maxn];
int c[maxn];
bool vis[maxn];
int pre[maxn]; void dijstra(int begin)
{
fill(d,d+maxn,INF);
fill(c,c+maxn,INF);
fill(vis,vis+maxn,false);
for(int i=;i<n;i++) pre[i]=i;
d[begin]=;
c[begin]=;
pre[begin]=begin;
for(int i=;i<n;i++)
{
int u=-,MIN=INF;
for(int j=;j<n;j++)
{
if(vis[j]==false&&d[j]<MIN)
{
u=j;
MIN=d[j];
}
}
if(u==-) return ;
vis[u]=true;
for(int v=;v<n;v++)
{
if(vis[v]==false&&G[u][v]!=INF)
{
if(d[u]+G[u][v]<d[v])
{
d[v]=d[u]+G[u][v];
c[v]=c[u]+cost[u][v];
pre[v]=u;
}
else if(d[u]+G[u][v]==d[v])
{
if(c[u]+cost[u][v]<c[v])
{
c[v]=c[u]+cost[u][v];
pre[v]=u;
}
}
}
}
}
} void printPath(int k)
{
if(k==s)
{
printf("%d ",k);
return ;
}
printPath(pre[k]);
printf("%d ",k);
} int main()
{
cin>>n>>m>>s>>den;
int city1,city2,dist,co;
fill(G[],G[]+maxn*maxn,INF);
for(int i=;i<m;i++)
{
cin>>city1>>city2>>dist>>co;
G[city1][city2]=dist;
G[city2][city1]=dist;
cost[city1][city2]=co;
cost[city2][city1]=co;
}
dijstra(s);
printPath(den);
cout<<d[den]<<" "<<c[den]<<endl;
}

[图算法] 1030. Travel Plan (30)的更多相关文章

  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 A 1030. Travel Plan (30)【最短路径】

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

  3. 1030. Travel Plan (30)

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

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

    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. 1030 Travel Plan (30分)(dijkstra 具有多种决定因素)

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

  7. PAT (Advanced Level) 1030. Travel Plan (30)

    先处理出最短路上的边.变成一个DAG,然后在DAG上进行DFS. #include<iostream> #include<cstring> #include<cmath& ...

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

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

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

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

随机推荐

  1. 关于Xshell无法连接本地虚拟机的问题

    近期想搭建一个测试用的集群,但是!  刚开始搭第一台虚拟机就出现问题了,Xshell无法连接到虚拟机! 然后我更改了/etc/sysconfig/network-scripts/ifcfg-ens33 ...

  2. python列表的通用操作

    #'+'和'*'#+可以将两个列表拼接为一个列表my_list = [1,2,3]+[4,5,6]#*可以将列表重复指定的次数my_list = [1,2,3]*5 print(my_list) #创 ...

  3. C#中的抽象方法,虚方法,接口之间的对比

    1.首先来看一看抽象类 抽象类是特殊的类,不能够被实例化:具有类的其他特性:抽象方法只能声明于抽象类中,且不包含任何实现 (就是不能有方法体),派生类也就是子类必须对其进行重写.另外,抽象类可以派生自 ...

  4. 爬虫常用的 urllib 库知识点

    urllib 库 urllib 库是 Python 中一个最基本的网络请求库.它可以模仿浏览器的行为向指定的服务器发送请求,同时可以保存服务器返回的数据. urlopen() 在 Python3 的 ...

  5. python2.7入门---GUI编程(Tkinter)

        Python 提供了多个图形开发界面的库,几个常用 Python GUI 库如下: Tkinter: Tkinter 模块(Tk 接口)是 Python 的标准 Tk GUI 工具包的接口 . ...

  6. 20155233 《Java程序设计》 实验三 敏捷开发与XP实践

    20155233 <Java程序设计> 实验三 敏捷开发与XP实践 实验内容 XP基础 XP核心实践 相关工具 实验步骤与内容 1.在IDEA中使用工具(Code->Reformat ...

  7. 20155301 2016-2017-2 《Java程序设计》第4周学习总结

    20155301 2016-2017-2 <Java程序设计>第4周学习总结 教材学习内容总结 1.关键词extends,表示SwordsMan会扩充Role的行为,然后再扩充Role原本 ...

  8. 20155321实验二 Java面向对象程序设计

    实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验步骤 单元测试 三种代码 伪代码:使用自然语言来显示设 ...

  9. pyqt5 菜单,工具栏,线程,matplotlib

    import sys from PyQt5.QtGui import QIcon from PyQt5.QtWidgets import QMainWindow, QMenuBar, QToolBar ...

  10. 【BZOJ3197】[SDOI2013]刺客信条

    [BZOJ3197][SDOI2013]刺客信条 题面 bzoj 洛谷 题解 关于树的同构,有一个非常好的性质: 把树的重心抠出来,那么会出现两种情况: 1.有一个重心,那么我们直接把这个重心作为树的 ...