WA 求调
查看原帖
WA 求调
678012
yuchuanqi楼主2024/4/17 21:08
#include<iostream>
#include<cstring>
#include<queue>
#define epts 1e-5
using namespace std;
typedef double db;
const int maxm=1e6,maxn=1e6;
struct edge{
	int to,nxt,w;
} e[maxm+maxn];
int n,head[maxn],cnt,path[maxn],a[maxn],L;
char st[1010];
bool vis[maxn];
db dis[maxn];
deque <int> q;
void add(int u,int v,int w){
	e[++cnt]={v,head[u],w};
	head[u]=cnt;
}
void init(){
	L=0;
	memset(head,-1,sizeof head);
	for(int i=1;i<=cnt;++i) e[i]={};
	cnt=0;
}
bool spfa(db mid){
	while(!q.empty()) q.pop_back();	
	for(int i=1;i<=L;++i){
		dis[a[i]]=path[a[i]]=0;
		vis[a[i]]=1;
		q.push_back(a[i]);
	}
	while(!q.empty()){
		int now=q.front();
		q.pop_front();
		vis[now]=0;
		for(int i=head[now];~i;i=e[i].nxt){
			int v=e[i].to;db w=mid-e[i].w;
			if(dis[now]+w<dis[v]){
				dis[v]=dis[now]+w;
				path[v]=path[now]+1;
				if(path[v]>=L) return true;
				if(!vis[v]){
					vis[v]=1;
					if(!q.empty()){
						if(dis[v]>dis[q.front()]) q.push_back(v);
						else q.push_front(v);
					}
					else q.push_back(v);
				}
			}
		}
	}
	return false;
}
int main(){
	while(cin>>n){
		if(!n) break;
		init();
		int len,x,y,tmp;
		while(n--){
			scanf("%s",st);
			len=strlen(st);
			if(len<2) continue;
			x=(st[0]-'a'+1)*26+st[1]-'a'+1,y=(st[len-2]-'a'+1)*26+st[len-1]-'a'+1;
			add(x,y,len);
			tmp=max(tmp,len);
			if(!vis[x]){
				vis[x]=1;
				a[++L]=x;
			} 
			if(!vis[y]){
				vis[y]=1;
				a[++L]=y;
			} 
		}
		db l=0,r=tmp,mid;
		bool flag=0;
		while(r-l>=epts){
			mid=(l+r)/2;
			if(spfa(mid)){
				l=mid;
				flag=1;
			}
			else{
				r=mid;
			}
		}
		if(flag) printf("%.2f\n",l);
		else printf("No solution.\n");
	}
	return 0;
}
2024/4/17 21:08
加载中...