其他分享
首页 > 其他分享> > POJ 2949 Word Rings

POJ 2949 Word Rings

作者:互联网

传送门

AcWing 1165 单词环洛谷 SP2885 WORDRING - Word RingsLOJ 10082 Word Rings

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <cmath>

using namespace std;
typedef long long ll;
typedef pair<int, int> p;
const int maxn = 26 * 26;
const int maxm = 1e5 + 10;
char str[maxm];
bool vis[maxn];
double dis[maxn];
int ecnt, head[maxn], cnt[maxn];

struct edge {
    int to, wt, nxt;
} edges[maxm];

template<typename T>
inline const T read()
{
    T x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 3) + (x << 1) + ch - '0';
        ch = getchar();
    }
    return x * f;
}

template<typename T>
inline void write(T x, bool ln)
{
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) write(x / 10, false);
    putchar(x % 10 + '0');
    if (ln) putchar(10);
}

inline int getId(char a, char b)
{
    return (a - 'a') * 26 + b - 'a';
}

void addEdge(int u, int v, int w)
{
    edges[ecnt].to = v;
    edges[ecnt].wt = w;
    edges[ecnt].nxt = head[u];
    head[u] = ecnt++;
}

bool spfa(int n, double val)
{
    queue<int> q;
    memset(cnt, 0, sizeof cnt);
    memset(vis, false, sizeof vis);
    for (int i = 0; i < maxn; ++i) {
        dis[i] = 0;
        vis[i] = true;
        q.push(i);
    }
    int tot = 0;
    while (not q.empty()) {
        int u = q.front();
        q.pop();
        vis[u] = false;
        for (int i = head[u]; compl i; i = edges[i].nxt) {
            int v = edges[i].to, w = edges[i].wt;
            if (dis[v] < dis[u] + w - val) {
                dis[v] = dis[u] + w - val;
                if (not vis[v]) {
                    vis[v] = true;
                    q.push(v);
                    cnt[v] = cnt[u] + 1;
                    if (cnt[v] >= n or ++tot > n * 2) {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("input.txt", "r", stdin);
#endif
    while (true) {
        int n = read<int>();
        if (not n) break;
        ecnt = 0;
        memset(head, -1, sizeof head);
        for (int i = 0; i < n; ++i) {
            scanf("%s", str);
            int len = (int)strlen(str);
            if (len < 2) continue;
            int u = getId(str[0], str[1]), v = getId(str[len - 2], str[len - 1]);
            addEdge(u, v, len);
        }
        double low = 0, high = 1e3;
        while (high - low > 1e-4) {
            double mid = (low + high) / 2;
            if (spfa(n, mid)) {
                low = mid;
            } else {
                high = mid;
            }
        }
        if (low == 0.0) {
            puts("No solution.");
        } else {
            printf("%.2f\n", low);
        }
    }
    return 0;
}

标签:ch,Word,int,Rings,vis,edges,POJ,str,maxn
来源: https://www.cnblogs.com/singularity2u/p/13850078.html