[图算法] 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教程(四)·序列
距离上次的小项目已经休息了很长一段时间,是时候来继续本系列教程了.这一节开始我们将深入python中的数据结构. 序列的概念 在python中,最基本的数据结构是序列,序列包含一个或多个元素,每个元素 ...
- Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符
/* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...
- PTA(BasicLevel)-1007素数对猜想
一 问题描述-素数对 让我们定义素数差dn为:dn=pn+1−pn,其中pi是第i个素数.显然有d1=1,且对于n>1有dn是偶数.“素数对猜想”认为“ ...
- Java动态代理代码快速上手
动态代理的两个核心的点是:代理的行为 和 代理机构. 举个例子,上大学的时候,很多同学吃午饭的时候都是叫别人带饭,有一个人H特别热心肠,想了一个办法,他在门口挂了个公示牌,每天有谁想要找人带饭就写公告 ...
- scala (5) 可变序列和不可变序列
/** * 序列分为可变长和不可变长,序列其实就是list,底层是链表结构 * 特点:插入有序,可重复,增加和移除元素很快,查询慢 * 不可变长序列:List * 可变长序列:ListBuffer * ...
- 20145226夏艺华 《Java程序设计》实验报告二
实验二 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验步骤 (一)单元测试 ...
- 20145234黄斐《Java程序设计》实验二—Java面向对象程序设计
1.提交最后三个测试用例都通过的截图,截图上要有画图加水印,输入自己的学号. 2. 以 TDD的方式研究学习StringBuffer 3.对设计模式示例进行扩充,体会OCP原则和DIP原则的应用,初步 ...
- 关于正则表达式"\b"
今天刚刚开始看正则表达式就遇到一个十分头疼的问题,原文是这样的: “不幸的是,很多单词里包含hi这两个连续的字符,比如him,history,high等等.用hi来查找的话,这里边的hi也会被找出来. ...
- sql心经
问题: 查数据是一件很痛苦的事,尤其是多张表链接查询更是惨不忍睹 各种条件拼接,各种查询数据不对,看着写了半天的sql,感觉很完美,没毛病啊... 分析: http://blog.jobbole.co ...
- 简单的Slony-I设置实例 II
磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面: PostgreSQL集群方案相关索引页 回到顶级页面:PostgreSQL索引页 接前面例子, 简单的Slony-I设置实例 这次我 ...