PAT (Advanced Level) Practice 1030 Travel Plan (30分)
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
多源最短路板子题,但是带了一点PAT的一贯作风,要求在最短路中再找一条花费最小的路径,因为之前找的最短路是路径最短且不唯一,现在要你找出这些最短路中花费最小的路径
那还能怎么办呢,上dijkstra板子,但要做一点修改,在松弛步骤中,如果找到路径相同的,比较其花费,但是如果找到路径小的直接将花费改成该条路径的,因为路径长度被考虑优先级最高,算是30分中比较中规中矩的一题吧~
#include<bits/stdc++.h>
#define inf 2147483647
#define ll long long
#define rg register ll
using namespace std;
ll n,m,s,d;
ll vis[505],dis[505],fee[505],val[505][505],cost[505][505],path[505];
inline void work()
{
while(!vis[d])
{
ll v=-1,minn=inf;
for(rg i=0;i<n;i++)
{
if(!vis[i]&&dis[i]<minn)
{
v=i,minn=dis[i];
}
}
if(!(~v))return ;
vis[v]=1;
for(rg i=0;i<n;i++)
{
if(!vis[i]&&dis[v]+val[v][i]<dis[i])
{
dis[i]=dis[v]+val[v][i];
fee[i]=fee[v]+cost[v][i];
path[i]=v;
}
if(!vis[i]&&dis[v]+val[v][i]==dis[i]&&fee[v]+cost[v][i]<fee[i])
{
fee[i]=fee[v]+cost[v][i];
path[i]=v;
}
}
}
}
inline void display(ll x)
{
if(x==s)cout<<s;
else
{
display(path[x]);
cout<<" "<<x;
}
}
int main()
{
cin>>n>>m>>s>>d;
fill(dis,dis+505,inf);
fill(fee,fee+505,inf);
fill(val[0],val[0]+505*505,inf);
fill(cost[0],cost[0]+505*505,inf);
for(rg i=0;i<m;i++)
{
ll a,b,c,d;
cin>>a>>b>>c>>d;
val[a][b]=val[b][a]=c,cost[a][b]=cost[b][a]=d;
}
dis[s]=0,fee[s]=0;
work();
display(d);
cout<<" "<<dis[d]<<" "<<fee[d]<<endl;
//while(1)getchar();
return 0;
}
- JS 评分五角星随鼠标移动显示
- Golang标准库学习——buffio包 ---转
- 【Go 语言社区】Go语言条件变量的两个例子
- mysqlimport导入报错的排查(r10笔记第58天)
- 【Go 语言社区】POJ 1047 Round and Round We Go 循环数新解
- 【Go 语言社区】删除redis所有KEY
- 【Go 语言社区】Golang 动态实例化结构体
- 【Go 语言社区】Go 错误处理
- 【Go 语言社区】Go 语言范围(Range)
- 【Go 语言社区】JS 相关---Window Location
- 【Go 语言社区】Go 语言Map(集合)
- 【Go 语言社区】JavaScript Date(日期)对象
- UWP基础教程 - XAML类型转换器
- Oracle 12c Data Guard搭建(一) (r10笔记第57天)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- jdk8安装及环境变量配置
- 使用MA Anderson御用软件SpliceSeq对TCGA数据库的RNA-seq找可变剪切
- Tomcat9安装配置、服务配置开机自启动以及启动窗口的中文乱码问题解决
- Java的System.exit()详解
- Hadoop伪分布式搭建(hadoop2.x通用)
- R语言tryCatch使用方法:判断Warning和Error
- leetcode1546题解【前缀和+贪心】
- leetcode560题解【前缀和+哈希】
- 5秒解决:VMware Workstation 与 Hyper-V 不兼容
- Java的访问控制符详解(结合代码演示)
- Python贪吃蛇小游戏_完整源码免费分享
- GitHub修改昵称和用户名(图解详细教程)
- Python飞机大战小游戏_完整源码免费分享
- Linux求助命令
- Linux关机命令及步骤