1129.热浪
算法:
堆优化dijkstra,不过起点是题目给的,所以初始化时dist[ts]=0,之前一直写的dist[1]=0,淦。
代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
typedef pair<int, int> PII;
int n,m,ts,te;
const int N = 2510,M=6200*2+10;
int h[N], e[M], w[M], ne[M], idx;
int dist[N];
bool st[N];
void add(int a, int b, int c) // 添加一条边a->b,边权为c
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}
void dijkstra() // 求ts号点到te号点的最短路距离
{
memset(dist, 0x3f, sizeof dist);
dist[ts] = 0;
priority_queue<PII, vector<PII>, greater<PII>> heap;
heap.push({0, ts});
while (heap.size())
{
auto t = heap.top();
heap.pop();
int ver = t.second, distance = t.first;
if (st[ver]) continue;
st[ver] = true;
for (int i = h[ver]; i != -1; i = ne[i])
{
int j = e[i];
if (dist[j] > dist[ver] + w[i])
{
dist[j] = dist[ver] + w[i];
heap.push({dist[j], j});
}
}
}
}
int main()
{
cin>>n>>m>>ts>>te;
memset(h, -1, sizeof h);
for(int i=0;i<m;i++){
int a,b,c;
cin>>a>>b>>c;
add(a, b, c);
add(b, a, c);
}
dijkstra();
cout<<dist[te];
return 0;
}