社区讨论
迷茫女嘉宾在线求助
P4779【模板】单源最短路径(标准版)参与者 11已保存回复 26
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 26 条
- 当前快照
- 1 份
- 快照标识符
- @mi6wkep5
- 此快照首次捕获于
- 2025/11/20 11:59 4 个月前
- 此快照最后确认于
- 2025/11/20 16:12 4 个月前
Dijkstra WA得wawa大哭 ~~ 求助
CPP// %%%%%% LRJ %%% Eziotao
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#define F(i, a, b) for(register ui i = a; i <= b; ++i)
#define D(i, a, b) for(register ui i = a; i >= b; --i)
#define Imax(a, b) ((a) > (b) ? (a) : (b))
#define Imin(a, b) ((a) < (b) ? (a) : (b))
#define Iswap(a, b) (a ^= b ^= a ^= b)
#define debug(x) cout<<x<<
#define Isdigit(ch) (ch >= '0' && ch <= '9')
#define Change(x) ((x<<1) + (x<<3) + (ch^48))
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<17, stdin), p1 == p2) ? EOF : *p1++)
typedef long long LL;
typedef unsigned int ui;
typedef unsigned long long ul;
typedef double db;
const int N = 100005;
const int M = 200005;
const int INF = 0x7f7f7f7f;
char buf[1<<17], *p1 = buf, *p2 = buf;
using namespace std;
struct FastIO{
FastIO(){};
inline FastIO& operator>>(register ui &x){
x = 0; register char ch = getchar();
while(!Isdigit(ch)) ch = getchar();
while(Isdigit(ch)) x = Change(x), ch = getchar();
return x, *this;
}
};
FastIO io;
inline void write(register ui x){
register ui _ = 10;
while(_ <= x) _ *= 10;
while(_ ^ 1) _ /= 10, putchar(x/_ + 48), x %= _;
printf(" ");
}
struct Node{
ui val, u;
bool operator < (const Node &a) const{
return val > a.val;
}
};
ui dis[N], nxt[M], hd[N], v[M], w[M], n, m, st;
inline void Dijkstra(){
memset(dis, 0x7f, sizeof(dis));
dis[st] = 0;
priority_queue<Node>q;
register Node k;
k.u = st; k.val = 0;
q.push(k);
register ui vv, uu, val, u;
while(!q.empty()){
k = q.top(); q.pop();
uu = k.u; val = k.val;
if(val ^ dis[uu]) continue;
for(u = hd[uu]; u; u = nxt[u]){
vv = v[u];
if(dis[vv] > val + w[u]){
dis[vv] = val + w[u];
q.push((Node){dis[vv], vv});
}
}
}
}
int main(){
io >> n >> m >> st; register ui x;
F(i, 1, m){
io >> x >> v[i] >> w[i];
nxt[i] = hd[x]; hd[x] = i;
}
Dijkstra();
F(i, 1, n) write(dis[i]);
return 0;
}
回复
共 26 条回复,欢迎继续交流。
正在加载回复...