首页 技术 正文
技术 2022年11月18日
0 收藏 658 点赞 3,215 浏览 1579 个字

题意:给定N个字符串,如果A串的最后两个字母跟B串的前两个字母相同它们就能连接。

求一个由字符串组成的首尾相连的环,使(字符串总长度/字符串个数)最大。

n<=100000 len<=1000

思路:SPFA国家队论文题

【POJ2949】Word Rings(最大平均值环)

赋所有dis[i]=0,跑最长路,如果某个元素入队次数超过点数就说明有正环。

使用DFS版本的SPFA做比BFS快10倍,为什么?

论文里高大上看不懂,蒟蒻用简单粗暴的方法想了一下:

如果某个环有K个点组成,BFS会从K个点中的某个点开始,每次都换一个点扩展一次,可能达到O(N*K^2)

DFS则只选其中的一个点不断扩展,K次就能使自己重新入队一次,这样就是O(N*K)

孰优孰劣一眼分明

以下是论文原文,用来证明初始值=0的正确性

【POJ2949】Word Rings(最大平均值环)

 var q:array[..]of longint;
dis:array[..]of double;
head,vet,next,len,time,inq:array[..]of longint;
n,m,tot,i,x,y,k:longint;
l,r,mid,last,eps:double;
ch:ansistring; procedure add(a,b,c:longint);
begin
inc(tot);
next[tot]:=head[a];
vet[tot]:=b;
len[tot]:=c;
head[a]:=tot;
end; function spfa(k:double):boolean;
var u,top,i,e,v:longint;
begin
top:=;
for i:= to * do
begin
inq[i]:=; dis[i]:=; time[i]:=;
end;
for i:= to * do
if head[i]> then
begin
inc(top); q[top]:=i; inq[i]:=; inc(time[i]);
end;
while top> do
begin
u:=q[top]; dec(top);
inq[u]:=;
e:=head[u];
while e<> do
begin
v:=vet[e];
if dis[u]+len[e]-k>dis[v] then
begin
dis[v]:=dis[u]+len[e]-k;
if inq[v]= then
begin
inc(top); q[top]:=v; inq[v]:=;
inc(time[v]);
if time[v]>n then exit(true);
end;
end;
e:=next[e];
end;
end;
exit(false);
end; begin
assign(input,'poj2949.in'); reset(input);
assign(output,'poj2949.out'); rewrite(output);
while not eof do
begin
readln(n);
if n= then exit;
fillchar(head,sizeof(head),); tot:=;
for i:= to n do
begin
readln(ch); k:=length(ch);
x:=(ord(ch[])-ord('a'))*+ord(ch[])-ord('a')+;
y:=(ord(ch[k-])-ord('a'))*+ord(ch[k])-ord('a')+;
add(x,y,k);
end;
n:=;
for i:= to * do
if head[i]> then inc(n);
l:=; r:=; last:=;
eps:=1e-5;
while r-l>eps do
begin
mid:=(l+r)/;
if spfa(mid) then begin last:=mid; l:=mid; end
else r:=mid;
end;
if last<eps then writeln('No solution.')
else writeln(last::); end;
close(input);
close(output);
end.
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,991
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,505
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,349
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,134
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,766
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,844