1.建图:
一个比较直观的建图方式是将每个单词作为一个节点,如果这两个单词能够相连,则在这两个单词之间连接一条有向边,此时最多有105105个点,10101010条边,不能接受.
考虑一个对偶的建图方式,将每一个单词看作一条边,其开头两个字符和结尾两个字符为它两边的点.
这样建图的话,节点数就缩小到了676676个(26∗2626∗26),边数为105105条.
太妙了,简直不要太庙。这样得话,字符串中,除了开头前两个字母和后边两个字母。中间的字母没影响啊。只不过是长度罢了。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
// N是点数, M是边数。
const int N = 700, M = 100010;
int n;
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(st, 0, sizeof st);
memset(cnt, 0, sizeof cnt);
int hh = 0, tt = 0;
for (int i = 0; i < 676; i ++ )
{
q[tt ++ ] = i;
st[i] = true;
}
int count = 0;
while (hh != tt)
{
int t = q[hh ++ ];
if (hh == N) hh = 0;
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; // 经验上的trick
if (cnt[j] >= N) return true; // 走过的点超过了26*26,就存在环
if (!st[j])
{
q[tt ++ ] = j;
if (tt == N) tt = 0;
st[j] = true;
}
}
}
}
return false;
}
int main()
{
char str[1010];
while (scanf("%d", &n), n)
{
memset(h, -1, sizeof h);
idx = 0;
for (int i = 0; i < n; i ++ )
{
scanf("%s", str);
int len = strlen(str);
if (len >= 2)
{
// 两个字母映射到两位26进制数
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 = 1000;
while (r - l > 1e-4)
{
double mid = (l + r) / 2;
if (check(mid)) l = mid;
else r = mid;
}
printf("%lf\n", r);
}
}
return 0;
}
队列换成栈之后,效率更高了。
因为在队列中,一个点会把它更新的节点依次入队,当有环的时候,它需要遍历完队列中所有的节点才会发现。
但是栈不一样了。如果这个环只有三个节点,它很快就会找到了。
// 队列换成栈效率会更高。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
// N是点数, M是边数。
const int N = 700, M = 100010;
int n;
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(st, 0, sizeof st);
memset(cnt, 0, sizeof cnt);
int hh = 0, tt = 0;
for (int i = 0; i < 676; i ++ )
{
q[tt ++ ] = i;
st[i] = true;
}
int count = 0;
while (hh != tt)
{
int t = q[ -- tt ];
// 从对头弹,换成从数组尾部弹,甚至都不需要循环数组了。
// if (hh == N) hh = 0;
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; // 经验上的trick
if (cnt[j] >= N) return true; // 走过的点超过了26*26,就存在环
if (!st[j])
{
q[tt ++ ] = j;
// if (tt == N) tt = 0;
st[j] = true;
}
}
}
}
return false;
}
int main()
{
char str[1010];
while (scanf("%d", &n), n)
{
memset(h, -1, sizeof h);
idx = 0;
for (int i = 0; i < n; i ++ )
{
scanf("%s", str);
int len = strlen(str);
if (len >= 2)
{
// 两个字母映射到两位26进制数
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 = 1000;
while (r - l > 1e-4)
{
double mid = (l + r) / 2;
if (check(mid)) l = mid;
else r = mid;
}
printf("%lf\n", r);
}
}
return 0;
}