1030 Travel Plan(甲级)

1030 Travel Plan (30分)
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
只使用dijkstra

#include<iostream>
#include<memory.h>
#include<vector>
#include<algorithm>
using namespace std;
int n, m, st, ed;
const int inf = 0x3f3f3f3f;
const int maxn = 501;//n为顶点数,m为边数,st和ed分别为起点和终点
//g为矩阵,weight为花费
//d记录最短距离,w记录最小花费
int g[maxn][maxn]{inf};
int weight[maxn][maxn];
int w[maxn];
int d[maxn];
bool vist[maxn]{ false };
int path[maxn];//记录父节点
void dijistra(int s)
{fill(w, w+maxn, inf);fill(d, d + maxn, inf);for (int i = 0; i < n; i++)path[i] = i;d[s] = 0;w[s] = 0;for (int i = 0; i < n; i++){int u = -1;int min = inf;for (int j = 0; j < n; j++){if (!vist[j] && d[j] < min){min = d[j];u = j;}}if (u == -1)return;vist[u] = true;for (int j = 0; j < n; j++){if (!vist[j] && g[u][j] != inf){if (d[j] > g[u][j] + d[u]){d[j] = g[u][j] + d[u];w[j] = weight[u][j] + w[u];path[j] = u;}else if (d[j] == g[u][j] + d[u]){if (w[j] > weight[u][j] + w[u]){w[j] = weight[u][j] + w[u];path[j] = u;}}}}}
}
void dfs(int v)
{if (v == st){cout << v << " ";return;}dfs(path[v]);cout << v<<" ";return;
}
int main()
{cin >> n >> m >> st >> ed;fill(g[0], g[0] + maxn * maxn, inf);for (int i = 0; i < m; i++){int u, v;cin >> u >> v;cin >> g[u][v];cin >> weight[u][v];g[v][u] = g[u][v];weight[v][u] = weight[u][v];}dijistra(st);dfs(ed);cout << d[ed]<<" ";cout << w[ed];
}

dijkstra+dfs

#include<iostream>
#include<memory.h>
#include<vector>
using namespace std;
int n, m, st, ed;
const int inf = 0x3f3f3f3f;
const int maxn = 501;//最大顶点数//n为顶点数,m为边数,st和ed分别为起点和终点
//g为矩阵,weight为花费
//d记录最短距离;mincost记录最短路径上的最小花费
int g[maxn][maxn]{inf};
int weight[maxn][maxn];
int mincost = inf;
int d[maxn];
bool vist[maxn]{ false };
vector<int>path[maxn];//前驱
vector<int>temp,path_n;//临时路径和最优路径
void dijistra(int s)
{fill(d, d + maxn, inf);d[s] = 0;for (int i = 0; i < n; i++){int u = -1;int min = inf;for (int j = 0; j < n; j++){if (!vist[j] && d[j] < min){min = d[j];u = j;}}if (u == -1)return;vist[u] = true;for (int j = 0; j < n; j++){if (!vist[j] && g[u][j] != inf){if (d[j] > g[u][j] + d[u]){d[j] = g[u][j] + d[u];path[j].clear();path[j].push_back(u);}else if (d[j] == g[u][j] + d[u]){path[j].push_back(u);	}}}}
}
void dfs(int v)
{if (v == st){temp.push_back(v);int tempcost = 0;for (int i = temp.size() - 1; i > 0; i--){int id = temp[i];int idnext = temp[i - 1];tempcost += weight[id][idnext];}if (mincost > tempcost){mincost = tempcost;path_n = temp;}temp.pop_back();return;}temp.push_back(v);for (int i = 0; i < path[v].size(); i++){dfs(path[v][i]);}temp.pop_back();
}
int main()
{cin >> n >> m >> st >> ed;fill(g[0], g[0] + maxn * maxn, inf);memset(weight, 0, sizeof(weight));for (int i = 0; i < m; i++){int u, v;cin >> u >> v;cin >> g[u][v];cin >> weight[u][v];g[v][u] = g[u][v];weight[v][u] = weight[u][v];}dijistra(st);dfs(ed);for (int i =path_n.size()-1; i >= 0; i--){cout << path_n[i] << " ";}cout << d[ed]<<" "<<mincost;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/497009.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

perl里面隐式的继承

perl里面隐式的继承隐式的继承今天看见某断代码: A.pmCODE:[Copy to clipboard]Package A;...sub test{print "test\n";}B.pmCODE:[Copy to clipboard]Package B;use A;B->test();觉得比较奇怪,B并未显式从A继承,何以能使用A的test函数最后发现A.pm里面重写了impo…

2017全球教育机器人行业研究报告(附PDF下载)

来源&#xff1a; 起点财经 概要&#xff1a;伴随着全球化市场激烈竞争及高科技日新月异的发展&#xff0c;美、日、韩、欧、中等国家逐渐将智能机器人作为战略新兴产业发展不可缺少的创新技术支撑。伴随着全球化市场激烈竞争及高科技日新月异的发展&#xff0c;美、日、韩、…

Python编程从入门到实践~操作列表~创建数值列表

使用函数range() 使用range() 创建数字列表 对数字列表进行统计 列表解析

从零开始——PowerShell应用入门(全例子入门讲解)

From&#xff1a;https://www.cnblogs.com/lavender000/p/6935589.html PowerShell 在线教程&#xff1a;https://www.pstips.net/powershell-online-tutorials 微软 PowerShell 官方文档&#xff1a;https://docs.microsoft.com/zh-cn/powershell/ 学习一门技术&#xff0c;…

网站运作的几个阶段

作者&#xff1a;徐湘涛 看不见&#xff0d;&#xff0d;是因为你的网站还小&#xff0c;小到你的客户与业内人士都不知道&#xff0c;看不见你的存在&#xff0c;这个时候&#xff0c;是你最开始的时期&#xff0c;也是最为艰难的时刻&#xff0c;你要做的就是想办法提升品牌…

时空大数据赋能智慧城市的思考和实践

来源&#xff1a; 超图集团时空大数据和新型智慧城市是当下地信产业的两大热词&#xff0c;这两者的奇妙关联将擦出怎样精彩的火花&#xff1f;时空大数据如何赋能城市智能与城市智慧&#xff0c;让智慧城市建设迈上新的高度&#xff1f;2月6日至7日&#xff0c;由中国地理信息…

linux 命令:nc、netcat、ncat、socat

参考 &#xff1a;http://www.linuxso.com/command/nc.html NC工具的使用说明教程&#xff1a;https://blog.csdn.net/xysoul/article/details/52270149 window 版本 nc 下载&#xff1a;https://eternallybored.org/misc/netcat/ 1、nc、ncat 简介 NC 全名 Netcat (网络刀)&…

Python编程从入门到实践~操作列表

列表是什么 #列表是什么 bicycles ["trek", "cannodale", "redline","specialized"] print(bicycles)#访问列表元素 print(bicycles[0])#使用列表中的各个值 message f"My first bicycle was a {bicycles[0].title()}" p…

关于GPS 车辆定位导航中的投影变换

GPS 采用 WGS-84 椭球地理坐标, 用经、纬度和大地系来表示3 维空间信息。因此,GPS 车辆定位导航监控中心接收到的只是经、纬度信息, 必须通过高斯投影将其转换成高斯坐标。转换公式如下:转载于:https://www.cnblogs.com/kaixin110/archive/2007/12/11/990851.html

Linux下查看系统版本号信息的方法

From&#xff1a;https://linux.cn/article-9586-1.html 如果你加入了一家新公司&#xff0c;要为开发团队安装所需的软件并重启服务&#xff0c;这个时候首先要弄清楚它们运行在什么发行版以及哪个版本的系统上&#xff0c;你才能正确完成后续的工作。作为系统管理员&#xff…

Python编程从入门到实践~if语句

#检查是否相等 cars ["bmw", "audi", "toyota", "subaru"] for car in cars:if car bmw:print(car.upper())else:print(car.title())#忽略大小写是否相等 car "Audi" if (car.lower() "audi"):print(True)#检…

人工智能的恶意用途:预测、预防和缓解

来源&#xff1a; 新浪科技编译一份由26名专家联合撰写的报告&#xff0c;对人工智能技术的潜在威胁发出警告。他们认为&#xff0c;这项技术可能在未来5到10年催生新型网络犯罪、实体攻击和政治颠覆。这份100页的报告标题为《人工智能的恶意用途&#xff1a;预测、预防和缓解》…

CSS使用总结

在分配ID和类名时&#xff0c;尽可能保持与表现形式无关&#xff0c;例如contleft有可能以后希望出现在右边。 尽量少使用类,因为可以层叠识别,如: .News h3而不必在h3上加类<div class”News”> <h3></h3> <h2></h2> <p></p> <…

Linux下history命令详解---转载

Linux下History命令主要用于显示历史指令记录内容, 下达历史纪录中的指令 。 >History命令语法&#xff1a; [www.linuxidc.comlinux]# history [n] [www.linuxidc.comlinux]# history [-c] [www.linuxidc.comlinux]# history [-raw] histfiles 参数&#xff1a; n &#x…

linq 关联查询

可得会在以后的实体类中能用到usingSystem;usingSystem.Collections;usingSystem.Configuration;usingSystem.Data;usingSystem.Linq;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.HtmlControls;usingSystem.Web.UI.WebControls;usingSyst…

C++学习之路 | PTA乙级—— 1025 反转链表 (20分)(精简)

1025 反转链表 (25 分) 给定一个常数 K 以及一个单链表 L&#xff0c;请编写程序将 L 中每 K 个结点反转。例如&#xff1a;给定 L 为 1→2→3→4→5→6&#xff0c;K 为 3&#xff0c;则输出应该为 3→2→1→6→5→4&#xff1b;如果 K 为 4&#xff0c;则输出应该为 4→3→2→…

DeepMind提出「心智神经网络ToMnet」,训练机器的「理解」能力

原文来源&#xff1a;arXiv作者&#xff1a;Neil C. Rabinowitz、Frank Perbet、H. Francis Song、Chiyuan Zhang、S. M. Ali Eslami、Matthew Botvinick「雷克世界」编译&#xff1a;嗯~阿童木呀、KABUDA一般来说&#xff0c;心智理论&#xff08;ToM&#xff0c;Premack和Woo…

Python编程从入门到实践~字典

使用字典 #一个简单的字典 alien_0 {color: green, points: 5}#访问字典中的值 print(alien_0[color])#添加键值对 alien_0[x_position] 4 alien_0[y_position] 33 print(alien_0)#修改字典中的值 alien_0[color] red print(alien_0)#删除键值对 del alien_0[points] prin…

redis中使用redis-dump导出、导入、还原数据实例

使用redis-dump进行Redis数据库合并&#xff1a;https://www.cnblogs.com/jasondan/p/4031399.html Redis-Dump安装及使用&#xff1a;https://www.jianshu.com/p/19b5e7b3bffb Redis数据导出导入&#xff0d;redis-dump and redis-load&#xff1a;https://blog.csdn.net/html…

设计模式系列漫谈之一 - 观察者模式

此文为转载,地址: http://yuyijq.cnblogs.com/ 故事 小雪是一个非常漂亮的女孩&#xff0c;漂亮的女孩总是有很多的追求者&#xff0c;而且追求者的队伍在不断的变动&#xff0c;随时有人进入这个队伍&#xff0c;也有人退出。男孩们追求女孩时总是表现出120%的关心&#xff0…