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.
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.
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;}
- hdu 1689 BFS+剪枝 求最小奇数环 很好很好的一道题目
- poj 3414 Pots(认为是一道很好的bfs题目)
- stl-map的一道很好的题目
- HDU 1015 Safecracker(一道很好的dfs)
- 很好的一道思维题目(有坑点的)
- poj2513 很好的一道题目 并查集+字典树
- HDU4101-很好的BFS题目(虽然结果是判断输赢)
- hdu 1412 搬寝室 (很好的一道dp题)
- hdu 1158(很好的一道dp题)
- 很好的一道树状数组
- 很好的一道面试题
- hdu 1254 推箱子 很好的嵌套搜索 双层BFS
- zoj 2110 很好的dfs+奇偶剪枝
- 很好的j2se面试题目
- POJ1011 搜索+剪枝【很好】
- POJ 3057 Evacuation(一道很好可以加强对二分图理解的题目)
- 一道很好的C语言题
- 一道很好的C语言题
- DP--hdu2084树塔
- App列表之圆角ListView(续)
- ecshop版本模板的放大镜,兼容ie6+ie7+ie8+ff
- ExchangeService(EWS)定位服务器的两种方式
- 编译运行第一个DirectFB程序
- hdu 1689 BFS+剪枝 求最小奇数环 很好很好的一道题目
- ecshop添加一个30天销量的方法
- 程序员怎样估算项目的进度时间
- MyEclipse 刷新网页内容不变
- ecshop模板中如何实现局部刷新
- ecshop模板的文章加上分页
- java单例模式
- hdu 1431 第一次打表留念~
- JavaScript实现X秒后自动跳转