题目描述
blablabla
通过B等于100反推A
每次用dist[j] = dist[t] / (1.0 - w[i] / 100.0) 来更新,将B入队,其他置为INF,求B -> A的最短路即可
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 2010;
const int M = 2e5 + 10;
int n, m;
int h[N], e[M], w[M], ne[M], idx;
double dist[N];
bool st[N];
void add(int a, int b, int c) {
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx ++;
}
void spfa(int s) {
memset(dist, 127, sizeof dist);
dist[s] = 100;
queue<int > q;
q.push(s);
while (!q.empty()) {
int t = q.front();
q.pop();
st[t] = false;
for (int i = h[t]; ~i; i = ne[i]) {
int j = e[i];
if (dist[j] > dist[t] / (1.0 - w[i] / 100.0)) {
dist[j] = dist[t] / (1.0 - w[i] / 100.0);
if (!st[j]) {
q.push(j);
st[j] = true;
}
}
}
}
return;
}
int main() {
cin >> n >> m;
memset(h, -1, sizeof h);
while (m --) {
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
add(b, a, c);
}
int A, B;
cin >> A >> B;
spfa(B);
printf("%.8lf\n", dist[A]);
return 0;
}