zoukankan      html  css  js  c++  java
  • hdu 1689 Alien’s Necklace (bfs层次图剪枝)

    Alien’s Necklace

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 1526    Accepted Submission(s): 415


    Problem Description
    JYY is taking a trip to Mars. To get accepted by the Martians, he decided to make a magic necklace for their king. (Otherwise, JYY will be eaten) Now, he has collected many magic balls, and he is going to string them up.
    Unfortunately, only particular pairs of balls can be adjacent in the necklace, otherwise they will explode. Notice that the first and the last ball in the necklace are also adjacent. Besides, the Martians think even numbers are unlucky, so the number of balls in the necklace must be odd. (Of course each ball can be used only once)
    A necklace contains at least 3 balls. Because the balls are really precious, JYY wants the necklace has as few balls as possible. (Then he can give rest balls to his GF)
    So JYY wonders the number of balls he has to use to make this necklace.
     

    Input
    The input consists of several test cases. There is a single number above all, the number of cases. There are no more than 20 cases.
    For each input, the first line contains 2 numbers N and M, N is the number of balls JYY collected, and M is the pairs of compatible balls. Balls are numbered from 1 to N. Followed M lines, each contains 2 numbers A and B, means that ball A and ball B are compatible. For each case, 0 < N <= 1,000, 0 < M <= 20,000.
     

    Output
    If the gift can't be done, just print "Poor JYY." in a line, otherwise, print the minimal number of balls in the necklace. Use the format in the example.
     

    Sample Input
    2 5 6 1 2 2 4 1 3 3 5 4 3 4 5 2 1 1 2
     

    Sample Output
    Case 1: JYY has to use 3 balls. Case 2: Poor JYY.
     

    题意:让找到一个环使得组成环的点数为奇数且点数至少为3,。

    由于一个点能够多个途径到达,但从一点出发第一次到达的肯定是最短路径,又题目要求的奇偶性,每次到达一个点步数的奇偶性进行标记。

    #include<stdio.h>
    #include<algorithm>
    #include<queue>
    #include<string.h>
    #include<vector>
    using namespace std;
    #define N 1005
    #define ll __int64
    const int inf=0x7fffffff;
    vector<int>g[N];
    int vis[N][2];
    struct node
    {
        int u,t;
        friend bool operator<(node a,node b)
        {
            return a.t>b.t;
        }
    };
    int bfs(int s)
    {
        int i,u,v;
        priority_queue<node >q;
        node cur,next;
        cur.u=s;
        cur.t=1;
        memset(vis,0,sizeof(vis));
        vis[s][1]=1;       //奇数点。用一个珠子
        q.push(cur);
        while(!q.empty())
        {
            cur=q.top();
            q.pop();
            u=cur.u;
            for(i=0;i<g[u].size();i++)
            {
                next.u=v=g[u][i];
                next.t=cur.t+1;
                if(v==s&&next.t%2==0&&next.t>3)   //结点处到达两次。故应该减一
                    return next.t-1;
                if(!vis[v][next.t%2])
                {
                    vis[v][next.t%2]=1;
                    q.push(next);
                }
            }
        }
        return inf;
    }
    int main()
    {
        int i,n,m,u,v,tt,cnt=0;
        scanf("%d",&tt);
        while(tt--)
        {
            scanf("%d%d",&n,&m);
            for(i=1;i<=n;i++)
                g[i].clear();
            while(m--)
            {
                scanf("%d%d",&u,&v);
                g[u].push_back(v);
                g[v].push_back(u);
            }
            int ans=inf;
            for(i=1;i<=n;i++)
                ans=min(ans,bfs(i));
            if(ans==inf)
                printf("Case %d: Poor JYY.
    ",++cnt);
            else
                printf("Case %d: JYY has to use %d balls.
    ",++cnt,ans);
        }
        return 0;
    }


    以下这样的方法是用一个数组记录到达该点的步数,当再次訪问该点时。说明出现环,直接推断奇偶性就能够了。

    又要求所用珠子数目大于三,所以要用一个pre变量记录上一个节点的标号,推断是否是两点直接成环。

    #include<stdio.h>
    #include<algorithm>
    #include<queue>
    #include<string.h>
    #include<vector>
    using namespace std;
    #define N 1005
    #define ll __int64
    const int inf=0x7fffffff;
    vector<int>g[N];
    int vis[N][2];
    int ans;
    struct node
    {
        int u,t,pre;
        friend bool operator<(node a,node b)
        {
            return a.t>b.t;
        }
    };
    int bfs(int s)
    {
        int i,u,v,t;
        priority_queue<node >q;
        node cur,next;
        cur.u=s;
        cur.t=1;
        cur.pre=-1;
        memset(vis,0,sizeof(vis));
        vis[s][1]=1;
        q.push(cur);
        while(!q.empty())
        {
            cur=q.top();
            q.pop();
            u=cur.u;
            for(i=0;i<g[u].size();i++)
            {
                next.u=v=g[u][i];
                next.t=cur.t+1;
                next.pre=u;
                if(v==cur.pre)
                    continue;
                if(vis[v][0])  //偶数点
                {
                    t=cur.t+vis[v][0];
                    if(t%2==0)
                        return t-1;
                }
                else if(vis[v][1])
                {
                    t=cur.t+vis[v][1];
                    if(t%2==0)
                        return t-1;
                }
                else
                {
                    vis[v][next.t%2]=next.t;
                    q.push(next);
                }
            }
        }
        return inf;
    }
    int main()
    {
        int i,n,m,u,v,tt,cnt=0;
        scanf("%d",&tt);
        while(tt--)
        {
            scanf("%d%d",&n,&m);
            for(i=1;i<=n;i++)
                g[i].clear();
            while(m--)
            {
                scanf("%d%d",&u,&v);
                g[u].push_back(v);
                g[v].push_back(u);
            }
            ans=inf;
            for(i=1;i<=n;i++)
                ans=min(ans,bfs(i));
            if(ans==inf)
                printf("Case %d: Poor JYY.
    ",++cnt);
            else
                printf("Case %d: JYY has to use %d balls.
    ",++cnt,ans);
        }
        return 0;
    }


  • 相关阅读:
    android 模拟点击事件
    IGZO显示屏
    java 代码混淆
    android 模拟点击3
    android 文件读写
    android 抓包 tcpdump
    pathon 2
    conversion to dalvik format failed with error 1
    zipalign
    android 来电状态
  • 原文地址:https://www.cnblogs.com/zhchoutai/p/8597677.html
Copyright © 2011-2022 走看看