Minimum cost (shortest path)

Insert picture description here

Idea: The path from A to B is, you can assume that the money at A is X, and when it reaches B, it is 100 (the meaning of the question), then you can find that 100=x*(z1% z2% …zb%) then x is exhausted If it is small, let (z1% z2% …zb%) be as large as possible, then we will find a path with the largest product.

//#pragma GCC optimize(2)
#include<bits/stdc++.h>
 
using namespace std;
typedef long long ll;
#define SIS std::ios::sync_with_stdio(false)
#define space putchar(' ')
#define enter putchar('\n')
#define lson root<<1
#define rson root<<1|1
typedef pair<int,int> PII;
const int mod=1e9+7;
const int N=1e5+5;
const int inf=0x7f7f7f7f;

int gcd(int a,int b)
{
    
    
    return b==0?a:gcd(b,a%b);
}
 
ll lcm(ll a,ll b)
{
    
    
    return a*(b/gcd(a,b));
}
 
template <class T>
void read(T &x)
{
    
    
    char c;
    bool op = 0;
    while(c = getchar(), c < '0' || c > '9')
        if(c == '-')
            op = 1;
    x = c - '0';
    while(c = getchar(), c >= '0' && c <= '9')
        x = x * 10 + c - '0';
    if(op)
        x = -x;
}
template <class T>
void write(T x)
{
    
    
    if(x < 0)
        x = -x, putchar('-');
    if(x >= 10)
         write(x / 10);
    putchar('0' + x % 10);
}
ll qsm(int a,int b,int p)
{
    
    
    ll res=1%p;
    while(b)
    {
    
    
        if(b&1)
            res=res*a%p;
        a=1ll*a*a%p;
        b>>=1;
    }
    return res;
}
double mp[2005][2005];
double dis[N];
int vis[N];
int n,m,k;

void dj(int ts)
{
    
    
   
   
    //memset(vis,0,sizeof vis);
    dis[ts]=1;
    //vis[ts]=1;
    for(int i=1;i<=n;i++)
    {
    
    
        int t=-1;
        for(int j=1;j<=n;j++)
          if(!vis[j]&&(dis[t]<dis[j]||t==-1))
           t=j;
        vis[t]=1;
        for(int j=1;j<=n;j++)
        dis[j]=max(dis[j],dis[t]*mp[t][j]);

    }



}
int main()
{
    
    

     
     
      scanf("%d%d",&n,&m);

      for(int i=1;i<=m;i++)
      {
    
    
          int u,v,w;
          scanf("%d%d%d",&u,&v,&w);
          double z=(100.0-w)/100;
          mp[v][u]=mp[u][v]=max(mp[u][v],z);
              
      }
      int ts,te;
      scanf("%d%d",&ts,&te);
      dj(ts);
      printf("%.8lf",100.0/dis[te]);
       
      

    return 0;

}


Guess you like

Origin blog.csdn.net/qq_43619680/article/details/109549712