Minimum cost c++ detailed explanation
minimum cost
topic description
at nnAmong n individuals, the bank accounts of certain individuals can transfer money to each other. Fees for transferring money between these people vary. Given that a few percent of the handling fee needs to be deducted from the transfer amount when transferring money between these people, askAAHow much money does A need at least to make BBafter the transferB receives100 100100 yuan.
input format
The first line enters two positive integers n, mn,mn,m , represent the logarithm of the total number of people and the number of people who can transfer money to each other, respectively.
Below mmEnter three positive integers x , y , zx,y,zin each line of m linesx,y,z , means the label isxxperson x and label yyTransfer between y people need to deduct z % z\%z % commission( z < 100 ) (z<100)(z<100)。
The last line enters two positive integers A, BA, BA,B. _ DataAssuranceAAA andBBB can transfer funds directly or indirectly.
output format
Export AAA makesBBB received100 100A minimum total fee of $ 100 is required. Accurate to 8after the decimal point8 bits.
sample
sample input
3 3
1 2 1
2 3 2
1 3 3
1 3
sample output
103.07153164
hint
1 ≤ n ≤ 2000 , m ≤ 100000 1\le n \le 2000,m\le 1000001≤n≤2000,m≤100000。
solution
As can be seen from the meaning of the title, the label is xxperson x and label yyTransfer between y people need to deduct z % z\%z % commission( z < 100 ) (z<100)(z<100 ) .
That is to say:
the label isxxperson x and label yyWhen people of y transfer money to each other, only the original money will be received( 100 − z ) % (100-z)\%(100−z ) %
and after the intermediary transfer, the money received is the original( 100 − z 1 ) % × ( 100 − z 2 ) % (100-z_1)\%×(100-z_2)\%(100−z1)%×(100−z2) %
then we can run a Dijkstra fix.
the code
#include <bits/stdc++.h>
using namespace std;
int n,m,x,y,z,s,t,p[2200];
double f[2200][2200],c[2200];
int main()
{
cin >>n >>m;
for (int i=1;i<=m;i++)
{
cin >>x >>y >>z;
f[x][y]=f[y][x]=(100.0-z)/100.0;
}
cin >>s >>t;
p[s]=1;
for (int i=1;i<=n;i++)
c[i]=f[s][i];
for (int i=1;i<n;i++)
{
int k=0;
double ma=0.0;
for (int j=1;j<=n;j++)
if (p[j]==0 && ma<c[j])
{
k=j;
ma=c[j];
}
p[k]=1;
for (int j=1;j<=n;j++)
if (p[j]==0 && c[k]*f[k][j]>c[j])
c[j]=c[k]*f[k][j];
}
printf("%.8lf",100.0/c[t]);
return 0;
}