Dijkstra——Travel Plan

2024-04-07 16:48
文章标签 travel dijkstra plan

本文主要是介绍Dijkstra——Travel Plan,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在这里插入图片描述
在这里插入图片描述
解法1: Dijkstra

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
using namespace std;const int MAXV = 1010;
const int INF = 1000000000;
int n, m, st, ed, G[MAXV][MAXV], cost[MAXV][MAXV];
int d[MAXV], c[MAXV], pre[MAXV];
bool vis[MAXV] = {false};
vector<int> t;void Dijkstra(int s)
{fill(d, d + MAXV, INF);fill(c, c + MAXV, INF);d[s] = 0;c[s] = 0;for(int i = 0; i < n; i++){  // 循环 n 次int u = -1, MIN = INF;for(int j = 0; j < n; j++){if(vis[j] == false && d[j] < MIN){u = j;MIN = d[j];}	} // 找最小if(u == -1) return;vis[u] = true; // 拉入 for(int v = 0; v < n; v++){if(vis[v] == false && G[u][v] != INF){if(d[u] + G[u][v] < d[v]){d[v] = d[u] + G[u][v];c[v] = c[u] + cost[u][v];pre[v] = u;} else if(d[u] + G[u][v] == d[v]){if(c[u] + cost[u][v] < c[v]){c[v] = c[u] + cost[u][v];pre[v] = u;}}}}  // 更新 	}
} void Print(int ed)
{t.push_back(ed);int x = ed;while(x != st){x = pre[x];t.push_back(x);}for(int i = t.size() - 1; i >= 0; i--){printf("%d ",t[i]);}printf("%d %d\n", d[ed], c[ed]);
}int main()
{fill(G[0], G[0] + MAXV * MAXV, INF);fill(cost[0], cost[0] + MAXV * MAXV, INF);scanf("%d%d%d%d", &n, &m, &st, &ed);int c1, c2, dis, cos;for(int i = 0; i < m; i++){scanf("%d%d%d%d", &c1, &c2, &dis, &cos);G[c1][c2] = dis;G[c2][c1] = dis;cost[c1][c2] = cos;cost[c2][c1] = cos;}Dijkstra(st);Print(ed);return 0;
}/*
4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20
*/

解法2: Dijkstra + DFS

#include <stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXV = 510;
const int INF = 1000000000;int n, m, st, ed, G[MAXV][MAXV], cost[MAXV][MAXV];
int d[MAXV], minCost = INF;
bool vis[MAXV] = {false};
vector<int> pre[MAXV];
vector<int> tempPath, path;void Dijkstra(int s)
{fill(d, d + MAXV, INF);d[s] = 0;for(int i = 0; i < n; i++){  // 循环 n 次 int u = -1, MIN = INF;for(int j = 0; j < n; j++){if(vis[j] == false && d[j] < MIN){u = j;MIN = d[j];}}if(u == -1) return;vis[u] = true;for(int v = 0; v < n; v++){if(vis[v] == false && G[u][v] != INF){if(d[u] + G[u][v] < d[v]){d[v] = d[u] + G[u][v];pre[v].clear();pre[v].push_back(u);  } else if(d[u] + G[u][v] == d[v]){pre[v].push_back(u); }}} // 更新 }
}void DFS(int v)
{if(v == st){  // 递归到叶子节点tempPath.push_back(v);int tempCost = 0;for(int i = tempPath.size() - 1; i > 0; i--){  // 计算耗费 int id = tempPath[i], idNext = tempPath[i - 1];tempCost += cost[id][idNext];	}  if(tempCost < minCost){minCost = tempCost;path = tempPath;} // 更新最优路径tempPath.pop_back();return; }tempPath.push_back(v);for(int i = 0; i < pre[v].size(); i++){DFS(pre[v][i]);	} tempPath.pop_back();
}int main()
{fill(G[0], G[0] + MAXV * MAXV, INF);fill(cost[0], cost[0] + MAXV * MAXV, INF);scanf("%d%d%d%d", &n, &m, &st, &ed);int c1, c2, dis, cos;for(int i = 0; i < m; i++){scanf("%d%d%d%d", &c1, &c2, &dis, &cos);G[c1][c2] = dis;G[c2][c1] = dis;cost[c1][c2] = cos;cost[c2][c1] = cos;}Dijkstra(st);DFS(ed);for(int i = path.size() - 1; i >= 0; i--){printf("%d ", path[i]);}printf("%d %d\n", d[ed], minCost);return 0;
}

这篇关于Dijkstra——Travel Plan的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/883143

相关文章

poj 1502 MPI Maelstrom(单源最短路dijkstra)

题目真是长得头疼,好多生词,给跪。 没啥好说的,英语大水逼。 借助字典尝试翻译了一下,水逼直译求不喷 Description: BIT他们的超级计算机最近交货了。(定语秀了一堆词汇那就省略吧再见) Valentine McKee的研究顾问Jack Swigert,要她来测试一下这个系统。 Valentine告诉Swigert:“因为阿波罗是一个分布式共享内存的机器,所以它的内存访问

uva 10801(乘电梯dijkstra)

题意: 给几个电梯,电梯0 ~ n-1分别可以到达很多层楼。 换乘电梯需要60s时间。 问从0层到target层最小的时间。 解析: 将进入第0层的电梯60s也算上,最后减。 坑点是如果target为0输出0。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algori

hdu 3790 (单源最短路dijkstra)

题意: 每条边都有长度d 和花费p,给你起点s 终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。 解析: 考察对dijkstra的理解。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstrin

poj 3255 次短路(第k短路) A* + spfa 或 dijkstra

题意: 给一张无向图,求从1到n的次短路。 解析: A* + spfa 或者 dijkstra。 详解见上一题:http://blog.csdn.net/u013508213/article/details/46400189 本题,spfa中,stack超时,queue的效率最高,priority_queue次之。 代码: #include <iostream>#i

QT Travel

Code Resource: https://github.com/MoreYoungGavin/QT_Travel.git What is QT? QT is a cross-platform application development framework for desktop,embedded and mobile. What need install QT before? Yo

Study Plan For Algorithms - Part24

1. 包含min函数的栈 定义栈的数据结构,要求在该类型中实现一个 min 函数,能够获取栈的最小元素。在该栈中,调用 min、push 以及 pop 函数的时间复杂度均为 O (1)。 方法: class MinStack:def __init__(self):self.stack = []self.min_stack = [float('inf')]def push(self, x):sel

执行计划查看方法(Explain plan)

什么是执行计划 所谓执行计划,顾名思义,就是对一个查询任务,做出一份怎样去完成任务的详细方案。举个生活中的例子,我从珠海要去英国,我可以 选择先去香港然后转机,也可以先去北京转机,或者去广州也可以。但是到底怎样去英国划算,也就是我的费用最少,这是一件值得考究 的事情。同样对于查询而言,我们提交的SQL仅仅是描述出了我们的目的地是英国,但至于怎么去,通常我们的SQL中是没有给出提示信息

Dijkstra算法总结

1.如何建立图   Graph 一般是adjecent list, class DirectedGraphNode {     int label;     List<DirectedGraphNode> neighbors;     ... } 也可以使用 HashMap 和 HashSet 搭配的方式来存储邻接表 hashmap<Integer, List<Integer

BFS 到 Level Order traverse 到 UnionFind 到 Topological Sort 到 Dijkstra 思路总结

====BFS 找联通量,找component. Number of Islands (BFS, DFS 都可以做) Surrounded Regions 算法是:先收集四个周边的 O,然后用BFS或者DFS向里面扩展,visited记录connect点,最后如果没有被visited到的O,会变成X;T: O(m*n), Space: O(m*n). Is Graph Bipartite (

HDU1874_畅通工程续(Dijkstra最短路)

畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 23022    Accepted Submission(s): 8085 Problem Description 某省自从实行了很多年的畅通工程计划后,终