先上题目:

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. bzoj 1782: [Usaco2010 Feb]slowdown 慢慢游【dfs序+线段树】

    考虑每头牛到达之后的影响,u到达之后,从1到其子树内的点需要放慢的都多了一个,p为u子树内点的牛ans会加1 用线段树维护dfs序,每次修改子树区间,答案直接单点查询p即可 #include<i ...

  2. P2339 提交作业usaco(区间dp)

    P2339 提交作业usaco 题目背景 usaco 题目描述 贝西在哞哞大学选修了 C 门课,她要把所有作业分别交给每门课的老师,然后去车站和同学们一起回家.每个老师在各自的办公室里,办公室要等他们 ...

  3. 流程图软件draw.io

    工作中经常需要梳理一些流程图,时序图.以前用微软Visio绘制流程图(当然不是正版Visio).后来为了响应国家号召,改用processon(proceson.com)进行绘制流程图.Processo ...

  4. eccharts-gl 3D立体柱状图

    echarts-gl继承于echarts echarts-gl官方实例https://echarts.baidu.com/examples/index.html#chart-type-globe 代码 ...

  5. sql server 大数据处理

    对SQL Server数据表进行分区的过程分为三个步骤: 1)建立分区函数 2)建立分区方案 3)对表格进行分区 第一个步骤:建立分区函数 分区函数定义[u]how[/u],即你想要SQL Serve ...

  6. C# 相关概念

    解决方案 在磁盘上由 .sln 文件表示,是一个或多个相关项目的容器. 例如,如果为 Python 应用程序编写 C++ 扩展,该 C++ 项目可以驻留在同一解决方案中. 解决方案还可以包含 Web ...

  7. .Net MVC 前台验证跟后台验证

    前台验证: 首先你得有一个参数类,参数类代码如下 验证标记总结 [DisplayName("邮箱:")]        [Required(ErrorMessage = " ...

  8. Vue组件之间通信的三种方式

    最近在看梁颠编著的<Vue.js实战>一书,感觉颇有收获,特此记录一些比价实用的技巧. 组件是MVVM框架的核心设计思想,将各功能点组件化更利于我们在项目中复用,这类似于我们服务端面向对象 ...

  9. php函数的定义和声明

    1.函数的定义 函数是一个被命名的独立的代码段,它执行特定任务,并可以给调用它的程序返回值. 2.函数的优点 提高程序的重用性 提高程序的可维护性 可以提高软件的开发效率 提高软件的可靠性 控制程序的 ...

  10. SQL生僻字模糊查询

    生僻字指在数据库默认的编码中不存 又称难字或冷僻字 一.SQL中解决生僻字录入乱码问题[调整列数据类型->由varchar改为NVARCHAR]