p2384 最短路

添加链接描述
乘法最短路

#include<bits/stdc++.h>
#define ll long long
#define ios ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);
using namespace std;
const int maxn=1e6+2,INF=0x7f7f7f7f,mod=9987;
struct edge
{
    int to,w;
    edge(int a,int b)
    {
        to=a;
        w=b;
    }
};
struct node
{
    int num;
    double dist;
    node(int a,double b)
    {
        num=a;
        dist=b;
    }
    bool operator <(const node x)const
    {
        return dist>x.dist;
    }
};
double d[maxn];
int vis[maxn],pre[maxn][2],n,m;
vector<edge>g[maxn];
priority_queue<node>q;
void dij(int s)
{
    for(int i=0;i<=n;i++)
        d[i]=INF;
    //memset(d,0x7f,sizeof(d));
    //cout<<d[0]<<endl;
    d[s]=0;
    q.push(node(s,0));
    while(!q.empty())
    {
        node t=q.top();
        q.pop();
        if(vis[t.num]==1)
            continue;
        vis[t.num]=1;
        for(int i=0;i<g[t.num].size();i++)
        {
            edge e=g[t.num][i];
            //cout<<t.num<<" "<<e.to<<" "<<log10(e.w)+d[t.num]<<" "<<d[e.to]<<endl;
            if(log10(e.w)+d[t.num]<d[e.to])
            {
                d[e.to]=log10(e.w)+d[t.num];
                //cout<<e.w<<" "<<t.num<<" "<<e.to<<endl;
                pre[e.to][0]=t.num;
                pre[e.to][1]=e.w;
                q.push(node(e.to,d[e.to]));
            }
        }
    }
}
int main()
{
    ios;
    cin>>n>>m;
    for(int i=0;i<m;i++)
    {
        int u,v,w;
        cin>>u>>v>>w;
        g[u].push_back(edge(v,w));
    }
    dij(1);
    int ans=1,pos=n;
    while(pos!=1)
    {
        //cout<<pre[pos][1]<<endl;
        ans*=pre[pos][1];
        ans%=mod;
        pos=pre[pos][0];
    }
    cout<<ans<<endl;
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值