B. Shortest Cycle without seeking the smallest ring of FIG.

 

 

The meaning of problems:
Given n points, each point has a weight value a [i], if a [u] & a [vthen it may be between (u, v) an even edge, seeking! Finally, FIG smallest ring (the ring formed by several dots)

 

Solution:
logical operation is an operation under the & binary, each weight value of the subject to a [i] is the maximum range of 1018, i.e., 64-bit binary up next.
64 If there is a certain number of occurrences greater than 1 2, then it is clear that the smallest ring is 3 (the position loop).
Another way, in the worst case, the number n is given, which is not more than 128 to the number 0, then the answer must be 3 (128 because when the number is not 0, each 64-bit the number of bits is 2, then as long as the number is not casually to 0, there will be greater than 2 bits, constituting the ring 3)

so long as we do not consider the case where the number is less than the number of 0 on the line 130 , n this time is very small, and can be used for the minimum dfs searching or rings Flyod.

#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<math.h>
#define ll long long
#define mx 0x3f3f3f3f
using namespace std;
ll way[250][250],dis[250][250],num[250];
ll n,m,ans;
void init()
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(i!=j&&num[i]&num[j])
                way[i][j]=dis[i][j]=1;
            else
                way[i][j]=dis[i][j]=mx;
        }
    }
}
void floyd()
{
    ans=mx;
    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]+way[i][k]+way[k][j]);//接成环
        }

        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]);
    }
}
int main()
{
    while(~scanf("%lld",&n))
    {
        ll cnt=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&num[i]);
            if(num[i])
                cnt++;
        }
        if(cnt>130)
            printf("3\n");
        else
        {
            init();
            floyd();
            if(ans==mx)
                printf("-1\n");
            else
                printf("%lld\n",ans);
        }
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/-citywall123/p/11461636.html