zoukankan      html  css  js  c++  java
  • POJ 2949 Word Rings

    传送门

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

    方法类似 Sightseeing Cows,通过移项构造环路判定,二分搜索答案缩小目标精度,注意使用 tot >= n * 2stack 代替 queue 玄学优化。

    #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
    ", low);
            }
        }
        return 0;
    }
    
  • 相关阅读:
    隧道适配器,本地连接过多的解决办法
    C# 遍历HashTable
    sql2005 数据库转为sql 2000数据库的步骤
    自动扫描IP代理地址和自动切换IP的软件
    JS实现网页图片延迟加载[随滚动条渐显]
    批量修改hosts
    C#.NET获取当前月份最大天数
    如何让js调用不影响页面的加载速度?
    在sql中如何替换去掉回车符?
    Linq(01)对Linq的疑问及学习路径
  • 原文地址:https://www.cnblogs.com/singularity2u/p/13850078.html
Copyright © 2011-2022 走看看