AcWing 1126. 最小花费
原题链接
简单
作者:
fffzx
,
2021-02-22 16:53:12
,
所有人可见
,
阅读 250
/*
算法描述:
本题的dist数组表示从原点到其他点还剩原本钱的百分比,因此本题需要最大化d[B]
初始d[A] = 1,所以更新时采用d[j] = max(d[j], d[t] * g[t][j]);
g[t][j] = (100.0 - z) / 100,稍微对输入数据进行一下变化
*/
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 2010;
double g[N][N];
int n, m, A, B;
double d[N];
bool st[N];
void dijkstra()
{
d[A] = 1;
for (int i = 0; i < n; i ++ )
{
int t = -1;
for (int j = 1; j <= n; j ++ )
if (!st[j] && (t == -1 || d[t] < d[j]))
t = j;
st[t] = true;
for (int j = 1; j <= n; j ++ )
d[j] = max(d[j], d[t] * g[t][j]);
}
printf("%.8f\n", 100 / d[B]);
}
int main()
{
scanf("%d%d", &n, &m);
memset(g, 0x3f, sizeof g);
while (m -- )
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
double w = (100.0 - c) / 100;
g[a][b] = g[b][a] = max(g[a][b], w);
}
scanf("%d%d", &A, &B);
dijkstra();
return 0;
}