题目来源: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+强连通缩点+最小路径覆盖的更多相关文章

  1. Light OJ 1406 Assassin`s Creed 减少国家DP+支撑点甚至通缩+最小路径覆盖

    标题来源:problem=1406">Light OJ 1406 Assassin`s Creed 意甲冠军:向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路: ...

  2. Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖

    题目来源:Light OJ 1429 Assassin`s Creed (II) 题意:最少几个人走全然图 能够反复走 有向图 思路:假设是DAG图而且每一个点不能反复走 那么就是裸的最小路径覆盖 如 ...

  3. Light OJ 1316 A Wedding Party 最短路+状态压缩DP

    题目来源:Light OJ 1316 1316 - A Wedding Party 题意:和HDU 4284 差点儿相同 有一些商店 从起点到终点在走过尽量多商店的情况下求最短路 思路:首先预处理每两 ...

  4. hoj2662 状态压缩dp

    Pieces Assignment My Tags   (Edit)   Source : zhouguyue   Time limit : 1 sec   Memory limit : 64 M S ...

  5. POJ 3254 Corn Fields(状态压缩DP)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4739   Accepted: 2506 Descr ...

  6. [知识点]状态压缩DP

    // 此博文为迁移而来,写于2015年7月15日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6jf.html 1.前 ...

  7. HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP

    题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...

  8. DP大作战—状态压缩dp

    题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...

  9. 状态压缩dp问题

    问题:Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Ev ...

随机推荐

  1. [leetcode]Valid Palindrome @ Python

    原题地址:https://oj.leetcode.com/problems/valid-palindrome/ 题意: Given a string, determine if it is a pal ...

  2. Android -- java代码设置margin

    我们平常可以直接在xml里设置margin,如: <ImageView android:layout_margin="5dip" android:src="@dra ...

  3. Word Embedding与Word2Vec

    http://blog.csdn.net/baimafujinji/article/details/77836142 一.数学上的“嵌入”(Embedding) Embed这个词,英文的释义为, fi ...

  4. 转: Vim快捷键分类

    Vim快捷键分类 http://www.cnblogs.com/jikey/archive/2011/12/28/2304341.html  一. 移动:    h,j,k,l: 左,下,上,右.   ...

  5. c/c++ sizeof运算符详解以及对象大小

    原文:http://krystism.is-programmer.com/posts/41468.html 学过c的都知道sizeof运算符.不过还是需要注意以下几点.先从c的sizeof说起: 1. ...

  6. 免费素材:气球样式的图标集(PSD, SVG, PNG)

    本地下载 一套30枚设计精良的气泡式圆形图标,两种款式供您选择,相信你会喜欢!

  7. Try Before Choosing

     Try Before Choosing Erik Doernenburg CREATing An AppliCATion REquiRES MAKing MAny dECiSionS. Some ...

  8. Android Volley 库通过网络获取 JSON 数据

    本文内容 什么是 Volley 库 Volley 能做什么 Volley 架构 环境 演示 Volley 库通过网络获取 JSON 数据 参考资料 Android 关于网络操作一般都会介绍 HttpC ...

  9. C#.NET常见问题(FAQ)-如何让文本框textbox内容限制为数字

    //限制文本框的输入 private void txtQuestionScore_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyCha ...

  10. Bridging and Bonding with CentOS 6.5

    eth0和eth1要做bond,然后kvm虚拟机通过bridge与外界通信. 那么就要在bond上做bridge.配置文件例如以下,实測这样配置,能够从kvm虚拟机ping通外界拓扑. ifcfg-e ...