zcb123 @ 2022-07-31 12:07:25
c++堆优化标准版AC, 弱化版第3个WA了...
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const int inf = 0x3f3f3f3f;
struct node {
int v, w;
node(int vv, int ww) {
v = vv;
w = ww;
}
};
vector<node> g[N];
int n, m, s;
int d[N];
set<pair<int, int> > min_heap;
void read(int u, int v, int w) {
g[u].push_back(node(v, w));
}
void Dijkstra() {
min_heap.insert(make_pair(0, s));
while (min_heap.size()) {
int mind = min_heap.begin() -> first;
int v = min_heap.begin() -> second;
min_heap.erase(min_heap.begin());
for (int i = 0; i < g[v].size(); i++) {
if (d[g[v][i].v] > d[v] + g[v][i].w) {
min_heap.erase(make_pair(d[g[v][i].v], g[v][i].v));
d[g[v][i].v] = d[v] + g[v][i].w;
min_heap.insert(make_pair(d[g[v][i].v], g[v][i].v));
}
}
}
}
int main() {
cin >> n >> m >> s;
for (int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w;
read(u, v, w);
}
memset(d, inf, sizeof(d));
d[s] = 0;
Dijkstra();
for (int i = 1; i <= n; i++) {
if (d[i] < 1000000){
cout << d[i] << " ";
}else{
cout << pow(2, 31) - 1 << " ";
}
}
return 0;
}
by 快乐的大童 @ 2022-07-31 12:13:00
你的 pow(2,31)
炸int
了
by yuer_qwq @ 2022-07-31 12:36:35
@zcb123 用快速幂
by TheSky233 @ 2022-07-31 12:46:57
@chenyuer 貌似直接输出
若不能到达则输出
2^{31}-1 。
by yuer_qwq @ 2022-07-31 12:50:00
@TheSky233 要是直接算好了也不是不可以嘛……
by zcb123 @ 2022-08-01 09:07:03
过了,多谢