题解里大家都是从终点推起点的做法,但是我一开始做的时候觉得正着推二分可行就直接莽了没多想2333
效率还是挺低的,但是能ac,注意二分的精度要拉到1e-10,不然部分样例会挂掉
#include <bits/stdc++.h>
using namespace std;
const int N = 2005, M = 2e5 + 5;
int head[N], ne[M], edge[M], wg[M], idx, n, m;
double dist[N];
bool st[N];
struct HeapNode
{
int id;
double val, cost;
bool operator<(const HeapNode& t) const
{
return cost > t.cost;
}
};
void add(int u, int v, int w)
{
edge[idx] = v;
wg[idx] = w;
ne[idx] = head[u];
head[u] = idx++;
}
void dijkstra(int x, double val)
{
memset(dist, 0x7f, sizeof(dist));
memset(st, false, sizeof(st));
dist[x] = 0;
priority_queue<HeapNode> q;
q.push({ x, val, 0});
while (!q.empty()) {
HeapNode node = q.top();
q.pop();
if (st[node.id]) continue;
for (int j = head[node.id]; j != -1; j = ne[j])
{
double num = node.val * wg[j] * 0.01;
if (!st[edge[j]] && num + node.cost < dist[edge[j]]) {
dist[edge[j]] = num + node.cost;
q.push({ edge[j], node.val - num, dist[edge[j]] });
}
}
st[node.id] = true;
}
}
int main()
{
memset(head, -1, sizeof(head));
cin >> n >> m;
for (int i = 0; i < m; i++)
{
int u, v, w;
cin >> u >> v >> w;
add(u, v, w);
add(v, u, w);
}
int a, b;
cin >> a >> b;
double l = 100, r = 1e9, ans;
while (r - l > 1e-10) {
double mid = (l + r) / 2;
dijkstra(a, mid);
if (mid - dist[b] >= 100) {
ans = mid;
r = mid;
}
else l = mid;
}
printf("%.8f\n", ans);
return 0;
}