当前位置:网站首页>1030 Travel Plan
1030 Travel Plan
2022-06-27 20:47: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 40Note the array used to store the path pre Stored should be a precursor , Should use the dfs Traverse the output , Otherwise, the test point fails .
#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];// Record the shortest path Forerunner
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;
}
边栏推荐
- 海量数据出席兰州openGauss Meetup(生态全国行)活动,以企业级数据库赋能用户应用升级
- SQL audit platform permission module introduction and account creation tutorial
- 一段时间没用思源,升级到最新的 24 版后反复显示数据加密问题
- [STL programming] [common competition] [Part 3]
- 最佳实践:优化Postgres查询性能(下)
- pfSense Plus22.01中文定制版发布
- [required reading for high-quality products] sub query of Oracle database in Linux system
- Abap-sm30 check before deletion
- 抗洪救灾,共克时艰,城联优品驰援英德捐赠爱心物资
- Necessary software tools in embedded software development
猜你喜欢

“好声音“连唱10年,星空华文如何唱响港交所?

Cloud native Security Guide: learn kubernetes attack and defense from scratch

元宇宙虚拟数字人离我们更近了|华锐互动

谈谈我写作生涯的画图技巧

Practice of combining rook CEPH and rainbow, a cloud native storage solution

Linux system Oracle 19C OEM monitoring management

Select auto increment or sequence for primary key selection?

Postman 汉化教程(Postman中文版)

SQL audit platform permission module introduction and account creation tutorial

Type the URL to the web page display. What happened during this period?
随机推荐
云原生存储解决方案Rook-Ceph与Rainbond结合的实践
Enumeration and control flow operation in rust
openssl客户端编程:一个不起眼的函数导致的SSL会话失败问题
308. 2D area and retrieval - variable segment tree / hash
muduo
Hash table - Review
SQL审核平台权限模块介绍和账号创建教程
基于微信小程序的警局报案便民服务平台#毕业设计
After kotlin wechat payment callback, the interface is stuck and uipagefragmentactivity windowleft is thrown
Model reasoning acceleration based on tensorrt
“好声音“连唱10年,星空华文如何唱响港交所?
SQL报了一个不常见的错误,让新来的实习生懵了
College graduation thesis management system based on wechat applet graduation design
Redis cluster
Pycharm common functions - breakpoint debugging
数据库索引
爱数课实验 | 第七期-基于随机森林的金融危机分析
Unity3D Button根据文本内容自适应大小
云原生安全指南: 从零开始学 Kubernetes 攻防
Mongodb introduction and typical application scenarios