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行输入一条边的起点,终点,通过时间和通过花费.求花费最小的最短路,输入这条路径包含起点终点,通过时间和通过花费 ...
随机推荐
- maya 2015配置openCollada插件
1.下载对应的openCollada插件内容 https://github.com/KhronosGroup/OpenCOLLADA/wiki/OpenCOLLADA-Tools 该页面目前提供Mac ...
- [改善Java代码]Java的泛型是类型擦除的
泛型可以减少强制类型的转换,可规范集合的元素类型,还可以提高代码的安全性和可读性,正是因为有了这些优点,自从Java引入泛型之后,项目的编码规则上便多了一条,优先使用泛型. Java泛型(Generi ...
- IP地址计算和划分
一. B类地址 范围从128-191(第一串8位二进制10000000~10111111),如172.168.1.1,第一和第二段号码为网络号码,剩下的2段号码为本地计算机的号码.转换为2进 ...
- PS基础学习 1---基本工具
1,选框工具: 选择以后对选框中的内容进行修改 ① Shift + 选框 为正方形 ② 选中后鼠标放在选框中对选择范围进行拖动 ③ 移动工具可以拉着选框中的内容移动 ④ ctrl+D取消选框 ⑤单行选 ...
- Quartz Scheduler(2.2.1) - Working with TriggerListeners and JobListeners
TriggerListeners and JobListeners Listeners are objects that you create to perform actions based on ...
- js限制文本框输入字数
//js代码 <script type="text/javascript"> function checkLen(term){ document.all.termLen ...
- 软件包 java.util 的分层结构
概述 软件包 类 使用 树 已过时 索引 帮助 JavaTM Platform Standard Ed. 6 上一个 下一个 框架 无框架 所有类 ...
- Android之进度条2
我之前有写过一篇“Android之进度条1”,那个是条形的进度条(显示数字进度),这次实现圆形进度条. 点击查看Android之进度条1:http://www.cnblogs.com/caidupin ...
- TSQL基础(四) - 日期处理
日期类型-DateTime DateTime是sql中最常用的日期类型. 存储大小为:8个字节: 日期范围:1753-01-01到9999-12-31: 精确度:3.33毫秒: 常用的日期函数 Get ...
- 检测SqlServer服务器内存是否瓶颈
性能监视器临视以下数据: Memory->Available MBytes 可用的内存 windows系统不低于1G,如果可用内存不多,则系统要求sqlserver释放内存 Paging F ...