https://pintia.cn/problem-sets/994805342720868352/problems/994805464397627392

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 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 4

代码:

#include <bits/stdc++.h>
using namespace std; #define inf 0x3f3f3f3f int N, M, S, D;
int mp[550][550], cost[550][550];
int dis1[550],dis2[550],dis3[550];
int vis[550];
int pre[550]; int MinStep, out = INT_MAX; void dijkstra(int S, int dis[]) {
dis[S] = 0;
memset(vis, 0, sizeof(vis));
int temp = S; for(int i = 0; i < N; i ++) {
int minn = inf;
for(int j = 0; j < N; j ++) {
if(dis[j] < minn && vis[j] == 0) {
minn = dis[j];
temp = j;
}
}
vis[temp] = 1;
for(int k = 0; k < N; k ++)
if(vis[k] == 0 && mp[temp][k] != inf) {
if(dis[k] > mp[temp][k] + dis[temp])
dis[k] = mp[temp][k] + dis[temp];
}
}
} void dijkstra(int S) { dis3[S] = 0;
memset(vis, 0, sizeof(vis));
int temp = S; for(int i = 0; i < N; i ++) {
int minn = inf;
for(int j = 0; j < N; j ++) {
if(dis3[j] < minn && vis[j] == 0) {
minn = dis3[j];
temp = j;
}
}
vis[temp] = 1;
for(int k = 0; k < N; k ++)
if(mp[temp][k] + dis1[temp] + dis2[k] == MinStep)
if(vis[k] == 0 && cost[temp][k] != inf)
if(dis3[k] > cost[temp][k] + dis3[temp]){
dis3[k] = cost[temp][k] + dis3[temp];
pre[k] = temp;
}
}
} void output(int d){
if(d==-1) return ;
output(pre[d]);
printf("%d ", d);
} int main() { memset(pre,-1, sizeof(pre));
memset(vis, 0, sizeof(vis));
memset(cost,inf, sizeof(cost)); memset(dis1, inf, sizeof(dis1));
memset(dis2, inf, sizeof(dis2));
memset(dis3, inf, sizeof(dis3)); memset(mp, inf, sizeof(mp)); scanf("%d%d%d%d", &N, &M, &S, &D);
for(int i = 0; i < M; i ++) {
int st, en, dist, val;
scanf("%d%d%d%d", &st, &en, &dist, &val);
mp[st][en] = mp[en][st] = min(dist, mp[en][st]);
cost[st][en] = cost[en][st] = min(val, cost[st][en]);
} dijkstra(S, dis1);
dijkstra(D, dis2); MinStep = dis1[D]; dijkstra(S); output(D);
printf("%d %d\n", dis1[D], dis3[D]);
return 0;
}

  两遍 dijkstra 一上午经历了无数遍点开题目又退出 枯了 睡一会清醒清醒再来写吧

PAT 甲级 1030 Travel Plan的更多相关文章

  1. 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 ...

  2. PAT A 1030. Travel Plan (30)【最短路径】

    https://www.patest.cn/contests/pat-a-practise/1030 找最短路,如果有多条找最小消耗的,相当于找两次最短路,可以直接dfs,数据小不会超时. #incl ...

  3. PAT甲级——A1030 Travel Plan

    A traveler's map gives the distances between cities along the highways, together with the cost of ea ...

  4. 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 ...

  5. PAT 1030 Travel Plan[图论][难]

    1030 Travel Plan (30)(30 分) A traveler's map gives the distances between cities along the highways, ...

  6. 1030 Travel Plan (30 分)

    1030 Travel Plan (30 分) A traveler's map gives the distances between cities along the highways, toge ...

  7. [图算法] 1030. Travel Plan (30)

    1030. Travel Plan (30) A traveler's map gives the distances between cities along the highways, toget ...

  8. 【PAT甲级】1030 Travel Plan (30 分)(SPFA,DFS)

    题意: 输入N,M,S,D(N,M<=500,0<S,D<N),接下来M行输入一条边的起点,终点,通过时间和通过花费.求花费最小的最短路,输入这条路径包含起点终点,通过时间和通过花费 ...

  9. PAT (Advanced Level) 1030. Travel Plan (30)

    先处理出最短路上的边.变成一个DAG,然后在DAG上进行DFS. #include<iostream> #include<cstring> #include<cmath& ...

随机推荐

  1. yarn的学习-2-从 npm 迁移到 yarn-包管理工具

    从npm处迁移过来多许多用户来说是一个相对简单的过程.yarn能想npm一样定制相同的package.json,并能够从npm仓库下载任意的包 如果你想要在已存在的npm项目中使用yarn,运行yar ...

  2. impala客户端连接

    想要使用plsql连接oracle一样,使用类似工具连接impala的方法:ClouderaImpalaODBC32.msi下载地址:http://www.cloudera.com/downloads ...

  3. android camera 摄像头预览画面变形

    问题:最近在处理一下camera的问题,发现在竖屏时预览图像会变形,而横屏时正常.但有的手机则是横竖屏都会变形. 结果:解决了预览变形的问题,同时支持前后摄像头,预览无变形,拍照生成的jpg照片方向正 ...

  4. Drupal性能优化:蜜蜂培训性能优化一

    大家一直都说Drupal的性能不怎么样,跑起来慢,即使不是在用户量大的时候,最近公司的蜜蜂培训产品在一个客户的使用过程中,由于用户量及数据量的激增,就遇到了比较大的性能问题,这篇文章就记录了整个优化过 ...

  5. 基于Python自动上传包到nexus仓库

    1.设计思路 用户通过excel表格的形式填写包的信息,并将包一起发送给负责人 2.代码实现 #coding:utf8 import os import xlrd def GetData(fileNa ...

  6. THUSC 2017 D1T2 杜老师

    这是个非常有趣的数学题啦... 其实大概推一推式子就能得到一个信息,就是答案一定是$2$的整数次幂,并且其实答案就是$2^{R-L+1-sum}$,其中$sum$表示有多少个数不能用$L-i-1$的数 ...

  7. BZOJ2427: [HAOI2010]软件安装 tarjan+树形背包

    分析: 一开始我以为是裸的树形背包...之后被告知这东西...可能有环...什么!有环! 有环就搞掉就就可以了...tarjan缩点...建图记得建立从i到d[i]之后跑tarjan,因为这样才能判断 ...

  8. C++STL之Vector的应用

    这是我第一次写博客,请多指教! vector是一种向量容器,说白了就是可以改变大小的数组. vector是一个模板类,如果直接这样会报错: vector a; //报错,因为要指定模板. 需要像这样: ...

  9. Python3入门(三)——Python基础语法

    一.基本语法 1.行和缩进 Python中,不使用括号来表示代码的类和函数定义块或流程控制. 代码块是由行缩进,缩进位的数目是可变的,但是在块中的所有语句必须缩进相同的量. 如下所示: a = 100 ...

  10. MAC下配置ssh让SourceTree通过秘钥访问远程仓库

    问题描述 由于TortoiseGit没有MAC版本,我们使用了SourceTree来替代. 在帮同事解决Mac下的Git的时候,碰到一个问题:SourceTree无法使用ssh方式提交代码,这是由于没 ...