[图算法] 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行输入一条边的起点,终点,通过时间和通过花费.求花费最小的最短路,输入这条路径包含起点终点,通过时间和通过花费 ...
随机推荐
- 【原创】CRM 2015/2016,SSRS 生成PDF文件,幷以附件的形式发送邮件
主要步骤如下: 生成一条邮件记录 生成一条ActivityParty记录 生成PDF文件,并以Base64添加到ActivityMimeAttachment 中去 打开发送邮件窗口,以便编辑及发送邮件 ...
- Docker 运行时的用户与组管理的方法
docker 以进程为核心, 对系统资源进行隔离使用的管理工具. 隔离是通过 cgroups (control groups 进程控制组) 这个操作系统内核特性来实现的. 包括用户的参数限制. 帐户管 ...
- Python 1.3 元组与文件
一 Python元组Tuple类型 元组T= (1, 2, 3, 4)是不可变类型,属于序列,但顶层元素不可变,仅支持count()和index()操作. -*- coding:UTF- -*- # ...
- leetcode记录-字符串转整数
实现 atoi,将字符串转为整数. 该函数首先根据需要丢弃任意多的空格字符,直到找到第一个非空格字符为止.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字 ...
- IDEA 通过插件jetty-maven-plugin使用 jetty
jetty:run -Djetty.port=8080 pom.xml配置 <build> <plugins> <plugin> <groupId>or ...
- 20155213 实验一《Java开发环境的熟悉》实验报告
20155213 实验一<Java开发环境的熟悉>实验报告 一. 实验内容及步骤 (一)使用JDK编译.运行简单的java程序 命令行下的程序开发 输入cd Code进入Code文件夹里 ...
- 20155304 2016-2017-2 《Java程序设计》实验五(网络编程与安全)实验报告
20155304 2016-2017-2 <Java程序设计>实验五(网络编程与安全)实验报告 实验内容及步骤: 一.两人一组结对编程: 参考http://www.cnblogs.com/ ...
- SublimeText 改变 tab的距离
view -> Indentation -> Tab width ……
- sql语句-7-更新数据
- day1 Ubuntu 使用
ctrl + shift + + 放大终端 ctrl + - 缩小终端 软连接,硬链接 ln python@ubuntu:~/Desktop$ vim .txt python@ubuntu ...