Light OJ 1406 Assassin`s Creed 状态压缩DP+强连通缩点+最小路径覆盖
题目来源:Light OJ 1406 Assassin`s Creed
题意:有向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方
思路:最少的的人能够走全然图 明显是最小路径覆盖问题 这里可能有环 所以要缩点 可是看例子又发现 一个强连通分量可能要拆分 n最大才15 所以就状态压缩
将全图分成一个个子状态 每一个子状态缩点 求最小路径覆盖 这样就攻克了一个强连通分量拆分的问题 最后状态压缩DP求解最优值
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <stack>
using namespace std;
const int maxn = 16;
vector <int> G[maxn], G2[maxn];
int dp[1<<maxn];
int pre[maxn], low[maxn], sccno[maxn];
int clock, scc_cnt;
int n, m;
stack <int> S;
int a[maxn][maxn];
int b[maxn][maxn]; void dfs(int u, int x)
{
pre[u] = low[u] = ++clock;
S.push(u);
for(int i = 0; i < G[u].size(); i++)
{
int v = G[u][i];
if(!(x&(1<<v)))
continue;
if(!pre[v])
{
dfs(v, x);
low[u] = min(low[u], low[v]);
}
else if(!sccno[v])
{
low[u] = min(low[u], pre[v]);
}
}
if(pre[u] == low[u])
{
scc_cnt++;
while(1)
{
int x = S.top(); S.pop();
sccno[x] = scc_cnt;
if(x == u)
break;
}
}
}
int find_scc(int x)
{
memset(sccno, 0, sizeof(sccno));
memset(pre, 0, sizeof(pre));
scc_cnt = 0, clock = 0;
for(int i = 0; i < n; i++)
{
if(x&(1<<i) && !pre[i])
dfs(i, x);
}
return scc_cnt;
} int y[maxn];
bool vis[maxn]; bool xyl(int u)
{
for(int i = 0; i < G2[u].size(); i++)
{
int v = G2[u][i];
if(vis[v])
continue;
vis[v] = true;
if(y[v] == -1 || xyl(y[v]))
{
y[v] = u;
return true;
}
}
return false;
}
int match()
{
int ans = 0;
memset(y, -1, sizeof(y));
for(int i = 1; i <= scc_cnt; i++)
{
memset(vis, false, sizeof(vis));
if(xyl(i))
ans++;
}
return scc_cnt-ans;
}
int main()
{
int cas = 1;
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d %d", &n, &m);
for(int i = 0; i < n; i++)
G[i].clear();
memset(a, 0, sizeof(a));
while(m--)
{
int u, v;
scanf("%d %d", &u, &v);
u--;
v--;
G[u].push_back(v);
a[u][v] = 1;
}
dp[0] = 0;
//puts("sdf");
for(int i = 1; i < (1<<n); i++)
{
//memset(b, 0, sizeof(b));
find_scc(i);
for(int j = 0; j <= n; j++)
G2[j].clear();
for(int j = 0; j < n; j++)
for(int k = 0; k < n; k++)
if(a[j][k] && sccno[j] && sccno[k] && sccno[j] != sccno[k])
G2[sccno[j]].push_back(sccno[k]);
dp[i] = match();
}
//puts("sdf");
for(int s = 1; s < (1<<n); s++)
{
for(int i = s; i; i = s&(i-1))
{
dp[s] = min(dp[s], dp[s^i] + dp[i]);
}
}
printf("Case %d: %d\n", cas++, dp[(1<<n)-1]);
}
return 0;
}
Light OJ 1406 Assassin`s Creed 状态压缩DP+强连通缩点+最小路径覆盖的更多相关文章
- Light OJ 1406 Assassin`s Creed 减少国家DP+支撑点甚至通缩+最小路径覆盖
标题来源:problem=1406">Light OJ 1406 Assassin`s Creed 意甲冠军:向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路: ...
- Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖
题目来源:Light OJ 1429 Assassin`s Creed (II) 题意:最少几个人走全然图 能够反复走 有向图 思路:假设是DAG图而且每一个点不能反复走 那么就是裸的最小路径覆盖 如 ...
- Light OJ 1316 A Wedding Party 最短路+状态压缩DP
题目来源:Light OJ 1316 1316 - A Wedding Party 题意:和HDU 4284 差点儿相同 有一些商店 从起点到终点在走过尽量多商店的情况下求最短路 思路:首先预处理每两 ...
- 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 ...
随机推荐
- iOS:麦克风权限检测和获取
一.检测 该方法是用来判断麦克风是否进行过授权,如果授权过就直接进行需要的功能操作:如果没有进行授权,那么就要获取授权. AVAuthorizationStatus authStatus = [AVC ...
- 读书笔记,《Java 8实战》,第四章,引入流
集合是Java中使用最多的API,但集合操作却远远算不上完美.主要表现在两点, 第一,集合不能让我们像数据库的SQL语言一样用申明式的语言指定操作: 第二,现在的集合API无法让我们 ...
- OpenGL® ES 3.0 Programming Guide - Book Website
OpenGL® ES 3.0 Programming Guide - Book Website http://opengles-book.com sample codes in GitHub: htt ...
- POI的一些配置
引用:http://apps.hi.baidu.com/share/detail/17249059 POI中可能会用到一些需要设置EXCEL单元格格式的操作小结: 先获取工作薄对象: HSSFWork ...
- Ant编译utf-8非法字符:/65279 解决方法
原文链接:http://blog.csdn.net/xiyuan1999/article/details/5989336 Ant编译utf-8非法字符:/65279 解决方法 使用ant编译j ...
- CareerCup Facebook Total number of substring palindrome
Write a function for retrieving the total number of substring palindromes. For example the input is ...
- [Canvas]动态背景
欲查看动态效果请点击下载代码再用Chrome或Firefox打开index.html 图例: 代码: <!DOCTYPE html> <html lang="utf-8&q ...
- WIN10系统 如何自动调整系统时间
在底部搜索框中输入设置 先关闭再打开自动设置时间的选项即可重新获取系统时间,前提要联网状态
- Solidworks如何替换工程图参考零件
不要在左侧树形图右击修改 而是要在右侧主视图上右击,替换模型 左侧浏览找到新的零件,然后打开 替换完成之后,会有一些尺寸变成黄色,只需要改动黄色部分即可,不需要每个尺寸重新标注
- CXF实战之拦截器Interceptor(四)
拦截器(Interceptor)是CXF功能最基本的扩展点,能够在不正确核心模块进行改动的情况下.动态加入非常多功能.拦截器和JAX-WS Handler.Filter的功能相似,当服务被调用时.就会 ...