ProgramAlgTrain/20240919/CSP常考算法模板/最短路径/单源最短路径Dijkstra.cpp

66 lines
1.2 KiB
C++
Raw Normal View History

2024-09-19 02:22:41 +00:00
#include <bits/stdc++.h>
using namespace std;
const int N = 2501;
const int M = 15001;
struct point {
int id;
int len;
};
vector<point> g[N];
int t, c, ts, te;
int rs, re, ci;
int dis[N];
bool vis[N];
struct cmp //<2F>º<EFBFBD><C2BA><EFBFBD>
{
bool operator()(point a, point b) {
return a.len > b.len; //priority_queue<75><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>sort<72>Ĺ<EFBFBD><C4B9><EFBFBD><EFBFBD>
}
};
priority_queue<point, vector<point>, cmp> pq;
void Dijkstra()
{
memset(dis, 0x3f, sizeof(dis));
dis[ts] = 0; // ע<><D7A2><EFBFBD><EFBFBD> ʼ<><CABC><EFBFBD><EFBFBD>ts<74><73><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
pq.push({ts, 0});
while (pq.size()>0) {
int id = pq.top().id; // ȡ<><C8A1><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>
pq.pop();
if (vis[id]) continue;
vis[id] = 1;
for (point e:g[id]) {
if (dis[e.id] > dis[id] + e.len) {
dis[e.id] = dis[id] + e.len;
pq.push({e.id, dis[e.id]});
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> t >> c >> ts >> te;
for (int i = 0; i < c; i++) {
cin >> rs >> re >> ci;
g[rs].push_back({re,ci});
g[re].push_back({rs,ci});
}
Dijkstra();
cout << dis[te] << endl;
return 0;
}