zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    php 扩展模块安装过程
    nginx 配置若干问题
    df 显示 100%的问题
    pcre 不支持 utf 的问题
    服务器TCP连接中 TIME_WAIT 状态过多
    Linux 运维笔试题(一)答案
    Linux 运维笔试题(一)
    Java 内存监控(一)之 jps命令
    python 入门
    Java 内存监控命令简介(零)
  • 原文地址:https://www.cnblogs.com/widsom/p/11378945.html
Copyright © 2011-2022 走看看