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]);
}
}