[图算法] 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
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std; const int maxn=;
const int INF=1e9; int n,m,s,den;
int G[maxn][maxn],cost[maxn][maxn];
int d[maxn];
int c[maxn];
bool vis[maxn];
int pre[maxn]; void dijstra(int begin)
{
fill(d,d+maxn,INF);
fill(c,c+maxn,INF);
fill(vis,vis+maxn,false);
for(int i=;i<n;i++) pre[i]=i;
d[begin]=;
c[begin]=;
pre[begin]=begin;
for(int i=;i<n;i++)
{
int u=-,MIN=INF;
for(int j=;j<n;j++)
{
if(vis[j]==false&&d[j]<MIN)
{
u=j;
MIN=d[j];
}
}
if(u==-) return ;
vis[u]=true;
for(int v=;v<n;v++)
{
if(vis[v]==false&&G[u][v]!=INF)
{
if(d[u]+G[u][v]<d[v])
{
d[v]=d[u]+G[u][v];
c[v]=c[u]+cost[u][v];
pre[v]=u;
}
else if(d[u]+G[u][v]==d[v])
{
if(c[u]+cost[u][v]<c[v])
{
c[v]=c[u]+cost[u][v];
pre[v]=u;
}
}
}
}
}
} void printPath(int k)
{
if(k==s)
{
printf("%d ",k);
return ;
}
printPath(pre[k]);
printf("%d ",k);
} int main()
{
cin>>n>>m>>s>>den;
int city1,city2,dist,co;
fill(G[],G[]+maxn*maxn,INF);
for(int i=;i<m;i++)
{
cin>>city1>>city2>>dist>>co;
G[city1][city2]=dist;
G[city2][city1]=dist;
cost[city1][city2]=co;
cost[city2][city1]=co;
}
dijstra(s);
printPath(den);
cout<<d[den]<<" "<<c[den]<<endl;
}
[图算法] 1030. Travel Plan (30)的更多相关文章
- 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)
时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A traveler's map gives the dista ...
- 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行输入一条边的起点,终点,通过时间和通过花费.求花费最小的最短路,输入这条路径包含起点终点,通过时间和通过花费 ...
随机推荐
- Python学习:20.Python网络编程(Socket)
一.Socket介绍 我们知道两个进程如果需要进行通讯,最基本的一个前提是能够唯一标示一个进程.在本地进程通讯中可以使用PID来唯一标示一个进程,但PID只在本地唯一,网络中的两个进程PID冲突几率很 ...
- windows提权之前的信息收集
0x00 基本信息 -获取主机名:hostname或者echo %COMPUTERNAME% -获取所属域信息:systeminfo 获取环境变量:set 0x01 获取系统安装的软件信息 -导出注册 ...
- Java学习笔记三十:Java小项目之租车系统
Java小项目之租车系统 一:项目背景介绍: 根据所学知识,编写一个控制台版的“呱呱租车系统” 功能: 1.展示所有可租车辆: 2.选择车型.租车量: 3.展示租车清单,包含:总金额.总载货量以及其车 ...
- 开了几天的phpmyadmin的总结
近来无事,免费体验了一波腾讯云的vps,打了一个phpstudy,全部默认的配置,只不过,没有给他写入文件的权限 开启了日志,看了下,这几天黑客们的活动 首先,有两三个ip来爆破我的phpmyadmi ...
- C服务程序模板
在DoWork中添加自己的代码. 服务安装: sc create srvdemo binpath= "F:\srvdemo.exe" 服务启动:sc start srvdemo ...
- Altera三速以太网IP核使用(下篇)--- 百兆网接口设计与使用
MAC IP核的主要作用是:实现数据链路层协议,分为TX方向与RX方向,TX方向实现的是在原包文的前面加上7个55和1个D5,RX方向则相反.在使用这个 MAC IP核之前,首先确认下自己使用的网卡是 ...
- NoSQL入门第一天——NoSQL入门与基本概述
一.课程大纲 二.入门概述 1.为什么用NoSQL 单机MySQL的年代: 一个网站的访问量一般都不大,用单个数据库完全可以轻松应付. 我们来看看数据存储的瓶颈是什么? 1.数据量的总大小 一个机器放 ...
- 20155307 2017-2018-3 《Java程序设计》第3周学习总结
20155307 2017-2018-3 <Java程序设计>第3周学习总结 教材学习内容总结 类相当于是设计图,对象是根据类设计出来的.用class定义,名字叫clothes.可以用ne ...
- 20155325 2016-2017-2 《Java程序设计》第1周学习总结
教材学习内容总结 三大平台 java SE:JVM(java虚拟机),JRE(运行java程序),JDK(包括JRE及开发过程中需要的一些工具程序),java语言. java EE:基于java SE ...
- windows查看系统版本号
win+R,输入cmd,确定,打开命令窗口,输入msinfo32,注意要在英文状态下输入,回车.然后在弹出的窗口中就可以看到系统的具体版本号了. win+R,输入cmd,确定,打开命令窗口,输入v ...