迪杰特拉的变形
思路:
目标函数: 100 = A ∗ Π w i 100 = A * \Pi w_i 100=A∗Πwi
要使得 A 最小,则
Π
w
i
\Pi w_i
Πwi 需最大。
max
(
Π
w
i
)
⇔
max
(
log
(
Π
w
i
)
)
⇔
max
(
∑
log
(
w
i
)
)
,其中
w
i
∈
(
0
,
1
)
\max(\Pi w_i) \\ \Leftrightarrow \max(\log(\Pi w_i)) \\ \Leftrightarrow \max(\sum \log(w_i)),其中 w_i \in(0, 1)
max(Πwi)⇔max(log(Πwi))⇔max(∑log(wi)),其中wi∈(0,1)
#include<iostream>
#include<cstring>
using namespace std;
const int N = 2010, INF = 0x3f3f3f3f;
int n, m, A, B;
double g[N][N];
double dist[N];
bool st[N];
void dij() {
dist[A] = 1;
for (int i = 1; i <= n; i ++ ) {
int t = -1;
for (int j = 1; j <= n; j ++ )
if (!st[j] && (t == -1 || dist[j] > dist[t]))
t = j;
st[t] = true;
for (int j = 1; j <= n; j ++ )
dist[j] = max(dist[j], dist[t] * g[t][j]);
}
}
int main(void) {
cin >> n >> m;
while (m -- ) {
int a, b, c;
cin >> a >> b >> c;
double z = 1.0 - c/100.0;
g[a][b] = g[b][a] = max(g[a][b], z);
}
cin >> A >> B;
dij();
printf("%.8lf", 100.0 / dist[B]);
return 0;
}