当前位置:网站首页>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;
}
边栏推荐
- shell脚本常用命令(三)
- 使用logrotate对宝塔的网站日志进行自动切割
- Is Guosen Securities a state-owned enterprise? Is it safe to open an account with Guosen Securities?
- 广发期货开户安全吗?
- 国际数字经济学院、华南理工 | Unified BERT for Few-shot Natural Language Understanding(用于小样本自然语言理解的统一BERT)
- 新中大冲刺科创板:年营收2.84亿 拟募资5.57亿
- 在线文本按行批量反转工具
- One to one relationship
- Where to look at high-yield bank financial products?
- Don't worry. This is the truth about wages in all industries in China
猜你喜欢
从感知机到前馈神经网络的数学推导
Redis 原理 - String
Bit.Store:熊市漫漫,稳定Staking产品或成主旋律
Usage of rxjs mergemap
C# 二维码生成、识别,去除白边、任意颜色
TIA博途_基于SCL语言制作模拟量输入输出全局库的具体方法
明美新能源冲刺深交所:年应收账款超6亿 拟募资4.5亿
Keras deep learning practice (12) -- facial feature point detection
The IPO of Yuchen Airlines was terminated: Guozheng was proposed to raise 500million yuan as the major shareholder
binder hwbinder vndbinder
随机推荐
买股票在券商经理的开户链接上开户安全吗?求大神赐教
On thread safety
新中大冲刺科创板:年营收2.84亿 拟募资5.57亿
Blink SQL内置函数大全
Substrate及波卡一周技术更新速递 20220425 - 20220501
External interrupt experiment based on stm32f103zet6 library function
Comprehensively analyze the zero knowledge proof: resolve the expansion problem and redefine "privacy security"
Oracle 获取月初、月末时间,获取上一月月初、月末时间
广发期货开户安全吗?
xctf攻防世界 MISC薪手进阶区
【云驻共创】 什么是信息化?什么是数字化?这两者有什么联系和区别?
Keras deep learning practice (12) -- facial feature point detection
金源高端IPO被终止:曾拟募资7.5亿 儒杉资产与溧阳产投是股东
Online text batch inversion by line tool
DCC888 :Register Allocation
教你打印自己的日志 -- 如何自定义 log4j2 各组件
Redis 原理 - String
高收益银行理财产品在哪里看?
Market status and development prospect forecast of the global infusion needle less connector industry in 2022
工作流自动化 低代码是关键