POJ 2949 Word Rings(差分约束)

来源:互联网 发布:北京恩知教育 编辑:程序博客网 时间:2024/05/17 00:57

思路:找平均值最大的环,将字符串前两个和最后两个当做两个结点,有一条边权值就是这个串的长度。

           那么avg = (w1+w2+....wk)/k,把k乘过去有avg*k = (w1+w2+...wk),然后有(w1-avg)+(w2-avg)+...(wk-avg)>=0,然后其实就是要求(w1-ans)+(w2-ans)+...(wk-ans)>0,二分ans然后求正环。

           用DFS找环会比BFS快上不少...另外这个题用hash比用map会快很多...


#include<iostream>#include<cstring>#include<string>#include<cstdio>#include<vector>#include<map>#include<algorithm>#include<queue>using namespace std;const int maxn = 100000+5;vector<pair<int,int> >e[maxn];map<string,int>mp;double d[maxn];int inq[maxn],cnt,n;bool dfs(int u,double avg){    inq[u]=1;for(int i = 0;i<e[u].size();i++){int v = e[u][i].first;if(d[v]<d[u]+e[u][i].second-avg){d[v]=d[u]+e[u][i].second-avg;if(inq[v]||dfs(v,avg))return true;}}inq[u]=0;return false;}bool check(double avg){memset(d,0,sizeof(d));memset(inq,0,sizeof(inq));for(int i = 1;i<=n;i++)if(dfs(i,avg))return true;return false;}int main(){    while(scanf("%d",&n)!=EOF && n){for(int i=0;i<=n;i++)e[i].clear();//mp.clear();string s;cnt=1;for(int i = 0;i<n;i++){string tmp="";             cin >> s;tmp+=s[0];tmp+=s[1];            if(!mp.count(tmp))mp[tmp]=cnt++;string tmp1="";tmp1+=s[s.size()-2];tmp1+=s[s.size()-1];if(!mp.count(tmp1))mp[tmp1]=cnt++;            e[mp[tmp]].push_back(make_pair(mp[tmp1],s.size()));}n = mp.size();double ans = -1;double l = 0,r=1000;for(int i = 0;i<100;i++){double mid = (l+r)/2;if(check(mid))l=mid,ans=mid;elser=mid;}if(ans==-1)printf("No solution.\n");elseprintf("%.2f\n",ans);}}

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

3intercommunicationalalkylbenzenesulfonatetetraiodophenolphthalein0

Sample Output

21.66

Hint

Huge input file, 'scanf' recommended to avoid TLE. 


0 0
原创粉丝点击