P2384 最短路 洛谷

https://www.luogu.org/problem/show?pid=2384

题目背景

狗哥做烂了最短路,突然机智的考了Bosh一道,没想到把Bosh考住了...你能帮Bosh解决吗?

他会给你100000000000000000000000000000000000%10金币w

题目描述

给定n个点的带权有向图,求从1到n的路径中边权之积最小的简单路径。

输入输出格式

输入格式:

第一行读入两个整数n,m,表示共n个点m条边。 接下来m行,每行三个正整数x,y,z,表示点x到点y有一条边权为z的边。

输出格式:

输出仅包括一行,记为所求路径的边权之积,由于答案可能很大,因此狗哥仁慈地让你输出它模9987的余数即可。

废话当然是一个数了w

//谢fyszzhouzj指正w

对于20%的数据,n<=10。

对于100%的数据,n<=1000,m<=1000000。边权不超过10000。

输入输出样例

输入样例#1:
3 3
1 2 3 
2 3 3 
1 3 10
输出样例#1:
9

说明

好好看一看再写哟w

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cstdio>
 5 #include <queue>
 6 #define maxn 1000015
 7 
 8 using namespace std;
 9 
10 int n,m,tot,ans=1,x,y,z;
11 int head[maxn],dis[maxn];
12 bool vis[maxn]; 
13 struct node
14 {
15     int to,val,next;
16 }e[maxn*3];
17 
18 void add(int x,int y,int z)
19 {
20     tot++;
21     e[tot].to=y;
22     e[tot].val=z;
23     e[tot].next=head[x];
24     head[x]=tot;
25 }
26 
27 void spfa(int s)
28 {
29     for(int i=1;i<=n;i++)
30         dis[i]=maxn*100;
31     queue<int>que;
32     que.push(s);
33     dis[s]=1;vis[s]=1;
34     while(!que.empty())
35     {
36         int now=que.front();
37         que.pop();
38         vis[now]=0;
39         for(int i=head[now];i!=-1;i=e[i].next)
40             if(dis[e[i].to]>dis[now]*e[i].val)
41             {
42                 dis[e[i].to]=dis[now]*e[i].val;
43                 if(!vis[e[i].to])
44                 {
45                     que.push(e[i].to);
46                     vis[e[i].to]=1;
47                 }
48             }
49     }
50 }
51 
52 int main()
53 {
54     cin>>n>>m;
55     memset(head,-1,sizeof(head));
56     for(int i=1;i<=m;i++)
57     {
58         cin>>x>>y>>z;
59         add(x,y,z);
60     }
61     spfa(1);
62     cout<<dis[n]%9987;
63     return 0;
64 }
——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
原文地址:https://www.cnblogs.com/Shy-key/p/6538995.html