hdu 1689 BFS+剪枝 求最小奇数环 很好很好的一道题目

来源:互联网 发布:js实现在线客服功能 编辑:程序博客网 时间:2024/06/06 08:39

Alien’s Necklace

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


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
25 61 22 41 33 54 34 52 11 2
 

Sample Output
Case 1: JYY has to use 3 balls.Case 2: Poor JYY.题意: 输入一些边 问这些边组成的不小于3节点的最小奇数环 有多少个点

算法:BFS,假设每个节点在一个最小环上,枚举每个节点,以该节点为根节点,遍历这个图,突出其层次度,

对于某个点   第一层 遍历出所有从该店出发的点  在重新的点遍历下去 一层一层的    知道遍历到已经走过的点  那么最短的环就出来了

对于这个题  我主要感兴趣的地方是vector的应用    很新鲜    也又一次深刻的认识了BFS

#include<stdio.h>#include<string.h>#include<vector>#include<queue>using namespace std;const int N=1001;vector<int>edge[N];int vis[N][2],n;//vis[i][0]表示偶数步到达,vis[i][1]表示奇数步到达struct haha{int node;int dis;int pre;}now,next;int find(int s){        int i,sz;memset(vis,0,sizeof(vis));        queue<struct haha>que;now.pre=-1;now.dis=0;now.node=s;que.push(now);//vis[s][1]=1;while(!que.empty()){               now=que.front();   que.pop();   sz=edge[now.node].size();   for(i=0;i<sz;i++)   {    next.node=edge[now.node][i];   //防止出现2个点的环if(next.node==now.pre) continue;  if(now.dis%2==0)//偶数{                           if(vis[next.node][0]) //偶数   return vis[next.node][0]+now.dis+1;//偶数+偶数+1=奇数   else if(!vis[next.node][1])   vis[next.node][1]=now.dis+1;}else //奇数{                            if(vis[next.node][1]) //奇数return now.dis+vis[next.node][1]+1;//奇数+奇数+1=奇数else if(!vis[next.node][0])vis[next.node][0]=now.dis+1;}next.dis=now.dis+1;next.pre=now.node;que.push(next);   }}return n+100;}int main(){int cas,i,k,s,e,m,ans,mid;scanf("%d",&cas);for(k=1;k<=cas;k++){         scanf("%d %d",&n,&m); for(i=1;i<=n;i++) edge[i].clear(); for(i=1;i<=m;i++) { scanf("%d %d",&s,&e); edge[s].push_back(e); edge[e].push_back(s);  } ans=999999999; for(i=1;i<=n;i++) { mid=find(i); if(mid<ans) ans=mid; } if(ans!=n+100) printf("Case %d: JYY has to use %d balls.\n",k,ans); else printf("Case %d: Poor JYY.\n",k);}return 0;}



原创粉丝点击