PAT 甲级 1030 Travel Plan (30 分)(dijstra,较简单,但要注意是从0到n-1)
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); M is the number of highways; S and D are the starting and the destination cities, respectively. Then Mlines 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
题解:
第一行是四个数字,n,m,s,d,分别代表有n个城市,有m条路,起点城市s和终点城市d;
后面跟着m行数据,代表道路的情况,每一行四个数字,City1 City2 Distance Cost,让你求出来由起点到终点的最短的路径,如果距离相等时选择花费最小的道路,最后输出道路和最短的距离和花费;
这道题也是用dijkstra算法来进行求解,记得在最短路径的判断中加入对于花费的判断(即,当距离一样时,选择花费更小的路径);
对于路径的存储,用一个数组,每一次路径变化时,只需要存下来这个城市是由哪个城市来的,最后一个栈从终点城市回溯输出即可。
第一次没过:
没注意到城市是从0到n-1,我太粗心直接写成了1到n编号,后来才发现。
这道题没有重边的情况,但是要注意将来会不会遇到重边的特殊情况。
AC代码:
#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
int n,m,s,dis;
int e[][];//距离
int d[];//距离
int dc[];//花费
int ec[][];//花费
bool v[];//标记有没有遍历过
int p[];//记录路径
void dijstra(int s){
memset(v,,sizeof(v));
for(int i=;i<=n;i++){
d[i]=e[s][i];
dc[i]=ec[s][i];
p[i]=s;
}
v[s]=;
for(int i=;i<=n;i++){
int k=-;
int mi=inf;
for(int j=;j<=n;j++){
if(mi>d[j]&&!v[j]){
k=j;
mi=d[j];
}
}
if(k==-){
break;
}
v[k]=;
for(int j=;j<=n;j++){
if(d[j]>d[k]+e[k][j]){
d[j]=d[k]+e[k][j];
dc[j]=dc[k]+ec[k][j];
p[j]=k;
}else if(d[j]==d[k]+e[k][j]){
if(dc[j]>dc[k]+ec[k][j]){
dc[j]=dc[k]+ec[k][j];
p[j]=k;
} }
}
}
}
int main()
{
cin>>n>>m>>s>>dis;
s++;//我是以1-n编号
dis++;
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(i==j){
e[i][j]=ec[i][j]=;
}else{
e[i][j]=ec[i][j]=inf;
}
}
}
for(int i=;i<=m;i++){
int u,v,dd,cc;
cin>>u>>v>>dd>>cc;
u++;//我是以1-n编号
v++;
e[u][v]=e[v][u]=dd;
ec[u][v]=ec[v][u]=cc;
}
dijstra(s);
//输出
stack<int>sta;
while(!sta.empty()) sta.pop();
int pair=dis;
while(pair!=s){
sta.push(pair);
pair=p[pair];
}
cout<<s-<<" ";
while(!sta.empty()){
cout<<sta.top()-<<" ";
sta.pop();
}
cout<<d[dis]<<" "<<dc[dis];
return ;
}
PAT 甲级 1030 Travel Plan (30 分)(dijstra,较简单,但要注意是从0到n-1)的更多相关文章
- PAT 甲级 1072 Gas Station (30 分)(dijstra)
1072 Gas Station (30 分) A gas station has to be built at such a location that the minimum distance ...
- 【PAT甲级】1030 Travel Plan (30 分)(SPFA,DFS)
题意: 输入N,M,S,D(N,M<=500,0<S,D<N),接下来M行输入一条边的起点,终点,通过时间和通过花费.求花费最小的最短路,输入这条路径包含起点终点,通过时间和通过花费 ...
- PAT A 1030. Travel Plan (30)【最短路径】
https://www.patest.cn/contests/pat-a-practise/1030 找最短路,如果有多条找最小消耗的,相当于找两次最短路,可以直接dfs,数据小不会超时. #incl ...
- PAT 甲级 1030 Travel Plan
https://pintia.cn/problem-sets/994805342720868352/problems/994805464397627392 A traveler's map gives ...
- 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-1030 Travel Plan (30 分) 最短路最小边权 堆优化dijkstra+DFS
PAT 1030 最短路最小边权 堆优化dijkstra+DFS 1030 Travel Plan (30 分) A traveler's map gives the distances betwee ...
- [图算法] 1030. Travel Plan (30)
1030. Travel Plan (30) A traveler's map gives the distances between cities along the highways, toget ...
- PAT 甲级 1080 Graduate Admission (30 分) (简单,结构体排序模拟)
1080 Graduate Admission (30 分) It is said that in 2011, there are about 100 graduate schools ready ...
随机推荐
- BCB6 使用TZCompressionStream压缩
最近由于项目需要涉及到解压第三方公司的数据,在此做一下记录环境部署和使用方法,免得以后忘记. 对方公司的数据是通过TCompressionStream 压缩之后,存到数据库中,采用的 ...
- Java基础-异常处理机制 及异常处理的五个关键字:try/catch/finally/throw /throws
笔记: /** 异常处理机制: 抓抛模型 * 1."抛", 一旦抛出,程序终止! printStackTrace()显示异常路径! * 2."抓", 抓住异常 ...
- ggplot2入门与进阶(下)
出处:http://www.cellyse.com/how_to_use_gggplot2_part2/ 更多实战 例一 Michaelis-Menten动力学方程 这个例子中采用出自文献中的一组有关 ...
- nginx中ngx_http_core_module模块
http核⼼心模块指令:套接字相关的配置3.1 server{ }设置虚拟服务器器的配置Syntax: server { ... }Default: —Context: httpserver {lis ...
- 甘特图控件如何自定义绘图?DevExpress Winforms帮你忙
DevExpress Winforms Controls 内置140多个UI控件和库,完美构建流畅.美观且易于使用的应用程序.无论是Office风格的界面,还是分析处理大批量的业务数据,DevExpr ...
- SSH自动断开处理
解决ssh登录后闲置时间过长而断开连接 时我们通过终端连接服务器时,当鼠标和键盘长时间不操作,服务器就会自动断开连接,我们还的需要重新连接,感觉很麻烦,总结一下解决此问题的方法 方法一. 修改/etc ...
- 我是如何理解Android的Handler模型_1
Handler Message类似于旧时的电话系统,对应关系如下: 电话局->Handler 电话机->Message 接线员->handlerMessage 接线员的工作-> ...
- 3、Spring Boot 2.x 核心技术
1.3 Spring Boot 核心技术 1.3.1 起步依赖 为项目的依赖管理提供帮助.起步依赖其实就是特殊的Maven,利用了传递依赖解析,把常用库聚合在一起,组成几个为特定功能而定制的依赖. 1 ...
- Codeforces Round #469 (Div. 1) 949C C. Data Center Maintenance (Div. 2 950E)
题 OvO http://codeforces.com/contest/949/problem/C codeforces 949C 950E 解 建图,记原图为 G1,缩点,记缩完点后的新图为G2 缩 ...
- 最小生成树&&次小生成树
对于一个边上具有权值的图来说,其边权值和最小的生成树叫做图G的最小生成树 求无向图最小生成树主要有prim和kruskal两种算法 1.prim 将点集V分成Va和Vb两部分,Va为已经连入生成树的点 ...