[Brush] [title] luogu P2384 Shortest Shortest

Given n points weighted directed graph, find the path from 1 to n of the product of the smallest edge weights of simple paths.

(From a solution to a problem)

First consider violence to maintain, apparently extreme data will burst, then what is there to maintain it?

Since the log (n * m) = log (n) + log (m)

The OK , this question is over

We just put the product into a log, and then restore it, because of the precision required, so it is still better record path.

#include<cstdio>
#include<cstdlib>
#include<vector>
#include<cstring>
#include<queue>
#include<cmath>
using namespace std;
int n,m;
const int N=1003,inf=1<<30;
struct node
{
    int v;double w;
    node(int vv,double ww)
    { v=vv,w=ww; }
    node(){}
};
vector <node > g[N];
int sz[N],path[N][2];
double dis[N];

struct nd
{
    int v;double d;
    nd(int vv,double dd)
    { v=vv,d=dd; }
    nd(){}
    bool operator < (const nd & o) const
    { return d>o.d; }
};
priority_queue <nd> q;
void dijk()
{
    for(int i=2;i<=n;i++) dis[i]=inf;
    q.push(nd(1,0)); 
    
    while(!q.empty() )
    {
        int t=q.top() .v;double dd=q.top() .d;
        q.pop() ;
        if(dd!=dis[t]) continue;
        if(t==n) break;
        
        for(int i=0;i<sz[t];i++)
        {
            int nx=g[t][i].v ;
            if(dis[nx] > dis[t]+log(g[t][i].w ) )
            {
                dis[nx]=dis[t]+log(g[t][i].w );
                path[nx][0]=t,path[nx][1]=g[t][i].w ;
                q.push(nd(nx,dis[nx])); 
            }
        }
    }
    
}

int ans=1,mod=9987;
void get_path()
{
    int v=n;
    while(v!=1)
    {
        ans=(ans*path[v][1])%mod;
        v=path[v][0];
    }
}

int main()
{
    scanf("%d%d",&n,&m);
    int u,v,w;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&u,&v,&w);
        g[u].push_back(node(v,w)); 
    }
    for(int i=1;i<=n;i++)
        sz[i]=g[i].size() ;
    
    dijk();
    get_path();
    printf("%d\n",ans);
    
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/xwww666666/p/11670585.html