SGU -1500 - Pass Licenses

先上题目:

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的话有哪几种可以用的驾照。然后枚举不同的驾照组合,找到最少需要的数目以后输出即可。

上代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <utility>
 4 #include <set>
 5 #define MAX 32
 6 #define MK(x,y) (make_pair(x,y))
 7 using namespace std;
 8
 9 int s[MAX][MAX];
10 bool vis[MAX];
11 int k,n,m;
12
13 bool dfs(int u,int caps) {
14     vis[u]=1;
15     if(u==1) return 1;
16     for(int i=0;i<n;i++){
17         if(!vis[i] && (caps&s[u][i])){
18             if(dfs(i,caps)) return 1;
19         }
20     }
21     return 0;
22 }
23
24 int main() {
25     int u,v,cap,ans,caps,f;
26     //freopen("data.txt","r",stdin);
27     while(scanf("%d %d %d",&k,&n,&m)!=EOF) {
28         memset(s,0,sizeof(s));
29         for(int i=0;i<m;i++){
30             scanf("%d %d %d",&u,&v,&cap);
31             if(u==v) continue;
32             s[u][v]|=(1<<cap);
33             s[v][u]|=(1<<cap);
34         }
35         ans=k+1;
36         caps=0;
37         for(int i=1;i<(1<<k);i++){
38             int cnt=0;
39             for(int j=0;j<k;j++){
40                 if(i&(1<<j)) cnt++;
41             }
42             if(cnt>=ans) continue;
43             memset(vis,0,sizeof(vis));
44             if(dfs(0,i)){
45                 ans=cnt;
46                 caps=i;
47             }
48         }
49         printf("%d\n",ans);
50         f=0;
51         for(int i=0;caps>0;caps>>=1,i++){
52             if(caps&1){
53                 if(f++) printf(" ");
54                 printf("%d",i);
55             }
56         }
57         printf("\n");
58     }
59     return 0;
60 }

/*SGU 1500*/

时间: 2024-08-01 06:57:29

SGU -1500 - Pass Licenses的相关文章

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

1500. Pass Licenses Time limit: 2.5 secondMemory 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 un

URAL 1500. Pass Licenses 枚举+位运算

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 u

Open Source and Applications and Licenses

Part 1: Major Open Source Applications The Linux kernel can run a wide variety of software across many hardware platforms. A computer can act as a server, which means it primarily handles data on other’s behalf, or can act as a desktop, which means a

pass语句

Python pass是空语句,是为了保持程序结构的完整性.  pass 不做任何事情,一般用做占位语句. #!/usr/bin/python # -*- coding: UTF-8 -*- # 输出 Python 的每个字母 for letter in 'Python': if letter == 'h': pass print '这是 pass 块' print '当前字母 :', letter print "Good bye!" 执行结果: 当前字母 : P 当前字母 : y 当

1500: [NOI2005]维修数列

1500: [NOI2005]维修数列 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 12952  Solved: 4138[Submit][Status][Discuss] Description Input 输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目.第2行包含N个数字,描述初始时的数列.以下M行,每行一条命令,格式参见问题描述中的表格.任何时刻数列中最多含有500 000个数,

【SGU 390】Tickets (数位DP)

Tickets Description Conductor is quite a boring profession, as all you have to do is just to sell tickets to the passengers. So no wonder that once upon a time in a faraway galaxy one conductor decided to diversify this occupation. Now this conductor

ACM: SGU 101 Domino- 欧拉回路-并查集

sgu 101 - Domino Time Limit:250MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u Description Dominoes – game played with small, rectangular blocks of wood or other material, each identified by a number of dots, or pips, on its face. The bl

for S7-1200&amp;1500 HwLib&amp;Example R150508

for S7-1200&1500 HwLib&Example R150508 http://yunpan.cn/cj6qtNeJghyjr 访问密码 f517 2015.05.05 1, 重新分类,统一接口: 2,编写文档: 2013.03.12 1, 重新组织结构,优化代码: 2012.11.09 1, 初版;

Bus Pass

ZOJ Problem Set - 2913 Time Limit: 5 Seconds      Memory Limit: 32768 KB You travel a lot by bus and the costs of all the seperate tickets are starting to add up. Therefore you want to see if it might be advantageous for you to buy a bus pass. The wa