A.the greed of Yehan-spfa最长路-log权值转化

Yehan在一次旅行中发现了一个神秘洞穴,洞口的告示牌承诺给予足够野心的人丰厚的回报。通过一系列边权计算,利用SPFA算法,本篇详细介绍了如何帮助Yehan找到从起点到终点的路径,使得获取金钱的最大权值乘积累计,并给出具体的实现代码。

A.the greed of Yehan

Description

During the trip, Yehan and Linlin pass a cave, and there is a board at the door, which says if you have enough ambition, you will get lots of money from me. At the beginning of the cave, you will get one dollar, and then, if you go from A to B (A and B are two vertexs of an edge, and C is the length of the edge), your money will be C times larger, for example, if you have x dollars, your dollars will be C*x dollars. And the next second, a directed map appears on the entrance of the cave. After that, Yehan thinks it is a good chance to make a big money, therefore, Yehan wants to calculate how much money she can get at most. Could you help Yehan calculate how much money she can get at most? Because of the result is so large, you should mod 1000000007

Input

The first line of input is 2 integers n, m (the number of vertex, the number of edges)The following m lines,(1<=n<=1e4,1<=m<=1e5)the i-th line contains 3 integers u, v, w (1<=w<=1e9)(two vertexs and the length of the edge), we guarantee that there is no cycle起点是1,终点是n

Output

The maximum of dollars Yehan can get after mod 1000000007

Sample Input 1 

3 3
1 2 100000
2 3 10001
1 3 100000
3 3
1 2 1
2 3 1
1 3 2

Sample Output 1

99993
2

求有向图,1到点n,权值乘积最大的取模

这里用spfa, 输入权值用-log带入

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<set>
#include<vector>
#include<sstream>
#include<queue>
#define ll long long
#define ull unsigned long long
#define PI 3.1415926535897932384626
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=1e5+10;

struct Edge{
    int to;
    int w;
    Edge(int _to=0,int _w=0):to(_to),w(_w){}
};
vector<Edge>E[maxn];
void addedge(int u,int v,int w)
{
    E[u].push_back(Edge(v,w));
}
/*int tot,head[maxn];
void init()
{
    tot=0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w)
{
    edge[tot].to=v;
    edge[tot].next=head[u];
    edge[tot].w=w;
    head[u]=tot++;
}*/
double dist[maxn];int vis[maxn];int cnt[maxn];
ll ans[maxn];
void spfa(int x,int n)
{
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++)dist[i]=inf;
    vis[x]=1;
    dist[x]=0;
    ans[x]=1;
    queue<int>que;
    while(!que.empty())que.pop();
    que.push(x);
    while(!que.empty())
    {
        int u=que.front();
        que.pop();
        vis[u]=0;
        for(int i=0;i<E[u].size();i++){
            int v=E[u][i].to;
            if(dist[v]>dist[u]-log(E[u][i].w)){
                dist[v]=dist[u]-log(E[u][i].w);
                ans[v]=(ans[u]*E[u][i].w)%1000000007;
            if(!vis[v]){
               vis[v]=1;
               que.push(v);
               }
            }
        }
    }
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int u,v,w;
        for(int i=1;i<=n;i++)E[i].clear();
        while(m--){
        scanf("%d%d%d",&u,&v,&w);
        addedge(u,v,w);
        }
        spfa(1,n);
        printf("%lld\n",ans[n]);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谷丘CODER

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值