01分数问题
#include <bits/stdc++.h>
using namespace std;
const int N = 700, M = 1e5 + 10;
int h[N], e[M], ne[M], w[M], idx;
int n;
double dist[N];
int cnt[N];
bool st[N];
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}
bool check(double mid)
{
queue<int> q;
memset(dist, 0, sizeof dist);
memset(st, 0, sizeof st);
memset(cnt, 0, sizeof cnt);
for (int i = 0; i < 676; i ++)
{
q.push(i);
st[i] = true;
}
int count = 0;//用来结束spfa算法,如果count的计数大于某个数,我们判断图中一定有环
while (q.size())
{
int t = q.front();
q.pop();
st[t] = false;
for (int i = h[t]; ~i; i = ne[i])
{
int j = e[i];
if (dist[j] < dist[t] + w[i] - mid)
{
dist[j] = dist[t] + w[i] - mid;
cnt[j] = cnt[t] + 1;
if (++ count > 10000) return true;
if (cnt[j] >= N) return true;
if (!st[j])
{
q.push(j);
st[j] = true;
}
}
}
}
return false;
}
int main(void)
{
while (scanf("%d", &n), n)
{
memset(h, -1, sizeof h);
idx = 0;
char s[1010];
while (n --)
{
scanf("%s", s);
int len = strlen(s);
if (len >= 2)
{
int a = (s[0] - 'a') * 26 + s[1] - 'a';
int b = (s[len - 2] - 'a') * 26 + s[len - 1] - 'a';
add(a, b, len);
}
}
if (!check(0)) puts("No solution");
else
{
double l = 0, r = 1000;
while (r - l > 1e-4)
{
double mid = (l + r) / 2;
if (check(mid)) l = mid;
else r = mid;
}
printf("%lf\n", l);
}
}
return 0;
}