WA求助
查看原帖
WA求助
224927
lei_yu楼主2020/6/30 21:10
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char s[100001];
bool ok;
int cnt,head[100001],times[100001],n,maxi,d[100001],ccnt;
double dis[100001];
bool b[100001];
struct node
{
	int to,next;
	double dis;
}a[100001];
void add_edge(int from,int to,int ds)
{
	a[++cnt].to=to;
	a[cnt].dis=ds;
	a[cnt].next=head[from];
	head[from]=cnt;
}
void spfa(int u,double pjs)
{			
	if(ok)return;	
	times[u]++;
	//cout<<"u:"<<u<<"    times:"<<times[u]<<endl;
	if(times[u]>=ccnt)
	{
		ok=1;
		return;
	}
	b[u]=0;
	for(int i=head[u];i;i=a[i].next)
	{
		int v=a[i].to;
		//cout<<"比对:"<<"u:"<<u<<" v:"<<v<<" dis[v]:"<<dis[v]<<" dis[u]+a[i].dis-pjs"<<dis[u]+a[i].dis-pjs<<endl;
		if(dis[v]<dis[u]+a[i].dis-pjs)
		{
			dis[v]=dis[u]+a[i].dis-pjs;
			if(dis[v]>maxi)
			{
				ok=1;
				return;
			}
			if(!b[v])
			{
				b[v]=1;
				spfa(v,pjs);
			}
		}
	}
}
bool check(double x)
{
	ok=0;
	memset(dis,0,sizeof(dis));
	memset(b,0,sizeof(b));
	memset(times,0,sizeof(times));
	for(int i=1;i<=ccnt;i++)
	if(!b[d[i]])spfa(d[i],x);
	if(ok)return 1;
	else return 0;
}
int main()
{
	while(1)
	{
		ccnt=0;
		memset(d,0,sizeof(d));
		memset(head,0,sizeof(head));
		memset(a,0,sizeof(a));
		cnt=0;
		maxi=0;
		cin>>n;
		if(n==0)return 0;
		for(int i=1;i<=n;i++)
		{
			cin>>(s+1);
			int str=strlen(s+1);
			maxi=max(maxi,str);
			int a=s[1]-'a';
			int b=s[2]-'a';
			int x=a*10+b;
			a=s[str-1]-'a';
			b=s[str]-'a';
			int y=a*10+b;
			d[++ccnt]=x;
			d[++ccnt]=y;
			//cout<<"x:"<<x<<"  y:"<<y<<"    str:"<<str<<endl;
			add_edge(x,y,str);
		}
		/*cout<<endl;
		for(int i=1;i<=3;i++)
		{
			cout<<head[i]<<" ";
		}
		cout<<endl;*/
		double l=1,r=maxi;
		double ans=0;
		while(r-l>0.001)
		{
			double mid=(l+r)/2.0;
			//cout<<endl<<mid<<endl;
			if(check(mid))
			{
				ans=mid;
				l=mid;
			}
			else r=mid;
		}
		if(ans-1<0.001)cout<<"No solution."<<endl;
		else printf("%.2f\n",ans);
	}
}

(应该没人会看到吧)

2020/6/30 21:10
加载中...