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. [CTSC2018]混合果汁

    题目连接:https://www.luogu.org/problemnew/show/P4602 因为题中说是让最小值最大,所以自然想到二分答案.对于每一个二分的值,判断是否合法,若合法,在右区间二分 ...

  2. docker swarm英文文档学习-1-概述

    参考https://docs.docker.com/engine/swarm/ Swarm mode overview群模式概述 Docker的当前版本包括集群模式,用于本地管理称为集群的Docker ...

  3. zookeeper&acticemq&redis&tomcat安装

    zookeeper安装  配置hosts  下载 wget http://apache.fayea.com/zookeeper/zookeeper-3.4.6/zookeeper-3.4.6.tar. ...

  4. ASP.NET Core MVC 模型绑定 (转载)

    ASP.NET Core MVC的Model Binding会将HTTP Request数据,以映射的方式对应到参数中.基本上跟ASP.NET MVC差不多,但能Binding的来源更多了一些.本篇将 ...

  5. 20155229《网络对抗技术》Exp5:MSF基础应用

    实验内容 本实践目标是掌握metasploit的基本应用方式,重点常用的三种攻击方式的思路.具体需要完成: 一个主动攻击实践,如ms08-067; 一个针对浏览器的攻击,如ms11-050: 一个针对 ...

  6. 2017-2018-2 20155315《网络对抗技术》免考五:Windows提权

    原理 使用metasploit使目标机成功回连之后,要进一步攻击就需要提升操作权限.对于版本较低的Windows系统,在回连的时候使用getsystem提权是可以成功的,但是对于更高的系统操作就会被拒 ...

  7. Markdown 入门指南

    导语: Markdown是一种轻量级的标记语言,语法简单,学习成本不算太高,但确实可以让你专注于文字,不用太分心与排版等等. Markdown 官方文档 这里可以看到官方的Markdown语法规则: ...

  8. TICTOC: Header Only C++ Timer

    感觉最近的更新频率略高啊-哈哈- 这次的带来的是一个十分简单便利的C++计时库. 项目地址:https://github.com/miaoerduo/tictoc 欢迎Start和提MR. 项目中有详 ...

  9. npm install —— 从一个简单例子,看本地安装与全局安装的区别

    npm的包安装分为本地安装(local).全局安装(global)两种,从敲的命令行来看,差别只是有没有-g而已,比如 npm install grunt # 本地安装 npm install -g ...

  10. Scrapy持久化存储

    基于终端指令的持久化存储 保证爬虫文件的parse方法中有可迭代类型对象(通常为列表or字典)的返回,该返回值可以通过终端指令的形式写入指定格式的文件中进行持久化操作; 执行输出指定格式进行存储:将爬 ...