http://www.elijahqi.win/archives/1168
Description
A word ring is a sequence of words where the last two letters of each word are the same as the first two letters of the next word (and the last two letters of the last word are the same as the first two letters of the first word). For example, the following sequence is a word ring:
intercommunicational
alkylbenzenesulfonate
tetraiodophenolphthalein
Your task is to write a program that, given a list of words, finds a word ring. You have to make the word ring as impressive as possible: the average length of the words in the ring has to be as large as possible. In the above example, the average length is (20 + 21 + 24)/3 ≈ 21.6666 , which makes it somewhat impressive. Note that each word can be used at most once in the ring, and the ring can consist of a single word.
Input
The input contains several blocks of test cases. Each case begins with a line containing a single integer 1 ≤ n ≤ 100000 , the number of possible words that can be used. The next n lines contain these words. The words contain only the characters a'-
z’ and the length of each word is at most 1000.
The input is terminated by a block with n = 0 .
Output
For each test case in the input, you have to output a single number on a separate line: the maximum average length of a ring composed from (a subset of) the words given in the input. The average length should be presented as a real number with two digits of precision. If it is not possible to compose a ring from these words, then output `No solution.’ (without quotes). To avoid rounding problems, we accept solutions with a maximum of ±0.01 error.
Sample Input
3
intercommunicational
alkylbenzenesulfonate
tetraiodophenolphthalein
0
Sample Output
21.66
Hint
Huge input file, ‘scanf’ recommended to avoid TLE.
Source
Central Europe 2005
0/1分数规划。。
判断负环 如果一直不存在负环那么说明这题是无解的
把每个单词视为一条边,由开头两字母指向结尾两字母,边权为单词长度。则原问题转化为求一个最优比率环,可以利用二分答案+spfa判环来解决。点最多有26*26个,边最多1e5
设ans是最大值那么ans>=权值和/边数
即ans-边权>=0 若存在负环则说明ans过小 否则说明ans过大 注意误差
#include<cstdio>
#include<cstring>
#define N 110000
#define N1 900
struct node{
int y,next,z;
}data[N];
double f[N1];bool flag[N1];
int num,n,h[N1];char str1[1100];
inline void insert1(int x,int y,int z){
data[++num].y=y;data[num].z=z;data[num].next=h[x];h[x]=num;
}
inline int calc(char a,char b){return (a-'a')*26+b-'a'+1;}
inline bool judge1(double a,double b){
if (b-a>=1e-4) return true;else return false;
}
bool spfa(int x,double mid){
flag[x]=true;
for (int i=h[x];i;i=data[i].next){
int y=data[i].y,z=data[i].z;
if (f[x]+mid-z<f[y]){
f[y]=f[x]+mid-z;
if (flag[y]||spfa(y,mid)){flag[x]=false;return true;}
}
}flag[x]=false;return false;
}
inline bool judge(double ans){
memset(f,0,sizeof(f));
for (int i=1;i<=26*26;++i) if (spfa(i,ans)) return true;
return false;
}
int main(){
freopen("poj2949.in","r",stdin);
while (~scanf("%d",&n)&&n){
memset(h,0,sizeof(h));num=0;
for (int i=1;i<=n;++i){
scanf("%s",str1+1);int len=strlen(str1+1);
if (len<2) continue;
insert1(calc(str1[1],str1[2]),calc(str1[len-1],str1[len]),len);
}
double l=0,r=1000;
while (judge1(l,r)){
double mid=(l+r)/2;
if (judge(mid)) l=mid;else r=mid;
}
if (l==0) printf("No solution.\n");else
printf("%.2f\n",l);
}
return 0;
}