1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| #include <iostream> #include <algorithm> #include <cstring>
using namespace std;
const int N = 700, M = 100010;
int n, m; int h[N], e[M], w[M], ne[M], idx; double dist[N]; int q[N], 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) { memset(cnt, 0, sizeof cnt); memset(st, 0, sizeof st); int tt = 0; for (int i = 0; i < 676; i ++ ) { q[ ++ tt] = i; st[i] = true; } int count = 0; while(tt) { int t = q[tt -- ]; 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(cnt[j] >= N) return true; if(!st[j]) { st[j] = true; q[ ++ tt] = j; } } } } return false; }
int main() { while(scanf("%d", &n), n) { memset(h, -1, sizeof h); idx = 0; char str[1010]; for (int i = 0; i < n; i ++ ) { scanf("%s", str); int len = strlen(str); if(len >= 2) { int left = (str[0] - 'a') * 26 + str[1] - 'a'; int right = (str[len - 2] - 'a') * 26 + str[len - 1] - 'a'; add(left, right, len); } } if(!check(0)) puts("No solution"); else { double l = 0, r = 1010; while(r - l > 1e-4) { double mid = (l + r) / 2; if(check(mid)) l = mid; else r = mid; } printf("%lf\n", r); } } return 0; }
|