AcWing 1126. 最小花费 基于log实现
原题链接
简单
作者:
code002
,
2025-01-17 16:15:36
,
所有人可见
,
阅读 1
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<iomanip>
using namespace std;
const int N = 2010,M=2e5+10;
typedef pair<double,int> PDI;
int h[N],ne[M],e[M],idx;
double w[M];
int n,m;
int A,B;
bool st[N];
double dist[N];
void add(int a,int b,double c)
{
e[idx] = b;
ne[idx] = h[a];
w[idx] = c;
h[a] = idx++;
}
double dijkstra()
{
for(int i=0;i<=n;i++) dist[i] = 100;
dist[A] = 0;
priority_queue<PDI,vector<PDI>,greater<PDI>> heap;
heap.push({0,A});
while(heap.size())
{
auto t = heap.top();
heap.pop();
double d = t.first; int ver = t.second;
if(!st[ver]) st[ver] = true;
else continue;
for(int i=h[ver];~i;i=ne[i])
{
int j = e[i];
if(dist[j] > d + w[i])
{
dist[j] = d+w[i];
heap.push({dist[j],j});
}
}
}
return dist[B];
}
int main()
{
memset(h,-1,sizeof h);
cin>>n>>m;
while(m--)
{
int a,b,c; cin>>a>>b>>c;
double t = (100.0-c)/100;
add(a,b,-log(t));
add(b,a,-log(t));
}
cin>>A>>B;
dijkstra();
cout <<fixed<<setprecision(8);
cout<<100.0/exp(-dist[B]);
return 0;
}