57分,WA在测试点16,19,20上。求调。
#include <bits/stdc++.h>
using namespace std;
#define N 20005
long long n, m, s, t;
long long x, y, z;
long long d[N], v[N];
long long minn = 0x3f3f3f3f3f3f3f3f3f;
struct Node {
int to;
long long w, st;
bool operator<(const Node &a) const {
return w > a.w || (w == a.w && st > a.st);
}
};
vector<Node> g[N];
priority_queue<Node> q;
void dijkstra() {
while (!q.empty()) {
Node cur = q.top();
q.pop();
if (v[cur.to] <= cur.st) continue;
v[cur.to] = cur.st;
for (int j = 0; j < g[cur.to].size(); j++) {
Node next = g[cur.to][j];
if (v[next.to] >= cur.st + 1) {
int ns = 0, nt = cur.st + 1;
if (d[cur.to] + next.w / cur.st < d[next.to]) {
d[next.to] = d[cur.to] + next.w / cur.st;
}
while (nt <= next.w) {
q.push(Node{next.to, d[next.to] + ns, nt});
ns += next.w / nt;
nt += 1;
ns += next.w / nt;
nt += 1;
}
minn = min(minn, d[next.to] + ns);
}
}
}
}
int main() {
cin >> n >> m >> s >> t;
for (int i = 1; i <= m; i++) {
cin >> x >> y >> z;
g[x].push_back(Node{y, z});
g[y].push_back(Node{x, z});
}
for (int i = 1; i <= n; i++) d[i] = v[i] = 0x3f3f3f3f3f3f3f3f3f;
d[t] = 0;
q.push(Node{t, 0, 1});
dijkstra();
cout << min(d[s], minn) ;
return 0;
}