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

Alien’s Necklace

Time Limit: 10000/5000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1405Accepted 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
 
 
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节点的最小奇数环 有多少个点

算法: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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值