状态压缩DP
Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:327680KB 64bit IO Format:%I64d & %I64u
Description
Input
Output
Sample Input
Sample Output
Hint
Description
Input
Output
Sample Input
3 3
1 2
1 3
2 3
Sample Output
2
题意:
给出N个珠子,要把他们连成串,每个珠子都要用上,有多少种连法。重点是dp状态转移方程。
代码:
/*
给出的珠子要全部用上,因此只要最后的状态dp[i][j]中的j能够与第一个珠子相连就行,其中i表示取珠子
的状态,每多加一颗珠子他的dp数就是加上没加之前的dp数(重点!).
*/
#include<iostream>
#include<string>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<queue>
#include<stack>
using namespace std;
int n,m,a,b;
bool d[][];
long long dp[<<][]; //用int过不了
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(dp,,sizeof(dp));
memset(d,,sizeof(d));
for(int i=;i<m;i++)
{
scanf("%d%d",&a,&b);
a--; //这里减一是为了后面写循环简便,也可以不减一
b--;
d[b][a]=;
d[a][b]=;
}
dp[][]=; //初始化第一个当放一个钥匙时总数为1
for(int i=;i<(<<n);i++)
{
for(int j=;j<n;j++)
{
if(dp[i][j]==) continue; //也可以用!(i&(1<<j))来判断,但后者会更费时
for(int k=;k<n;k++) //k也可以从0开始
{
if(!d[k][j]) continue; //判断k与j是否能连接
if(i&(<<k)) continue; //判断第K个钥匙是否已算过
dp[i|(<<k)][k]+=dp[i][j];
}
}
}
long long ans=;
for(int i=;i<n;i++)
{
if(d[][i]) ans+=dp[(<<n)-][i];
}
cout<<ans<<endl;
}
return ;
}
状态压缩DP的更多相关文章
- hoj2662 状态压缩dp
Pieces Assignment My Tags (Edit) Source : zhouguyue Time limit : 1 sec Memory limit : 64 M S ...
- POJ 3254 Corn Fields(状态压缩DP)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4739 Accepted: 2506 Descr ...
- [知识点]状态压缩DP
// 此博文为迁移而来,写于2015年7月15日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6jf.html 1.前 ...
- HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP
题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...
- DP大作战—状态压缩dp
题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...
- 状态压缩dp问题
问题:Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Ev ...
- BZOJ-1226 学校食堂Dining 状态压缩DP
1226: [SDOI2009]学校食堂Dining Time Limit: 10 Sec Memory Limit: 259 MB Submit: 588 Solved: 360 [Submit][ ...
- Marriage Ceremonies(状态压缩dp)
Marriage Ceremonies Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- HDU 1074 (状态压缩DP)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:有N个作业(N<=15),每个作业需耗时,有一个截止期限.超期多少天就要扣多少 ...
- HDU 4511 (AC自动机+状态压缩DP)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4511 题目大意:从1走到N,中间可以选择性经过某些点,比如1->N,或1->2-> ...
随机推荐
- ios本地推送
#import "AppDelegate.h" @interface AppDelegate () @end @implementation AppDelegate //无论程序在 ...
- currentStyle
用js的style属性可以获得html标签的样式,但是不能获取非行间样式. 解决方法: 在IE下可以用currentStyle; 在FF下用getComputedStyle; 然而,为了让其兼容,解决 ...
- 70多G的Kindle电子书合集
70多个G的电子书. 百度网盘分享了几次被屏蔽了, 360网盘每个文件夹最多只能分享2000个文件,所以只能分成多个文件夹分享. 如果大家知道更好的分享方式,欢迎留言相告,多谢: http://yun ...
- poj1328 贪心
http://http://poj.org/problem?id=1328 神TM贪心. 不懂请自行搜博客. AC代码: #include<cstdio> #include<algo ...
- Codeforces Round #371 (Div. 2) - A
题目链接:http://codeforces.com/contest/714/problem/A 题意:有两个人A,B 给定A的时间区间[L1,R1], B的时间区间[L2,R2],然后在正好K分钟的 ...
- POJ 1200 字符串HASH
题目链接:http://poj.org/problem?id=1200 题意:给定一个字符串,字符串只有NC个不同的字符,问这个字符串所有长度为N的子串有多少个不相同. 思路:字符串HASH,因为只有 ...
- Swift3.0语言教程组合字符串
Swift3.0语言教程组合字符串 Swift3.0语言教程组合字符串,当开发者想要将已经存在的字符串进行组合,形成一个新的字符串,可以使用NSString中的两个方法,分别为appending(_: ...
- constructor
function Person(name){ this.name = name; } Person.prototype = { constructor : Person, sayName : func ...
- JS扩展方法——字符串trim()
转自:http://www.cnblogs.com/kissdodog/p/3386480.html <head> <title>测试JS扩展方法</title> ...
- HD1847-(博弈论??)
Good Luck in CET-4 Everybody! Problem Description 大学英语四级考试就要来临了,你是不是在紧张的复习?也许紧张得连短学期的ACM都没工夫练习了,反正我知 ...