dijkstra 最短路算法
最朴素的做法o(V*V/2+2E)~O(V^2)
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>
const int MAX = 2002;
int n;
int graph[MAX][MAX];
int dis[MAX];
bool vis[MAX];
const int INF = 0X7FFFFFFF;
int dijkstra()
{
memset(vis, false, sizeof(vis));
vis[1] = true;
for (int i = 1; i <= n; i++)
dis[i] =INF;
dis[1] = 0;
for (int i = 1; i <= n; i++)
{
if (graph[i][1] != 0)
dis[i] = graph[i][1];
}
for (int i = 0; i < n-1; i++)
{
int minn = INF, position = 1;
for (int i = 1; i <= n; i++)
{
if (!vis[i] && minn>dis[i])
{
minn = dis[i];
position = i;
}
}
vis[position] = true;
for (int i = 1; i <= n; i++)
{
if (!vis[i] &&graph[i][position]!=0&&dis[i] > (dis[position] + graph[i][position]))
dis[i] = dis[position] + graph[i][position];
}
}
return dis[n];
}
int main()
{
int t;
cin >> t>> n;
memset(graph, 0, sizeof(graph));
for (int i = 0; i < t; i++)
{
int x1, y1, val;
cin >> x1 >> y1 >> val;
if (graph[x1][y1])
{
if (graph[x1][y1]>val)
graph[x1][y1] = graph[y1][x1] = val;
}
else
{
graph[x1][y1] = graph[y1][x1] = val;
}
}
cout << dijkstra() << endl;
return 0;
}
/*
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
*/
最短路的优先队列做法,时间复杂度O(2E+VlogV)~o(vlgv)
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>
#include<queue>
#include<algorithm>
struct node
{
int x, d;
node(int _x, int _d) :x(_x), d(_d){}
bool operator< (const node &b) const{
return d > b.d;
}
};
const int MAX = 2002;
int n;
int graph[MAX][MAX];
vector<vector<int>> edges(MAX);
int dis[MAX];
bool vis[MAX];
const int INF = 0X7FFFFFFF;
int dijkstra()
{
memset(vis, false, sizeof(vis));
priority_queue<node> que;
for (int i = 1; i <= n; i++)
dis[i] =INF;
dis[1] = 0;
que.push(node(1, 0));
while (!que.empty())
{
node tmp = que.top();
que.pop();
if (vis[tmp.x]) continue;
vis[tmp.x] = true;
for (int i = 0; i < edges[tmp.x].size(); i++)
{
if (!vis[edges[tmp.x][i]] && graph[edges[tmp.x][i]][tmp.x]!=0 && dis[edges[tmp.x][i]]>(dis[tmp.x] + graph[edges[tmp.x][i]][tmp.x]))
{
dis[edges[tmp.x][i]] = graph[edges[tmp.x][i]][tmp.x] + dis[tmp.x];
que.push(node(edges[tmp.x][i], dis[edges[tmp.x][i]]));
}
}
}
return dis[n];
}
int main()
{
int t;
cin >> t>> n;
memset(graph, 0, sizeof(graph));
edges.resize(MAX);
for (int i = 0; i < t; i++)
{
int x1, y1, val;
cin >> x1 >> y1 >> val;
if (graph[x1][y1])
{
if (graph[x1][y1]>val)
{
graph[x1][y1] = graph[y1][x1] = val;
}
}
else
{
graph[x1][y1] = graph[y1][x1] = val;
edges[x1].push_back(y1);
edges[y1].push_back(x1);
}
}
cout << dijkstra() << endl;
return 0;
}
/*
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
*/
dijkstra 最短路算法的更多相关文章
- Dijkstra最短路算法
Dijkstra最短路算法 --转自啊哈磊[坐在马桶上看算法]算法7:Dijkstra最短路算法 上节我们介绍了神奇的只有五行的Floyd最短路算法,它可以方便的求得任意两点的最短路径,这称为“多源最 ...
- 【坐在马桶上看算法】算法7:Dijkstra最短路算法
上周我们介绍了神奇的只有五行的Floyd最短路算法,它可以方便的求得任意两点的最短路径,这称为“多源最短路”.本周来来介绍指定一个点(源点)到其余各个顶点的最短路径,也叫做“单源最短路径 ...
- Dijkstra 最短路算法(只能计算出一条最短路径,所有路径用dfs)
上周我们介绍了神奇的只有五行的 Floyd 最短路算法,它可以方便的求得任意两点的最短路径,这称为"多源最短路".本周来来介绍指定一个点(源点)到其余各个顶点的最短路径,也叫做&q ...
- 【啊哈!算法】算法7:Dijkstra最短路算法
上周我们介绍了神奇的只有五行的Floyd最短路算法,它可以方便的求得任意两点的最短路径,这称为“多源最短路”.本周来来介绍指定一个点(源点)到其余各个顶点的最短路径,也叫做“单源最短路径”.例如求下图 ...
- 对于dijkstra最短路算法的复习
好久没有看图论了,就从最短路算法开始了. dijkstra算法的本质是贪心.只适用于不含负权的图中.因为出现负权的话,贪心会出错. 一般来说,我们用堆(优先队列)来优化,将它O(n2)的复杂度优化为O ...
- 如何在 Java 中实现 Dijkstra 最短路算法
定义 最短路问题的定义为:设 \(G=(V,E)\) 为连通图,图中各边 \((v_i,v_j)\) 有权 \(l_{ij}\) (\(l_{ij}=\infty\) 表示 \(v_i,v_j\) 间 ...
- python dijkstra 最短路算法示意代码
def dijkstra(graph, from_node, to_node): q, seen = [(0, from_node, [])], set() while q: cost, node, ...
- dijkstra最短路算法(堆优化)
这个算法不能处理负边情况,有负边,请转到Floyd算法或SPFA算法(SPFA不能处理负环,但能判断负环) SPFA(SLF优化):https://www.cnblogs.com/yifan0305/ ...
- 算法学习笔记(三) 最短路 Dijkstra 和 Floyd 算法
图论中一个经典问题就是求最短路.最为基础和最为经典的算法莫过于 Dijkstra 和 Floyd 算法,一个是贪心算法,一个是动态规划.这也是算法中的两大经典代表.用一个简单图在纸上一步一步演算,也是 ...
随机推荐
- windows 10 & Office 2016 安装
Office 2016 VOL版 http://blog.sina.com.cn/s/blog_470614a90102vtmc.html 专业版合集: magnet:?xt=urn:btih: ...
- Stanford机器学习笔记-10. 降维(Dimensionality Reduction)
10. Dimensionality Reduction Content 10. Dimensionality Reduction 10.1 Motivation 10.1.1 Motivation ...
- 阿里云 CentOS6.5 ssh连接慢的解决方案
我租了一台阿里云深圳的服务器,用的是CentOS6.5的系统,最近要在服务器上小改点代码,但是不管用private shell 还是securecrt工具连接,连上去后,都特别慢,经常敲一段代码要过个 ...
- 05章 OGNL
一.OGNL全称是Object Graph Navigation Language,即对象导航图语言 OGNL在框架中主要做两件事情:表达式语言和类型转换器 OGNL在框架中的作用以及数据的流入流出: ...
- curl命令
定位后端接口是否ok,经常使用到curl -b/cookie <name=string/file> cookie字符串或文件读取位置 curl http://localhost --co ...
- js继承《转》
http://www.jb51.net/article/55540.htm http://www.cnblogs.com/OceanHeaven/p/4965947.html http://www.j ...
- .net core API 统一拦截错误
public override void OnActionExecuted(ActionExecutedContext context) { if (context.Exception != null ...
- 如何把Json格式字符写进text文件中
本篇一步一步学习怎样把显示于网页的json格式的字符串写进text文件中,并保存起来.学习到创建model, Entity, 序列化List<object>转换为json,显示于网页上.然 ...
- C语言 memset函数盲点
#include <stdio.h> #include <stdlib.h> #include <string.h> struct packet { int len ...
- 如果动态设置json对象的key
项目中要求动态设置json的key属性,如果按照一般的json设置方法是不行的.假如你把一个key设置为一个变量的话,那么最后js解析出来的就是key为这个变量名而不是这个变量的值. 解决:通过使用 ...