SPFA为啥死了
查看原帖
SPFA为啥死了
61480
爱晚亭哦楼主2020/8/27 16:37

看题解SPFA似乎过了,但我的SPFA只有TLE 80/kk

#include<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=2001;
const int M=100001;
int vis[N];
double dis[N],w[M];
int head[M],to[M],nx[M];
int summ,n,m;
inline int read()
{
	int ans=0;
	char c=getchar();
	while(c<'0'||c>'9')
		c=getchar();
	while(c>='0'&&c<='9')
	{
		ans=(ans<<3)+(ans<<1)+(c^48);
		c=getchar();
	}
	return ans;
}
void add(int x,int y,double z)
{
	nx[++summ]=head[x];
	to[summ]=y;
	w[summ]=1-z;
	head[x]=summ;
}
void spfa(int s)
{
	queue <int> t;
	for(int i=1;i<=n;i++)
	{
		dis[i]=0.0;
		vis[i]=0;
	}
	t.push(s);
	dis[s]=1.0;
	vis[s]=1;
	while(!t.empty())
	{
		int u=t.front();
		vis[u]=0;
		t.pop();
		for(int i=head[u];i;i=nx[i])
		{
			int v=to[i];
			if(dis[v]<dis[u]*w[i])
			{
				dis[v]=dis[u]*w[i];
				if(!vis[v])
				{
					vis[v]=0;
					t.push(v);
				}
			}
		}
	}
}
int main()
{
	int x,y,z;
	n=read();
	m=read();
	for(int i=1;i<=m;i++)
	{
		x=read();
		y=read();
		z=read();
		add(x,y,(double)(1.0*z/100));
		add(y,x,(double)(1.0*z/100));
	}
	x=read();
	y=read();
	spfa(x);
	printf("%.8lf",100/dis[y]);
}
2020/8/27 16:37
加载中...