Tjaweiof @ 2023-07-24 16:47:21
#include <bits/stdc++.h>
using namespace std;
const int N = 100001;
struct edge{
int x, y, nxt;
long long w;
}E[N << 2];
int head[N], pre[N], n, m, s, tot = 0;
struct node{
int x;
long long d;
bool operator < (const node A) const{
return d > A.d;
}
};
priority_queue<node> Q;
long long dis[N];
bool vis[N];
inline void addedge(int x, int y, int w){
E[++tot] = (edge){x, y, head[x], w};
head[x] = tot;
}
inline long long dijkstra(int s, int t){
memset(dis, 0x7f, sizeof(dis));
memset(vis, 0, sizeof(vis));
pre[s] = 0;
Q.push((node){s, 0});
while (!Q.empty()){
int now = Q.top().x;
Q.pop();
if (vis[now]) continue;
vis[now] = 1;
for (int i = head[now]; ~i; i = E[i].nxt){
int to = E[i].y;
if (!vis[to] && dis[to] > dis[now] + E[i].w){
dis[to] = dis[now] + E[i].w;
pre[to] = now;
Q.push((node){to, dis[to]});
}
}
}
return dis[t];
}
int main(){
memset(head, -1, sizeof(head));
memset(pre, 0x3f, sizeof(pre));
scanf("%d%d%d", &n, &m, &s);
for (int i = 1; i <= m; i++){
long long j, k, l;
scanf("%lld%lld%lld", &j, &k, &l);
addedge(j, k, l);
addedge(k, j, l);
}
for (int i = 1; i <= n; i++){
printf("%lld ", dijkstra(s, i));
}
return 0;
}
by ran_qwq @ 2023-07-24 17:07:53
@Tjaweiof
不用跑 n 遍 dijkstra,跑一遍输出 dis 数组就行了
边是有向边
by ran_qwq @ 2023-07-24 17:08:04
关注@ran_qwq 谢谢喵~
by Tjaweiof @ 2023-07-24 17:12:14
@ran_qwq 谢谢!已关