先上题目:

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. 1617: [Usaco2008 Mar]River Crossing渡河问题(dp)

    1617: [Usaco2008 Mar]River Crossing渡河问题 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1219  Solved:  ...

  2. ECMA里面的一元符

    只能操作一个值的叫做一元操作符.一元操作符是ECMAScript中最简单的操作符 1.递增和递减操作符 递增和递减操作符直接借鉴自c,而且各有俩个版本,前置型和后置型.顾名思义,前置型就是位于要操作的 ...

  3. spring cloud config搭建说明例子(二)-添加eureka

    添加注册eureka 服务端 ConfigServer pom.xml <dependency> <groupId>org.springframework.cloud</ ...

  4. 组合数学题 Codeforces Round #108 (Div. 2) C. Pocket Book

    题目传送门 /* 题意:每一次任选i,j行字符串进行任意长度前缀交换,然后不断重复这个过程,问在过程中,第一行字符串不同的个数 组合数学题:每一列不同的字母都有可能到第一行,所以每列的可能值相乘取模就 ...

  5. Android 性能优化(23)*性能工具之「Heap Viewer, Memory Monitor, Allocation Tracker」Memory Profilers

    Memory Profilers In this document Memory Monitor Heap Viewer Allocation Tracker You should also read ...

  6. java 选择排序与冒泡排序

    选择排序与冒泡排序的特点与区别 ++++++++++++++++++++++++++++++++++++++++++++++ 选择排序 这一种简单的排序方法,它的基本思想是:R[n]第一次从R[0]~ ...

  7. EasyUI系列学习(七)-Linkbutton(按钮)

    一.加载组件 1.使用class加载 <a href="#" class="easyui-linkbutton">按钮</a> 2.使用 ...

  8. Thread stack overrun

    ERROR 1436 (HY000): Thread stack overrun:  6448 bytes used of a 131072 byte stac k, and 128000 bytes ...

  9. 远程图形界面:使用putty+xmin远程登录ubuntu-kde

    让我继续用反人类的编辑器Vim和emacs,我宁愿自断三指.因此,在Win端配置WinSCP+Putty+Xming远程操作ubuntu. 参考链接:putty+xming远程登录Ubuntu16.0 ...

  10. HDU_1024_dp

    Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...