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<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)的更多相关文章
- [图算法] 1030. Travel Plan (30)
		
1030. Travel Plan (30) A traveler's map gives the distances between cities along the highways, toget ...
 - 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 ...
 - PAT A 1030. Travel Plan (30)【最短路径】
		
https://www.patest.cn/contests/pat-a-practise/1030 找最短路,如果有多条找最小消耗的,相当于找两次最短路,可以直接dfs,数据小不会超时. #incl ...
 - 1030 Travel Plan (30)(30 分)
		
A traveler's map gives the distances between cities along the highways, together with the cost of ea ...
 - 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 ...
 - 1030 Travel Plan (30分)(dijkstra 具有多种决定因素)
		
A traveler's map gives the distances between cities along the highways, together with the cost of ea ...
 - PAT (Advanced Level) 1030. Travel Plan (30)
		
先处理出最短路上的边.变成一个DAG,然后在DAG上进行DFS. #include<iostream> #include<cstring> #include<cmath& ...
 - PAT甲题题解-1030. Travel Plan (30)-最短路+输出路径
		
模板题最短路+输出路径如果最短路不唯一,输出cost最小的 #include <iostream> #include <cstdio> #include <algorit ...
 - 【PAT甲级】1030 Travel Plan (30 分)(SPFA,DFS)
		
题意: 输入N,M,S,D(N,M<=500,0<S,D<N),接下来M行输入一条边的起点,终点,通过时间和通过花费.求花费最小的最短路,输入这条路径包含起点终点,通过时间和通过花费 ...
 
随机推荐
- FindBugs缺陷库
			
1.possible null pointer dereference 解释:某字段可能为空 修复:对应字段使用前判空 2.normal confidence 解释:私有成员变量没有初始化 修复:初始 ...
 - 实现JavaScript的组成----BOM和DOM
			
我们知道,一个完整的JavaScript的实现,需要由三部分组成:ECMAScript(核心),BOM(浏览器对象模型),DOM(文档对象模型). 今天主要学习BOM和DOM. BOM: BOM提供了 ...
 - Halcon C++混合编程学习之Qt 实现检测焊接点
			
1 # include "HalconCpp.h" # include "HDevThread.h" # include <X11/Xlib.h> ...
 - Linux 命令 - history: 显示或操作历史列表
			
命令格式 history [-c] [-d offset] [n] history -anrw [filename] history -ps arg [arg...] 命令参数 -c 清除历史列表. ...
 - Nginx - Core Module Directives
			
The following is the list of directives made available by the Core module. Most of these directives ...
 - RESTful 服务架构风格 * .NET的RESTful框架 OpenRasta
			
REST 的约束采用的就是掌控 Web 的基本原则.这些原则是: 用户代理与资源交互,任何可命名和表达的事物都可称为资源.每项资源都有一个唯一的统一资源标识符 (URI). 与资源的交互(通过其唯一的 ...
 - Android 如何监听返回键,弹出一个退出对话框
			
android 如何监听返回键点击事件,并创建一个退出对话框, 防止自己写的应用程序不小心点击退出键而直接退出.自己记录下这个简单的demo,备用. public class BackKeyTest ...
 - win7 服务详解-系统优化
			
Adaptive Brightness监视氛围光传感器,以检测氛围光的变化并调节显示器的亮度.如果此服务停止或被禁用,显示器亮度将不根据照明条件进行调节.该服务的默认运行方式是手动,如果你没有使用触摸 ...
 - iMAC——关闭自动弹出手机照片
			
有时当自己的iPhone手机插入一台iMAC电脑时,会自动弹出自己iPhone手机的照片. 这相当影响人的隐私等等,所以顺便将方法(参考网络的)小记一篇博客. 一.OS X 10.10以上用户解决办法 ...
 - ios开发入门篇(二):Objective-C的简单语法介绍
			
一:面向对象的思想 objective-c与C语言的编程思想不同,C语言是面向过程的编程,而objective-c则是面向对象的编程,所谓面向对象,我个人的理解,就是抽象.将具有一定共同点的实物抽象成 ...