analysis
首先想到是直接跑最短路,如果乘法不会溢出,不考虑取模,这道题就很水
但是一旦取模,就可能导致本来的较大距离偏小
这个时候就发现,由于新的松弛方程里面出现了乘法,我们可以用对数来将其转化为加法,转化后有一个好处就是避免了溢出的情况,由于边最长就10000,点最多就1000个,根本不需要考虑溢出的问题
于是先跑一次最短路求出最短路树(用父亲表示法),然后从点n倒推会点1,这个时候在去做取模乘法就可以了
code
#include<bits/stdc++.h>
using namespace std;
#define loop(i,start,end) for(register int i=start;i<=end;++i)
#define clean(arry,num) memset(arry,num,sizeof(arry))
#define anti_loop(i,start,end) for(register int i=start;i>=end;--i)
#define ll long long
template<typename T>void read(T &x){
x=0;char r=getchar();T neg=1;
while(r>'9'||r<'0'){if(r=='-')neg=-1;r=getchar();}
while(r>='0'&&r<='9'){x=(x<<1)+(x<<3)+r-'0';r=getchar();}
x*=neg;
}
int n,m;
const int maxn=1000+10;
const int maxm=1000000+10;
struct node{
int s;
int e;
double w;
int nxt;
}edge[maxm];
int cnt=0;
int head[maxn];
inline void addl(int u,int v,double w){
edge[cnt].e=v;
edge[cnt].s=u;
edge[cnt].w=w;
edge[cnt].nxt=head[u];
head[u]=cnt++;
}
double dis[maxn];
struct point{
int pos;
double dis;
point():pos(0),dis(0){}
point(int pos,double dis):pos(pos),dis(dis){}
friend bool operator<(point a,point b){
return a.dis>b.dis;
}
};
priority_queue<point>q;
int path[maxn];
void dijkstra(){
clean(path,0);
loop(i,1,n)
dis[i]=1e9;
dis[1]=0;
q.push(point(1,0));
while(q.empty()==false){
int f=q.top().pos;
q.pop();
for(int i=head[f];i!=-1;i=edge[i].nxt){
int v=edge[i].e;
if(dis[v]>dis[f]+log(edge[i].w)){
path[v]=i;
dis[v]=dis[f]+log(edge[i].w);
q.push(point(v,dis[v]));
}
}
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("datain.txt","r",stdin);
#endif
clean(head,-1);
read(n);
read(m);
loop(i,1,m){
int xi,yi,zi;
read(xi);
read(yi);
read(zi);
addl(xi,yi,(double)zi);
}
dijkstra();
ll res=1;
for(int p=n;p!=1;p=edge[path[p]].s)
res=((int)edge[path[p]].w*res)%9987;
printf("%lld\n",res);
return 0;
}