• Codeforces 1206 D


    D - Shortest Cycle
    思路:n大于某个值肯定有个三元环,否则floyd找最小环。
    代码:

    #pragma GCC optimize(2)
    #pragma GCC optimize(3)
    #pragma GCC optimize(4)
    #include<bits/stdc++.h>
    using namespace std;
    #define double long double
    #define y1 y11
    #define fi first
    #define se second
    #define pi acos(-1.0)
    #define LL long long
    //#define mp make_pair
    #define pb emplace_back
    #define ls rt<<1, l, m
    #define rs rt<<1|1, m+1, r
    #define ULL unsigned LL
    #define pll pair<LL, LL>
    #define pli pair<LL, int>
    #define pii pair<int, int>
    #define piii pair<pii, int>
    #define pdd pair<double, double>
    #define mem(a, b) memset(a, b, sizeof(a))
    #define debug(x) cerr << #x << " = " << x << "
    ";
    #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    //head
     
    const int N = 1e5 + 5;
    const int INF = 1e8;
    LL a[N];
    int mp[205][205], dis[205][205];
    int n, cnt;
    int main() {
        scanf("%d", &n);
        for (int i = 1; i <= n; ++i) scanf("%lld", &a[i]), cnt += a[i]==0;
        sort(a+1, a+1+n, greater<LL>());
        n -= cnt;
        if(n >= 200) {
            printf("3
    ");
            return 0;
        }
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= n; ++j) {
                if(i == j) continue;
                if(((a[i]&a[j]) != 0)) mp[i][j] = dis[i][j] = 1;
                else mp[i][j] = dis[i][j] = INF;
            }
        }
        int ans = INF;
        for (int k = 1; k <= n; ++k) {
            for (int i = 1; i < k; ++i) {
                for(int j = i+1; j < k; ++j) {
                    ans = min(ans, dis[i][j]+mp[j][k]+mp[k][i]);
                }
            }
            for (int i = 1; i <= n; ++i) {
                for (int j = 1; j <= n; ++j) {
                    dis[i][j] = min(dis[i][j], dis[i][k]+dis[k][j]);
                }
            }
        }
        if(ans == INF) printf("-1");
        else printf("%d
    ", ans);
        return 0;
    }
    
  • 相关阅读:
    寄存器总结
    汇编知识积累
    windbg 配置符号路径
    UNICODE环境下读写txt文件操作
    vs2010 安装项目完成桌面快捷方式无法定位程序文件夹 解决方法
    spring源码解析之属性编辑器propertyEditor
    springboot国际化与@valid国际化支持
    No identifier specified for entity
    springboot 项目中css js 等静态资源无法访问的问题
    Git教程
  • 原文地址:https://www.cnblogs.com/widsom/p/11378945.html
Copyright © 2020-2023  润新知