时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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<stdio.h>
#define MAX 510
int INF = ;
struct Edg
{
int len;
int cost;
}; int d[MAX];
int c[MAX];
Edg Grap[MAX][MAX];
bool vist[MAX];
int pre[MAX]; void Dijkstra(int begin,int NodeNum)
{
d[begin] = ;
c[begin] = ;
for(int i = ;i <NodeNum ;i++)
{
int index =-;
int MIN = INF;
for(int j = ;j < NodeNum ;j++)
{
if(!vist[j] && MIN > d[j])
{
MIN = d[j];
index = j;
}
} if(index == -) return; vist[index] = true; for(int v = ;v < NodeNum ; v++)
{
if(!vist[v] && Grap[index][v].len != INF)
{
if(d[index]+ Grap[index][v].len < d[v])
{
d[v] = d[index]+ Grap[index][v].len;
c[v] = c[index] + Grap[index][v].cost;
pre[v] = index;
}
else if(d[index]+ Grap[index][v].len == d[v] && c[v] > c[index] + Grap[index][v].cost)
{
c[v] = c[index] + Grap[index][v].cost;
pre[v] = index;
}
}
}
} } void DFS(int begin,int end)
{
if(end == begin)
{
printf("%d ",end);
return ;
}
DFS(begin,pre[end]);
printf("%d ",end);
} int main()
{
int i,j,N,M,S,D,x,y;
Edg Etem;
scanf("%d%d%d%d",&N,&M,&S,&D); for(i =;i < N;i++)
{
for(j =;j < N;j++)
{
Grap[i][j].cost = INF;
Grap[i][j].len = INF;
}
} for(i =;i < M;i++)
{
scanf("%d%d%d%d",&x,&y,&Etem.len,&Etem.cost);
Grap[x][y] = Grap[y][x] = Etem;
d[i] = c[i] = INF;
} Dijkstra(S,N); DFS(S,D); printf("%d %d\n",d[D],c[D]);
return ;
}

1030. Travel Plan (30)的更多相关文章

  1. [图算法] 1030. Travel Plan (30)

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

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

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

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

  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. 乐在其中设计模式(C#) - 单例模式(Singleton Pattern)【转】

    介绍 保证一个类仅有一个实例,并提供一个访问它的全局访问点. 示例 保证一个类仅有一个实例. Singleton using System; using System.Collections.Gene ...

  2. swift 中异常的处理方法

    swift 中什么时候需要处理异常,在调用系统某个方法的时,该方法最后有一个throws 说明该方法会抛出异常,如果一个方法抛出异常,那么需要对该异常进行处理 swift 异常处理提供了三种方法 方式 ...

  3. [置顶] 《MFC游戏开发》笔记一 系列简介

    本系列文章由七十一雾央编写,转载请注明出处.  http://blog.csdn.net/u011371356/article/details/9299121 作者:七十一雾央 新浪微博:http:/ ...

  4. (转载)ConcurrentHashMap 原理

    集合是编程中最常用的数据结构.而谈到并发,几乎总是离不开集合这类高级数据结构的支持.比如两个线程需要同时访问一个中间临界区 (Queue),比如常会用缓存作为外部文件的副本(HashMap).这篇文章 ...

  5. 控制器view的延迟加载

  6. 定义label标签宽度需要设置display:inline-block;

    label{    display:inline-block;     width:120px;    line-height:22px;    text-align: right;}

  7. Nginx - SSI Module

    SSI, for Server Side Includes, is actually a sort of server-side programming language interpreted by ...

  8. Genymotion安卓模拟器,性能最好

    老笔记本用AndroidSDK自带的模拟器 启动慢 运行卡 用了Genymotion启动快,运行响应媲美真机 想起学生时代,那时候智能手机还未完全普及 也用模拟器玩过”电脑里的手机“.

  9. 本招聘信息2014年长期有效!杭州派尔科技高薪诚聘android开发(10K-20K),web前端开发(8K-15K),IOS开发(15K-25K)

    杭州派尔科技有限公司发展至今,离不开员工的无私奉献和辛勤耕耘,在努力创造更好成绩的同时,公司也不忘回馈每一位员工的努力与付出.1.全面的绩效考核机制,让发展空间近在眼前!公司力争让每一位员工都了解自己 ...

  10. 【转】简单理解socket

    题外话 前几天和朋友聊天,朋友问我怎么最近不写博客了,一个是因为最近在忙着公司使用的一些控件的开发,浏览器兼容性搞死人:但主要是因为这段时间一直在看html5的东西,看到web socket时觉得很有 ...