loj 1406(状态压缩)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1406
思路:首先可以预处理出在每个顶点的状态的合法状态vis[u][state], 然后标记那些合法状态mark[state]。最后就是记忆化搜索了,对于当前状态state,我们有res = min(res, 1 + Solve(state ^ substate)), 其中substate为state的子状态,并且substate = (substate - 1) & state.那么最终就是要求Solve((1 << n) - 1)了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std; int n, m;
bool vis[][ << ];
bool mark[ << ];
int dp[ << ];
vector<int > g[]; void dfs(int u, int state)
{
vis[u][state] = true;
mark[state] = true;
for (int i = ; i < (int)g[u].size(); i++) {
int v = g[u][i];
if (!vis[v][state | ( << v)]) {
dfs(v, state | ( << v));
}
}
} int Solve(int state)
{
if (state == ) return ;
if (dp[state] != -) return dp[state];
int res = ;
for (int i = state; i > ; i = (i - ) & state) {
if (mark[i]) {
res = min(res, + Solve(state ^ i));
}
}
return dp[state] = res;
} int main()
{
int _case, t = ;
scanf("%d", &_case);
while (_case--) {
scanf("%d %d", &n, &m);
for (int i = ; i < n; i++) g[i].clear();
while (m--) {
int u, v;
scanf("%d %d", &u, &v);
u--, v--;
g[u].push_back(v);
}
memset(vis, false, sizeof(vis));
memset(mark, false, sizeof(mark));
for (int i = ; i < n; i++) dfs(i, << i);
memset(dp, -, sizeof(dp));
printf("Case %d: %d\n", t++, Solve(( << n) -));
}
return ;
}
loj 1406(状态压缩)的更多相关文章
- loj 1011(状态压缩+记忆化搜索)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25837 思路:状态压缩+记忆化搜索. #include<io ...
- Light OJ 1406 Assassin`s Creed 状态压缩DP+强连通缩点+最小路径覆盖
题目来源:Light OJ 1406 Assassin`s Creed 题意:有向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路:最少的的人能够走全然图 明显是最小路径覆盖问题 ...
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- HDU 3605:Escape(最大流+状态压缩)
http://acm.hdu.edu.cn/showproblem.php?pid=3605 题意:有n个人要去到m个星球上,这n个人每个人对m个星球有一个选择,即愿不愿意去,"Y" ...
- [HDU 4336] Card Collector (状态压缩概率dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4336 题目大意:有n种卡片,需要吃零食收集,打开零食,出现第i种卡片的概率是p[i],也有可能不出现卡 ...
- HDU 4336 Card Collector (期望DP+状态压缩 或者 状态压缩+容斥)
题意:有N(1<=N<=20)张卡片,每包中含有这些卡片的概率,每包至多一张卡片,可能没有卡片.求需要买多少包才能拿到所以的N张卡片,求次数的期望. 析:期望DP,是很容易看出来的,然后由 ...
- codeforces B - Preparing Olympiad(dfs或者状态压缩枚举)
B. Preparing Olympiad You have n problems. You have estimated the difficulty of the i-th one as inte ...
- NOIP2005过河[DP 状态压缩]
题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把独木桥上青蛙可能到达的点看成数 ...
- vijos1426兴奋剂检查(多维费用的背包问题+状态压缩+hash)
背景 北京奥运会开幕了,这是中国人的骄傲和自豪,中国健儿在运动场上已经创造了一个又一个辉煌,super pig也不例外……………… 描述 虽然兴奋剂是奥运会及其他重要比赛的禁药,是禁止服用的.但是运动 ...
随机推荐
- poj 1562
这道题主要注意输入的问题,以及对周围搜索时的注意,要使用递归,多次调用,附上一组数据 11 20*@*@*@@@**@*@**@@@*****@*@*@*@*@****@**@*@*@*@*@*@*@ ...
- CEF3开发者系列之外篇——IE中JS与C++交互
使用IE内核开发客户端产品,系统和前端页面之间的交互,通常给开发和维护带来很大的便利性.但操作系统和前端之间的交互却是比较复杂的.具体来说就是脚本语言和编译语言的交互.在IE内核中html和css虽然 ...
- andrond mk通配符遍历文件夹
define all-cpp-files-under$(patsubst ./%,%, \ $(shell cd $(LOCAL_PATH) ; \ find $(1) -name "*.c ...
- ZOJ 3812 We Need Medicine(dp、背包、状态压缩、路径记录)
参考:http://blog.csdn.net/qian99/article/details/39138329 参考的链接里说明得很好,注释也很好...thanks for sharing 朴素的想法 ...
- monitor disk
#!/bin/bash # #top #Big_USERS - find big disk space users in various directories ################### ...
- WebService到底是什?
一.序言 大家或多或少都听过WebService(Web服务),有一段时间很多计算机期刊.书籍和网站都大肆的提及和宣传WebService技术,其中不乏很多吹嘘和做广告的成分.但是不得不承认的是Web ...
- 最牛逼android上的图表库MpChart(三) 条形图
最牛逼android上的图表库MpChart三 条形图 BarChart条形图介绍 BarChart条形图实例 BarChart效果 最牛逼android上的图表库MpChart(三) 条形图 最近工 ...
- yum安装时出现:Cannot retrieve metalink for repository: epel. Please verify its path and try again
在CentOS 6.3 x86_64下安装php-mcrypt的时候出现了问题:Error: Cannot retrieve metalink for repository: epel. Please ...
- php策略模式的研究
<?php abstract class Moshi{ private $num; public $price; const Ted=1; const Sed=2 ...
- IE11 的区别
http://msdn.microsoft.com/zh-tw/visualc/bg182625