Luogu P2384 shortest path problem solution

Topic link

A question of comparison routines, the method of this question can also be used to make the maximum product independent set of trees

Since this question needs to take the modulus,
we cannot find the shortest path directly. We consider taking the logarithm of each edge, that is, the edge weight is wwThe edge of w becomeslog ⁡ 2 w \log_2wlog2w In
this way, according to the nature of the power, we will convert the product shortest path into the ordinary shortest path. It is obvious that
we must remember to change the edge weight back to output.

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
const long long Maxn=1010,Maxm=1000000+10;
const long long inf=0x3f3f3f3f;
const long long mod=9987;
long long nxt[Maxm],to[Maxm];
long long val[Maxm],head[Maxn];
double len[Maxm],dis[Maxn];
long long dist[Maxn];
bool vis[Maxn];
long long n,m,edgecnt;
struct node{
    
    
	long long pos;
	double dis;
	node(long long x,double y)
	{
    
    
		pos=x,dis=y;
	}
	bool operator <(const node &x)const
	{
    
    
		return x.dis<dis;
	}
};
inline long long read()
{
    
    
	long long s=0,w=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
    
    if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0' && ch<='9')s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
	return s*w;
}
inline void add(long long x,long long y,double c,long long v)
{
    
    
	++edgecnt;
	nxt[edgecnt]=head[x];
	to[edgecnt]=y;
	len[edgecnt]=c;
	val[edgecnt]=v;
	head[x]=edgecnt;
}
void dij(long long s)
{
    
    
	priority_queue <node> q;
	fill(dis+1,dis+1+n,inf*1.0);
	dis[s]=0.0,dist[s]=1ll,vis[s]=1;
	q.push(node(s,0.0));
	while(q.size())
	{
    
    
		long long x=q.top().pos;
		q.pop();
		for(long long i=head[x];i;i=nxt[i])
		{
    
    
			long long y=to[i];
			if(dis[y]>dis[x]+len[i])
			{
    
    
				dis[y]=dis[x]+len[i];
				dist[y]=(dist[x]*val[i])%mod;
				if(!vis[y])vis[y]=1,q.push(node(y,dis[y]));
			}
		}
	}
}
int main()
{
    
    
	n=read(),m=read();
	for(long long i=1;i<=m;++i)
	{
    
    
		long long x=read(),y=read(),z=read();
		add(x,y,log2(z),z);
	}
	dij(1);
	printf("%lld\n",dist[n]);
	return 0;
}

Guess you like

Origin blog.csdn.net/Brian_Pan_/article/details/109246921