题目链接:UVa12726

是个PDF,不好复制进来。

大意:有个人要追个妹子,想加妹子QQ,但是不知道谁规定的,玩QQ的人要加好友必须先要有至少k个共同好友。共有N个人玩QQ,编号为1到N,1是男主,N是妹子。有M个初始好友关系,求男主最少要加多少个人才能有资格加妹子,或者永远加不到妹子。

题解:

最少加多少个人加到妹子,可以想到广搜,状态是男主的加好友情况。但广搜却不能一个人只进队一次,因为从不同的路径加到这个人,结果是不同的,让我们来看看这个情况:

N=8,M=11,K=2,男主需要加到6,再加到7,才能加到女主;但如果宽搜时每次加第i个新好友的情况只进队一次,则男主会同时加到6和7,然后从加6的情况或者加7的情况都加不到女主,又不能从6跳到7或者从7跳到6,因为他们已经进过队了,这样就逗乐。

但是广搜不剪枝会超时,各种各样的路线搞出来的情况还是很多的。所以我们要根据状态来,状态是男主的加好友情况(和哪些人是好友,因为只有20个人玩QQ,可以用一个int的20个二进制位来存),dis[x]记录状态x的走过的距离,如果再次走到状态x时,距离不小于上次的距离,则不用再进这个状态了。

我用的20个bool来存的状态,结果比全用位的慢0.5s,大概我转成int来操作dis[]的时候耗时太多。

代码:

 #include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
#define ll __int64
#define usint unsigned int
#define mz(array) memset(array, 0, sizeof(array))
#define minf(array) memset(array, 0x3f, sizeof(array))
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,x,n) for(int i=(x);i<=(n);i++)
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define WN(x) printf("%d\n",x);
#define RE freopen("D.in","r",stdin)
#define WE freopen("1.out","w",stdout) const int maxn=; struct arr{
bool a[maxn+];
int dis;
}a[maxn]; int n,m,k;
int dis[<<maxn]; int atox(arr b)
{
int re=;
for(int i=;i<=n;i++)
re=(re<<)|b.a[i];
return re;
} int count_same(int x,int y)
{
int cnt=;
for(int i=;i<=n;i++)
if(a[x].a[i] && a[y].a[i])cnt++;
return cnt;
} int gank()
{
int i;
if(a[].a[n])return ;
queue<arr>q;
a[].dis=;
q.push(a[]);
while(!q.empty())
{
a[]=q.front();
q.pop();
for(i=;i<=n;i++)
{
if(!a[].a[i] && count_same(,i)>=k)
{
arr next=a[];
next.a[i]=;
next.dis++;
if(dis[atox(next)]<=next.dis)continue;
if(i==n) return a[].dis;
q.push(next);
}
}
}
return -;
} int main()
{
int cas=,i,T;
scanf("%d",&T);
while(T--)
{
RD3(n,m,k);
for(i=;i<=n;i++)
memset(a[i].a,,sizeof(a[i].a));
for(i=;i<=m;i++)
{
int u,v;
RD2(u,v);
a[u].a[v]=;
a[v].a[u]=;
}
for(i=;i<=n;i++)
{
a[i].a[i]=;
}
memset(dis,0x3f,sizeof(dis));
dis[atox(a[])]=;
printf("Case #%d: %d\n",cas++,gank());
}
return ;
}

UVa12726 one Friend at a Time (位 广搜)的更多相关文章

  1. HUST 1605 Gene recombination(广搜,位运算)

    题目描述 As a gene engineer of a gene engineering project, Enigma encountered a puzzle about gene recomb ...

  2. 广搜+打表 POJ 1426 Find The Multiple

    POJ 1426   Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25734   Ac ...

  3. hdu 1195:Open the Lock(暴力BFS广搜)

    Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  4. hdu 5025 Saving Tang Monk 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...

  5. hdu 5094 Maze 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...

  6. poj3126 Prime Path 广搜bfs

    题目: The ministers of the cabinet were quite upset by the message from the Chief of Security stating ...

  7. PTA 7-7 六度空间(广搜)

    “六度空间”理论又称作“六度分隔(Six Degrees of Separation)”理论.这个理论可以通俗地阐述为:“你和任何一个陌生人之间所间隔的人不会超过六个,也就是说,最多通过五个人你就能够 ...

  8. BFS广搜题目(转载)

    BFS广搜题目有时间一个个做下来 2009-12-29 15:09 1574人阅读 评论(1) 收藏 举报 图形graphc优化存储游戏 有时间要去做做这些题目,所以从他人空间copy过来了,谢谢那位 ...

  9. hdu 1026:Ignatius and the Princess I(优先队列 + bfs广搜。ps:广搜AC,深搜超时,求助攻!)

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

随机推荐

  1. java系列:《java核心技术 卷1》学习笔记,chapter 11 调试技巧

    11. 6 调试技巧 1)一个不太为人所知却非常有效的技巧是在每个类中放一个main方法,这样就可以对每个类进行单元测试.这个方法可以保留,因为在java虚拟机只调用启动类的main方法. 2)   ...

  2. Java系列: 关于HttpSessionListener的sessionDestroyed什么时候触发

    根据书本写了下面这个监听器,然后开始调试,打开一个浏览器来访问该网页,可以正常触发sessionCreated,然后关闭浏览器,发现没有触发sessionDestroyed,然后我怀疑是不是这个监听器 ...

  3. [CareerCup] 4.7 Lowest Common Ancestor of a Binary Search Tree 二叉树的最小共同父节点

    4.7 Design an algorithm and write code to find the first common ancestor of two nodes in a binary tr ...

  4. .NET MVC控制器向视图传递数据的四种方式

    .NET MVC控制器向视图传递数据的四种方式: 1.ViewBag  ViewBag.Mvc="mvc"; 2.ViewData ViewBag["Mvc"] ...

  5. AngularJS开发指南8:AngularJS模块的详解

    在讲angularjs的模块之前,我们先介绍一下angular的一些知识点: AngularJS是纯客户端技术,完全用Javascript编写的.它使用的是网页开发的常规技术(HTML,CSS,Jav ...

  6. 利用less监视模式实时预览样式刷新浏览器

    [前言]此处介绍的方法只是我个人的用法,相信大家有更好更简洁的方式. 上次写到利用LiveReload解放F5.而且LiveReload可以编辑sass/less/stylus.但是可惜发现LiveR ...

  7. The class has no identifier property

    这个问题一般是***.hbm.xml中的id属性中的name没有配置. 例: <?xml version="1.0" encoding="utf-8"?& ...

  8. htpasswd 详解

    Apache htpasswd命令用法详解 htpasswd建立和更新存储用户名.密码的文本文件, 用于对HTTP用户的basic认证. # /usr/local/apache/bin/htpassw ...

  9. Spring-事物配置

    Spring框架支持事务管理的核心是事务管理器抽象,对于不同的数据访问框架(如Hibernate)通过实现策略接口PlatformTransactionManager,从而能支持各种数据访问框架的事务 ...

  10. android 6.0 SDK中删除HttpClient的相关类的解决方法

    一.出现的情况 在eclipse或 android studio开发, 设置android SDK的编译版本为23时,且使用了httpClient相关类的库项目:如android-async-http ...