嗯...
题目链接:https://www.luogu.org/problem/P2384
这道题其实很水,对于用log做的大佬表示膜拜%%,这里其实就是一个简单的dijkstra,把加操作改为乘操作,然后注意把起点的最短路设为1,因为0乘任何数都得0...‘
AC代码:
1 #include<cstdio> 2 #include<iostream> 3 4 using namespace std; 5 const int inf = 0x3f3f; 6 int n, m, head[1000005], vis[1000005], cnt, dis[1000005]; 7 struct node{ 8 int from, to, next, val; 9 } g[1000005]; 10 11 inline void add(int u, int v, int w){ 12 g[++cnt].from = u; 13 g[cnt].to = v; 14 g[cnt].next = head[u]; 15 head[u] = cnt; 16 g[cnt].val = w; 17 } 18 19 inline void dijkstra(int x){ 20 for(int i = 1; i <= n; i++) dis[i] = (i == 1 ? 1 : inf);//注意起点 21 for(int i = 1; i <= n; i++){ 22 int t = 0, y = inf; 23 for(int j = 1; j <= n; j++) if(!vis[j] && dis[j] <= y) y = dis[t = j]; 24 vis[t] = 1; 25 for(int j = head[t]; j; j = g[j].next) dis[g[j].to] = min(dis[g[j].to], dis[t] * g[j].val); 26 } 27 printf("%d", dis[n] % 9987); 28 return; 29 } 30 31 int main(){ 32 int x, y, z; 33 scanf("%d%d", &n, &m); 34 for(int i = 1; i <= m; i++) {scanf("%d%d%d", &x, &y, &z); add(x, y, z);} 35 dijkstra(1); 36 return 0; 37 }
’