LightOJ - 1406入口

Altair is in great danger as he broke the three tenets of the assassin creed. The three tenets are: 1) never kill an innocent people, 2) always be discrete and 3) never compromise the brotherhood. As a result Altair is given another chance to prove that he is still a true assassin. Altair has to killn targets located inn different cities. Now as time is short, Altair can send a massage along with the map to the assassin's bureau to send some assassins who will start visiting cities and killing the targets. An assassin can start from any city, but he cannot visit a city which is already visited by any other assassin except him (because they do not like each other's work). He can visit a city multiple times though. Now Altair wants to find the minimum number of assassins needed to kill all the targets. That's why he is seeking your help.

Input

Input starts with an integer T (≤ 50), denoting the number of test cases.

Each case starts with a blank line. Next line contains two integers n (1 ≤ n ≤ 15) and m (0 ≤ m ≤ 50), where n denotes the number of cities and m denotes the number of one way roads. Each of the next m lines contains two integers u v (1 ≤ u, v ≤ n, u ≠ v) meaning that there is a road from u to v. Assume that there can be at most one road from a cityu to v.

Output

For each case, print the case number and the minimum number of assassins needed to kill all the targets.

Sample Input

2

3 2

1 2

2 3

6 6

1 2

2 3

2 4

5 4

4 6

4 2

Sample Output

Case 1: 1

Case 2: 2


题意:有n个(n<=15)城市,城市之间有m(m<=50)条单向路.刺客可以从任何一个城市出发去每个城市刺杀。每个刺客可以去自己去过的城市,但是不能去其他刺客去过的城市行刺,问最少需要几个刺客。

分析:状压DP,比赛时想到啦状压DP,可是子状态没有想清楚,导致wa。由于去过的城市还可以再去,走过的路还可以再走。那么在用dfs求子结构时用什么记录状态呢。

比赛时头脑有些混乱我当时记录的是每条路可以走100次(由于DFS的局限性,走完是不可能走完的),毕竟只有15个城市,但是还是wa。最后想到应该用已经访问过的点,和将访问的的点记录状态。然后就能找出能够一个刺客访问的所有城市集合。状态较多,用记忆化搜索节省时间。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
bool v[1<<16][16],used[1<<16];
int dp[1<<16],n,m,t;
vector<int> g[16];
void dfs(int x,int s)
{used[s]=true;for(int i=0;i<g[x].size();i++){int y=g[x][i];if(v[s][y])continue;v[s][y]=true;dfs(y,s|(1<<y));}
}
int dis(int s)
{int &res=dp[s];if(res!=-1)return res;res=200;for(int i=s;i>0;i=(i-1)&s){if(used[i]){res=min(res,1+dis(s^i));}}return res;
}
int main()
{int T,x,y,cas=1;scanf("%d",&T);while(T--){scanf("%d%d",&n,&m);for(int i=0;i<=n;i++)g[i].clear();for(int i=1;i<=m;i++){scanf("%d%d",&x,&y);x--;y--;g[x].push_back(y);}memset(v,false,sizeof(v));memset(used,false,sizeof(used));for(int i=0;i<n;i++)dfs(i,1<<i);memset(dp,-1,sizeof(dp));dp[0]=0;t=1<<n;printf("Case %d: %d\n",cas++,dis(t-1));}return 0;
}

LightOJ - 1406 Assassin`s Creed【状压DP】相关推荐

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

    标题来源:Light OJ 1406 Assassin`s Creed 意甲冠军:向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路:最少的的人能够走全然图 明显是最小路径覆盖问 ...

  2. lightoj 1037 - Agent 47(状压dp)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1037 1 #include <iostream> 2 #inclu ...

  3. POJ 1038 Bugs Integrated Inc (复杂的状压DP)

    \(POJ~1038~~*Bugs~Integrated~Inc:\) (复杂的状压DP) \(solution:\) 很纠结的一道题目,写了大半天,就想练练手,结果这手生的.其实根据之前那道炮兵阵地 ...

  4. codeforces 8C. Looking for Order 状压dp

    题目链接 给n个物品的坐标, 和一个包裹的位置, 包裹不能移动. 每次最多可以拿两个物品, 然后将它们放到包里, 求将所有物品放到包里所需走的最小路程. 直接状压dp就好了. #include < ...

  5. UVA10296 Jogging Trails(中国邮递员问题)(欧拉回路、一般图最大权匹配 / 状压DP)

    整理的算法模板合集: ACM模板 目录 思路 UVA10296 Jogging Trails 题目翻译: 给你n个点,m条无向边,每条边有一定的距离数值,构造成一个连通图.问从任意一点出发,遍历所有的 ...

  6. POJ 2411 Mondriaan‘s Dream(最清楚好懂的状压DP讲解)(连通性状态压缩DP)

    poj 2411 Mondriaan's Dream(最清晰的状压DP解析) 闫氏DP大法好 我们这里是一列一列地来,因为是一个棋盘性的状态压缩DP,从哪个方向都一样 摆放的小方格总方案数 等价于 横 ...

  7. 【每日DP】day2、P1879 [USACO06NOV]Corn Fields G玉米地(状压DP模板题)难度⭐⭐⭐★

    昨天的每日DP我还在写01背包,今天就到状压DP了,真刺激. P1879 [USACO06NOV]Corn Fields G 题目链接 输入 2 3 1 1 1 0 1 0 输出 9 一道简单的状压D ...

  8. hdu 4778 Gems Fight! 状压dp

    转自wdd :http://blog.csdn.net/u010535824/article/details/38540835 题目链接:hdu 4778 状压DP 用DP[i]表示从i状态选到结束得 ...

  9. 【洛谷 P1896】[SCOI2005]互不侵犯(状压dp)

    题目链接 题意:在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. 这是道状压\(DP\)好题啊.. ...

  10. P2340 奶牛会展(状压dp)

    P2340 奶牛会展 题目背景 奶牛想证明它们是聪明而风趣的.为此,贝西筹备了一个奶牛博览会,她已经对N 头奶牛进行 了面试,确定了每头奶牛的智商和情商. 题目描述 贝西有权选择让哪些奶牛参加展览.由 ...

最新文章

  1. android 内部类的优化
  2. websocket工作原理
  3. lua学习笔记之模块、包
  4. android应用设计规范,未来的Android应用设计规范应如是
  5. 卷积神经网络 全连接层(稠密层)是什么?(全连接层就是每一个结点都与上一层的所有结点相连的网络层)
  6. freemarker的空值和默认值
  7. 2021年茂名市高考成绩查询,2021年茂名高考最高分多少分,历年茂名高考状元
  8. 卡扇区数据教程_分享一款硬盘分区和数据恢复软件
  9. Flutter 底部向上动画弹出的菜单选项
  10. python获取html文本框内容_Python3处理HTML获取所需内容
  11. Java 将一段时间以周、月、季分割
  12. linux快速查找文件中所包含的指定字段的个数
  13. logback日志配置文件
  14. 教之初计算机考试试题,教之初题库管理系统操作教程-考题处理
  15. BP神经网络(Python代码实现)基于pytorch
  16. 使用python根据图片链接下载图片
  17. 自适应函数符和函数适配器(Adaptable Functors and Function Adapters)
  18. 系统还原点设置被系统管理员禁用
  19. html 毛笔书写效果,canvas 手写毛笔字效果
  20. 世纪三部曲(全9册) 读后感

热门文章

  1. Leetcode实战:121.买卖股票的最佳时机
  2. cogs1439 货车运输 LCA
  3. plot画图 matlab,Matlab Plot 画图中图
  4. 假设今天是2015年3月1号星期日,计算13个月零6天后是星期几?距离现在多少秒?
  5. matlab误码率理论,PSK理论误码率与实际误码率MATLAB仿真程序
  6. 键盘上打出省略号的方法
  7. pdf照片显示正常打印时被翻转_2020年二级建造师执业资格考试打印准考证的10点注意事项!...
  8. TPP并不可怕,可怕的是我们开始自我封闭
  9. 图文笔记,带你走进《未来简史》(11-15)
  10. 美化牙齿的几大方式,护牙剂省钱省力