当前位置:网站首页>1030 Travel Plan
1030 Travel Plan
2022-06-27 17:56:00 【Brosto_Cloud】
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注意存储路径时使用的数组pre存储的应该是前驱,应该用dfs遍历输出,否则测试点一不通过。
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
struct edge {
int next, cost, dis;
};
vector<edge>g[1000];
int n, m, s, d, dis[1000], cost[1000];
int pre[1000];//记录最短路径 前驱
bool vis[1000];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>q;
void dfs(int x) {
if (x == s) {
cout << x << ' ';
return;
}
dfs(pre[x]);
cout << x << ' ';
}
int main() {
cin >> n >> m >> s >> d;
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
edge e;
cin >> e.dis >> e.cost;
e.next = v;
g[u].push_back(e);
e.next = u;
g[v].push_back(e);
}
memset(dis, 127, sizeof(dis));
memset(cost, 127, sizeof(cost));
dis[s] = 0;
cost[s] = 0;
q.push(make_pair(0, s));
while (!q.empty()) {
int u = q.top().second;
q.pop();
if (vis[u]) {
continue;
}
vis[u] = 1;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i].next;
if (dis[v] > dis[u] + g[u][i].dis) {
dis[v] = dis[u] + g[u][i].dis;
cost[v] = cost[u] + g[u][i].cost;
pre[v] = u;
q.push(make_pair(dis[v], v));
} else if (dis[v] == dis[u] + g[u][i].dis) {
if (cost[v] > cost[u] + g[u][i].cost) {
cost[v] = cost[u] + g[u][i].cost;
pre[v] = u;
q.push(make_pair(dis[v], v));
}
}
}
}
dfs(d);
cout << dis[d] << ' ' << cost[d];
return 0;
}
边栏推荐
- Four years of College for an ordinary graduate
- SQL Server - Window Function - 解决连续N条记录过滤问题
- On thread safety
- 信息学奥赛一本通 1335:【例2-4】连通块
- Market status and development prospect forecast of global functional polyethylene glycol (PEG) industry in 2022
- Character interception triplets of data warehouse: substrb, substr, substring
- Market status and development prospect forecast of global 4-methyl-2-pentanone industry in 2022
- Keras deep learning practice (12) -- facial feature point detection
- International School of Digital Economics, South China Institute of technology 𞓜 unified Bert for few shot natural language understanding
- Blink SQL built in functions
猜你喜欢

Core dynamic Lianke rushes to the scientific innovation board: with an annual revenue of 170million yuan, Beifang Electronics Institute and Zhongcheng venture capital are shareholders

实施MES管理系统前,要对哪些问题进行评估

binder hwbinder vndbinder

crontab的学习随笔

xctf攻防世界 MISC薪手进阶区

拥抱云原生:江苏移动订单中心实践

什么是 ICMP ?ping和ICMP之间有啥关系?

别焦虑了,这才是中国各行业的工资真相

指针和结构体

Bit. Store: long bear market, stable stacking products may become the main theme
随机推荐
Where to look at high-yield bank financial products?
Current market situation and development prospect forecast of global 3,3 ', 4,4' - biphenyltetracarboxylic dianhydride industry in 2022
CDGA|交通行业做好数字化转型的核心是什么?
Market status and development prospect forecast of global functional polyethylene glycol (PEG) industry in 2022
基于STM32F103ZET6库函数按键输入实验
基础数据类型和复杂数据类型
运算符的基础知识
流程判断-三目运算-for循环
C# 二维码生成、识别,去除白边、任意颜色
NVIDIA Clara-AGX-Developer-Kit installation
How to encapsulate and call a library
今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献
Is Guosen Securities a state-owned enterprise? Is it safe to open an account with Guosen Securities?
网络传输是怎么工作的 -- 详解 OSI 模型
你知道 log4j2 各项配置的全部含义吗?带你了解 log4j2 的全部组件
What is ICMP? What is the relationship between Ping and ICMP?
Informatics Orsay all in one 1335: [example 2-4] connected block
一种朴素的消失点计算方法
binder hwbinder vndbinder
《第五项修炼》(The Fifth Discipline):学习型组织的艺术与实践