朴素版Dijkstra算法
题目链接–Dijkstra求最短路径
关键代码
int Dijkstra()
{
dist[1] = 0;
for (int i = 1; i <= n; i++){
int t = -1;
for (int j = 1; j <= n; j++)
if (!vis[j] && (t == -1 || dist[j] < dist[t]))
t = j;
vis[t] = true;
for (int j = 1; j <= n; j++)
dist[j] = min(dist[j],dist[t] + path[t][j]);
}
return dist[n];
}
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 510;
int n, m, path[N][N], dist[N];
bool vis[N];
int Dijkstra()
{
dist[1] = 0;
for (int i = 1; i <= n; i++)
{
int t = -1;
for (int j = 1; j <= n; j++)
if (!vis[j] && (t == -1 || dist[j] < dist[t]))
t = j;
vis[t] = true;
for (int j = 1; j <= n; j++)
dist[j] = min(dist[j],dist[t] + path[t][j]);
}
return dist[n];
}
int main()
{
cin >> n >> m;
memset(path, 0x3f, sizeof path);
memset(dist, 0x3f, sizeof dist);
int x, y, v;
while (m--)
{
cin >> x >> y >> v;
path[x][y]=min(v,path[x][y]);
}
int ret = Dijkstra();
if (ret != 0x3f3f3f3f) cout << ret;
else cout << -1;
return 0;
}
- 关于定义无穷为0x3f3f3f3f而不取0x7fffffff详细见这篇博客
简单说就是用0x7fffffff可能会使无穷+无穷数据溢出,变成负数,而0x3f3f3f3f不会 - 关于memset( ,0x3f,sizeof ):
如果我们想要将某个数组清零,我们通常会使用memset(a,0,sizeof(a))这样的代码来实现(方便而高效),但是当我们想将某个数组全部赋值为无穷大时(例如解决图论问题时邻接矩阵的初始化),就不能使用memset函数而得自己写循环了(写这些不重要的代码真的很痛苦),我们知道这是因为memset是按字节操作的,它能够对数组清零是因为0的每个字节都是0,现在好了,如果我们将无穷大设为0x3f3f3f3f,那么奇迹就发生了,0x3f3f3f3f的每个字节都是0x3f!所以要把一段内存全部置为无穷大,我们只需要memset(a,0x3f,sizeof(a))。
堆排序优化版Dijkstra
题目链接–Dijkstra求最短路径Ⅱ
关于优先队列priority_queue函数的使用:
头文件#include<queue> 定义:priority_queue<Type, Container, Functional> Type 就是数据类型,Container 就是容器类型(Container必须是用数组实现的容器,比如vector,deque等等,但不能用 list。STL里面默认用的是vector),Functional 就是比较的方式。 greater<Type>是小根堆,less<Type>是大根堆
2 priority_queue <int,vector<int>,greater<int> > q;
3
4 priority_queue <int,vector<int>,less<int> >q;
完整代码
思路和朴素版的其实一样 关键代码
int Dijkstra()
{
heap.push({ 0,1 });
while (!heap.empty()) {
PII temp = heap.top();
heap.pop();
int node = temp.second, distance = temp.first;
if (vis[node]) continue;
vis[node] = true;
for (int i = h[node]; i != -1; i = ne[i])
if (distance + W[i] < dist[e[i]]) {
dist[e[i]] = W[i] + distance;
heap.push({ dist[e[i]],e[i] });
}
}
return dist[n];
}
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
typedef pair<int, int>PII;
const int N = 1.5 * 1e5 + 5;
int h[N], e[N], ne[N], W[N], idx, n, dist[N];
bool vis[N];
priority_queue<PII, vector<PII>, greater<PII>>heap;
void add(int x, int y, int w)
{
e[idx] = y; ne[idx] = h[x]; W[idx] = w; h[x] = idx++;
}
int Dijkstra()
{
heap.push({ 0,1 });
dist[1] = 0;
while (!heap.empty())
{
PII temp = heap.top();
heap.pop();
int node = temp.second, distance = temp.first;
if (vis[node]) continue;
vis[node] = true;
for (int i = h[node]; i != -1; i = ne[i])
{
if (dist[e[i]] > distance + W[i]) {
dist[e[i]] = distance + W[i];
heap.push({dist[e[i]],e[i]});
}
}
}
return dist[n];
}
int main()
{
memset(h, -1, sizeof h);
memset(dist, 0x3f, sizeof dist);
int m, x, y, w;
cin >> n >> m;
while (m--)
{
cin >> x >> y >> w;
add(x, y, w);
}
int ret = Dijkstra();
if (ret == 0x3f3f3f3f) cout << -1;
else cout << ret;
return 0;
}
|