当前位置:网站首页>L3-007 ladder map (test point 2 is stuck, you can see it)
L3-007 ladder map (test point 2 is stuck, you can see it)
2022-08-08 04:10:00 【Cod_ing】
一道Dijkstra的模板题,关于测试点2The problem that fails is that the additional requirements for the output of the question are not considered:
如果最快The route to reach is not unique,Then output several fastest routes最短的那条,题目保证这条路线是唯一的.而如果最短The route of distance is not unique,the output path节点The one with the least number,题目保证这条路线是唯一的.
1、If there are multiple results with the shortest time consumption,Then choose the shortest path length.
2、If there are multiple results with the shortest path,Then choose the one with the fewest nodes.
本题要求你实现一个天梯赛专属在线地图,队员输入自己学校所在地和赛场地点后,该地图应该推荐两条路线:一条是最快到达路线;一条是最短距离的路线.题目保证对任意的查询请求,地图上都至少存在一条可达路线.
输入格式:
输入在第一行给出两个正整数N(2 ≤ N ≤ 500)和M,分别为地图中所有标记地点的个数和连接地点的道路条数.随后M行,每行按如下格式给出一条道路的信息:
V1 V2 one-way length time
其中V1和V2是道路的两个端点的编号(从0到N-1);如果该道路是从V1到V2的单行线,则one-way为1,否则为0;length是道路的长度;time是通过该路所需要的时间.最后给出一对起点和终点的编号.
输出格式:
首先按下列格式输出最快到达的时间T和用节点编号表示的路线:
Time = T: 起点 => 节点1 => … => 终点
然后在下一行按下列格式输出最短距离D和用节点编号表示的路线:
Distance = D: 起点 => 节点1 => … => 终点
如果最快The route to reach is not unique,Then output several fastest routes最短的那条,题目保证这条路线是唯一的.而如果最短The route of distance is not unique,the output path节点The one with the least number,题目保证这条路线是唯一的.
如果这两条路线是完全一样的,则按下列格式输出:
Time = T; Distance = D: 起点 => 节点1 => … => 终点
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int MaxSize = 1e5 + 5;;
struct Edge {
int v, len, tm;
};
vector<Edge> E[505];
vector<int> ans[2];
int Ans[2];
bool vis[505];
int dis[505];
int cnt[505];
int pre[505];
void init() {
memset(vis, false, sizeof(vis));
memset(dis, 0x3f, sizeof(dis));
memset(cnt, 0, sizeof(cnt));
memset(pre, -1, sizeof(pre));
}
void Dijkstra_len(int v1, int v2) {
init();
dis[v1] = 0;
queue<int> Q;
int v, u,w;
Q.push(v1);
while (!Q.empty()) {
v = Q.front(); Q.pop();
if (vis[v]) continue;
vis[v] = true;
for (int i = 0; i < E[v].size(); i++) {
u = E[v][i].v, w = E[v][i].len;
if (dis[u] > dis[v] + w) {
Q.push(u);
dis[u] = dis[v] + w;//dis[x]表示起点到x的路程
cnt[u] = cnt[v] + 1;//cnt[x]表示起点到x的节点数
pre[u] = v;
}
else if (dis[u] == dis[v] + w && cnt[v] + 1 < cnt[u]) {
cnt[u] = cnt[v] + 1;
pre[u] = v;
}
}
}
int ends = v2;
while (ends != v1) {
ans[0].push_back(ends);
ends = pre[ends];
}
ans[0].push_back(ends);
reverse(ans[0].begin(), ans[0].end());
Ans[0] = dis[v2];
}
void Dijkstra_time(int v1, int v2) {
init();
queue<int> Q;
Q.push(v1);
dis[v1] = 0;
int u, v, w,w1;
while (!Q.empty()) {
v = Q.front(); Q.pop();
if (vis[v]) continue;
vis[v] = true;
for (int i = 0; i < E[v].size(); i++) {
u = E[v][i].v, w = E[v][i].tm, w1 = E[v][i].len;
if (dis[u] > dis[v] + w) {
Q.push(u);
dis[u] = dis[v] + w;//dis[x]表示起点到x的耗时
cnt[u] = cnt[v] + w1;//cnt[x]表示起点到x的路程
pre[u] = v;
}
else if (dis[u] == dis[v] + w && cnt[v] + w1 < cnt[u]) {
cnt[u] = cnt[v] + w1;
pre[u] = v;
}
}
}
int ends = v2;
while (ends != v1) {
ans[1].push_back(ends);
ends = pre[ends];
}
ans[1].push_back(ends);
reverse(ans[1].begin(), ans[1].end());
Ans[1] = dis[v2];
}
void output(int v1,int v2) {
if (ans[0] == ans[1]) {
printf("Time = %d; Distance = %d: ", Ans[1], Ans[0]);
for (int i = 0; i < ans[0].size(); i++) {
cout << ans[0][i];
if (i != ans[0].size() - 1) cout << " => ";
}
}
else {
printf("Time = %d: ", Ans[1]);
for (int i = 0; i < ans[1].size(); i++) {
cout << ans[1][i];
if (i != ans[1].size() - 1) cout << " => ";
}
cout << endl;
printf("Distance = %d: ", Ans[0]);
for (int i = 0; i < ans[0].size(); i++) {
cout << ans[0][i];
if (i != ans[0].size() - 1) cout << " => ";
}
}
}
int main() {
int N, M,v1,v2,one,len,tm;
cin >> N >> M;
while (M--) {
cin >> v1 >> v2 >> one >> len >> tm;
E[v1].push_back({
v2,len,tm });
if (!one) E[v2].push_back({
v1, len, tm });
}
cin >> v1 >> v2;
Dijkstra_len(v1, v2);
Dijkstra_time(v1, v2);
output(v1, v2);
return 0;
}
总结:For this kind of template question, you need to master the template,This will allow you to be flexible when doing questions.The basic template is given below:
Use an adjacency matrix to store weights
void Dijkstra_(int bg) {
memset(vis, false, sizeof(vis));
memset(dis, 0x3f, sizeof(dis));
for (int i = 0; i < N; i++) {
if (A[bg][i] < 0x3f3f3f3f) dis[i] = A[bg][i];
}
int t, minn;
vis[n] = true;
for (int i = 0; i < N; i++) {
minn = 0x3f3f3f3f;
for (int j = 0; j < N; j++) {
if (!vis[j] && minn > dis[j]) {
minn = dis[j];
t = j;
}
}
vis[t] = true;
for (int j = 0; j < N; j++) {
if (!vis[j] && dis[j] > dis[t] + A[t][j]) {
dis[j] = dis[t] + A[t][j];
}
}
}
}
Use an adjacency list to store weights
void Dijkstra(int bg) {
memset(vis, false, sizeof(vis));
memset(dis, 0x3f, sizeof(dis));
memset(pre, -1, sizeof(pre));
queue<int> Q;
Q.push(bg);
int w, u, v;
while (!Q.empty()) {
u = Q.front(); Q.pop();
if (vis[u]) continue;
vis[u] = true;
for (int i = 0; i < A[u].size(); i++) {
v = A[u][i].v, w = A[u][i].w;
if (dis[v] > dis[u] + w) {
dis[v] = dis[u] + w;
pre[v] = u;
}
}
}
}
边栏推荐
- ToDesk企业版上新 | 十大新功能,让企业远控更安全、更便捷、更流畅
- egg-validate-custom validation method error language (error Chinese prompt)
- leetcode: 122. 买卖股票的最佳时机 II
- Data labeling platform doccano----Introduction, installation, use, pit record
- 让你的文字被更多人看到:来投稿吧,稿酬靠谱!
- LeetCode_485_最大连续1的个数
- An egg - Nodemailer - qq email verification code development configuration
- LeetCode_485_Maximum number of consecutive 1s
- 国内最主流的5大项目工时管理系统
- 2022年最新《谷粒学院开发教程》:9 - 前台课程模块
猜你喜欢

Mini Program Optimization Practice

以0为底或以1为底对图片迭代次数的影响

egg-validate-自定义校验方法报错语言(报错中文提示)

数据标注平台doccano----简介、安装、使用、踩坑记录

08 获取器 withAttr、多连缀、whereRaw、事务、数据集《ThinkPHP6 入门到电商实战》

【Review of Live Streaming】Synthesis MindSpore Usability SIG2022 First Half Review Summary

leetcode 112.路经总和 递归

Monitoring tool Prometheus and project summary, 220805,,

Inside outside l think MindSpore AI framework, heavy industry gathering, huawei big extraordinary path of the model

egg-阿里云短信配置
随机推荐
cube-studio 部署过程
6G-Oriented Communication Perception Integrated Architecture and Key Technologies
测试岗电话面试——必问题型
Research on Blind Recognition of Digital Modulated Signal Based on MindSpore Framework
一行代码统计文本中指定字符串出现的次数
MySQL——索引与事务
剑指Offer 18.删除链表的节点
32. Do you know how Redis strings are implemented?
New retail project and offline warehouse core interview,, 220807,,
监控工具Prometheus及项目总结,220805,,
中国科学院金属研究所科研课题获华为技术认证,助力材料学发展新范式!
torch.view() function usage
依赖导致原则改善代码
项目分析(嵌入式产品中的硬件设计、生产)
Monitoring tool Prometheus and project summary, 220805,,
The effect of base 0 or base 1 on the number of image iterations
Awk syntax-03-awk expressions (if statements, while loops, for loops), execute shell commands in awk
【直播回顾】昇思MindSpore易用性SIG2022上半年回顾总结
Risk control strategy must be learned | This method of mining rules with decision trees
【模板引擎】velocity