先上题目:

1500. Pass Licenses

Time limit: 2.5 second
Memory limit: 64 MB
A New Russian Kolyan believes that to spend his time in traffic jams is below his dignity. This is why he had put an emergency flashlight upon the roof of his Hummer and had no problems until a recent decision of the city administration. Now each street of the city belongs to one or several categories, and a driver must have a separate license in order to use an emergency flashlight in the streets of each category. If a street belongs to several categories, it is sufficient to have a license only for one of these categories. For each category, a license is issued by a separate city official. Although these officials are different, they accept bribes of the same amount for giving a license. Help Kolyan to find a way from his home to work such that he can go this way with his flashlight turned on and having spent the minimal amount of money for bribes.

Input

The input contains the street plan in the following format. There are integers KN, and M in the first line, where K is the number of street categories (1 ≤ K ≤ 20), N is the number of crossroads (2 ≤ N ≤ 30), and M is the number of descriptions of street segments between crossroads.
Each of the next M lines describes a street segment by three integers V1 V2 C, where V1 and V2 are the numbers of the crossroads limiting this segment, and C is its category. Crossroads are numbered from 0 to N – 1, categories are numbered from 0 to K – 1. For any pair of crossroads no two segments of the same category connect these crossroads.

Output

Output in the first line the minimal number of licenses necessary for going from the crossroad 0 (Kolyan's home) to the crossroad 1 (Kolyan's work) with an emergency flashlight turned on.
In the second line, give the list of categories for which licenses must be obtained. The numbers should be separated with spaces. It is guaranteed that such list is always exist.

Sample

input output
3 3 3
0 2 0
0 2 1
1 2 2
2
0 2

  题意:给出n个点m条边(无向),有k种驾照,每条边如果想通过的话需要某一种驾照,问你从0号点到1号点最少需要多少种驾照,并把它们输出。

  做法:状态压缩然后暴力检查是否合法,记录最少需要多少中驾照。

  s[i][j]表示从i->j的话有哪几种可以用的驾照。然后枚举不同的驾照组合,找到最少需要的数目以后输出即可。

上代码:

 #include <cstdio>
#include <cstring>
#include <utility>
#include <set>
#define MAX 32
#define MK(x,y) (make_pair(x,y))
using namespace std; int s[MAX][MAX];
bool vis[MAX];
int k,n,m; bool dfs(int u,int caps) {
vis[u]=;
if(u==) return ;
for(int i=;i<n;i++){
if(!vis[i] && (caps&s[u][i])){
if(dfs(i,caps)) return ;
}
}
return ;
} int main() {
int u,v,cap,ans,caps,f;
//freopen("data.txt","r",stdin);
while(scanf("%d %d %d",&k,&n,&m)!=EOF) {
memset(s,,sizeof(s));
for(int i=;i<m;i++){
scanf("%d %d %d",&u,&v,&cap);
if(u==v) continue;
s[u][v]|=(<<cap);
s[v][u]|=(<<cap);
}
ans=k+;
caps=;
for(int i=;i<(<<k);i++){
int cnt=;
for(int j=;j<k;j++){
if(i&(<<j)) cnt++;
}
if(cnt>=ans) continue;
memset(vis,,sizeof(vis));
if(dfs(,i)){
ans=cnt;
caps=i;
}
}
printf("%d\n",ans);
f=;
for(int i=;caps>;caps>>=,i++){
if(caps&){
if(f++) printf(" ");
printf("%d",i);
}
}
printf("\n");
}
return ;
}

/*SGU 1500*/

SGU -1500 - Pass Licenses的更多相关文章

  1. ural 1500 Pass Licenses (状态压缩+dfs)

    1500. Pass Licenses Time limit: 2.5 secondMemory limit: 64 MB A New Russian Kolyan believes that to ...

  2. BZOJ 1500/Luogu 2042 - 维修数列 - [NOI2005][Splay]

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1500 题目链接:https://www.luogu.org/problemnew/sho ...

  3. [模拟电路] 2、Passive Band Pass Filter

    note: Some articles are very good in http://www.electronics-tutorials.ws/,I share them in the Cnblog ...

  4. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' heade

    XMLHttpRequest cannot load http://10.164.153.37:8050/WebService/WebService.asmx/wsGetStreetData. Res ...

  5. UvaLA 3938 "Ray, Pass me the dishes!"

                            "Ray, Pass me the dishes!" Time Limit: 3000MS   Memory Limit: Unkn ...

  6. 解决服务器每次都要输入Enter PEM pass phrase

    今天架设好Python的HTTPS云服务器, 发现每次连接都要Enter PEM pass phrase 把服务器端的key里面的key剥离掉就好了 openssl rsa -in server.ke ...

  7. SQL Pass北京举行2014年第一次线下活动

    地点:北京微软(中国)有限公司[望京利星行],三层308室 时间:2014年 3 月15日 13:30-16:30 SQL PASS 北京QQ群号:2435349 报名地址:http://1drv.m ...

  8. SQL Pass北京举办1周年活动(本次活动特别邀请到了来自微软的SQL Server大师何雷谈数据库职业规划)

    地点:北京微软(中国)有限公司[望京利星行],三层308室 时间:2013年 12 月28日 13:30-16:30 SQL PASS 北京QQ群号:2435349 新浪微群地址:http://q.w ...

  9. SQL Pass北京举办第11次线下活动,欢迎报名(本次活动特别邀请了来自微软总部Xin Jin博士)

    活动主题: 探讨SQL Server 2014与Fusion IO在SQL Server中的应用 地点:北京微软(中国)有限公司[望京利星行],三层308室 时间:2013年 10 月19日 13:3 ...

随机推荐

  1. (进制)51NOD 1057 N的阶乘

    输入N求N的阶乘的准确值.   Input 输入N(1 <= N <= 10000) Output 输出N的阶乘 Input示例 5 Output示例 120解:这其实是MOD进制,将一个 ...

  2. 乐搏讲自动化测试- Python环境搭建(7)

    Python的下载和安装 Python可应用于多平台包括 Linux 和 Mac OS X.你可以通过终端窗口输入 "python" 命令来查看本地是否已经安装Python以及Py ...

  3. 报错:ERROR! The server quit without updating PID file (/usr/local/var/mysql/chenyuntekiMacBook-Air.local.pid).

    在Mac上通过brew install mysql 安装了完mysql 执行mysql.server start 报错:ERROR! The server quit without updating ...

  4. NHibernate3.2学习笔记-几种查询方式

    一.开发环境 数据库:SQLServer2008 编译器:VS2010 .Net版本:.Net Framework 4.0 二.开发过程 1.项目结构 承接上一篇 2.执行sql语句 (1)基本语法 ...

  5. LN : leetcode 538 Convert BST to Greater Tree

    lc 538 Convert BST to Greater Tree 538 Convert BST to Greater Tree Given a Binary Search Tree (BST), ...

  6. hanframe开微博了

    之前一直在百度里转一些文章,平时都把积累的东西放在一些文档中,还是想着记下来会比较好一点,顺便,每天都来这里做一点总结吧

  7. 前端面试题HTML

    浏览器页面有哪三层构成,分别是什么,作用是什么?

  8. 关于vue项目 路由中 使用的坑

    关于vue路由重定向的时候 记得一定要先声明先声明

  9. js中取整数的方法

    1.取整的方法 Math.floor( ) Math 对象的方法--取比当前数值小的最大整数(下取整). Math.ceil( ) Math对象的方法--取比当前数值大的最小整数(上取整). Math ...

  10. js中获取class封装

    1.封装 //封装getClass function getClass(tagName,className) //获得标签名为tagName,类名className的元素 { if(document. ...