【最短路】洛谷 P4779 【模板】单源最短路径(标准版)
2021-04-15 16:31:00
# ACM
题链
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| #include <bits/stdc++.h> using namespace std; #define MS 100009 #define ls rt<<1 #define rs rt<<1|1 #define LL long long #define MAXN 2147483647
int n,m; int st;
struct node{ int to,val; }; vector<node > vc[MS];
struct nod{ int poi,val; }; priority_queue<nod > Q; bool operator < (nod t1,nod t2){ return t1.val > t2.val; } int dis[MS]; bool v[MS];
void dijkstra(){ for(int i=1;i<=n;i++){ v[i] = 0; dis[i] = MAXN; } dis[st] = 0; Q.push({st,0}); while(!Q.empty()){ nod x = Q.top(); Q.pop(); if(v[x.poi]) continue; v[x.poi] = 1; for(auto &it:vc[x.poi]){ if(dis[it.to] > dis[x.poi] + it.val){ dis[it.to] = dis[x.poi] + it.val; if(!v[it.to]){ Q.push({it.to,dis[it.to]}); } } } } }
int main() { ios::sync_with_stdio(false); cin >> n >> m >> st; for(int i=1;i<=m;i++){ int u,v,w; cin >> u >> v >> w; vc[u].push_back({v,w}); } dijkstra(); for(int i=1;i<=n;i++){ cout << dis[i] << " "; } cout << "\n"; return 0; }
|
2021-04-15 16:31:00
# ACM