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
Problem Author: Magaz Asanov, Alexander Mironenko, Anton Botov, Evgeny Krokhalev
Problem Source: Quarter-Final of XXXI ACM ICPC - Yekaterinburg - 2006
 
 
题意:给出k种通行证,n个点,m条边。每条边都会属于一个或几个通行证,能通过这条边的条件是有至少一个通行证。求最少通行证数量使得能从0点出发到1点。
 
思路:每条边按照通行证可以写成一个状态cost,例如有通行证1 3可以表示为1010,然后对于机主通行证的状态S,那能通过这条边的条件就是 S&cost != 0 。所以枚举每一个机主状态,dfs一遍判断能否到达终点就ok了
妈蛋这么水的题哇了那么多次,搞了半个钟原来是for循环结束条件的问题,我擦好粗心。
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include <utility>
#include <queue>
#include <stack>
#define set(s,x) (s|=(1<<x))
#define test(s,x) (s&(1<<x))
using namespace std;
const int INF=<<;
const double eps=1e-;
const int N = ; int cost[N][N];
int k,n,m;
bool vis[N]; bool dfs(int s,int u)
{
if(u==) return ;
if(vis[u]) return ;
vis[u]=;
for(int v=;v<n;v++)
{
if(u==v) continue;
if((cost[u][v]&s))
if(dfs(s,v))
return ;
}
return ;
} int getcnt(int s)
{
int i,cnt=;
for(i=;i<k;i++)
if(test(s,i))
cnt++;
return cnt;
} void print(int s)
{
for(int i=;i<k;i++)
if(test(s,i))
printf("%d ",i);
puts("");
} void run()
{
memset(cost,,sizeof(cost));
int u,v,w;
while(m--)
{
scanf("%d%d%d",&u,&v,&w);
if(u==v) continue;
set(cost[u][v],w);
set(cost[v][u],w);
}
int ans, anst = k+;
for(int i=;i<(<<k);i++)
{
memset(vis,,sizeof(vis));
int tmp = getcnt(i);
if(tmp>=anst) continue;
if(dfs(i,))
{
anst = tmp;
ans = i;
}
}
printf("%d\n",anst);
print(ans);
} int main()
{
#ifdef LOCAL
freopen("case.txt","r",stdin);
#endif
while(scanf("%d%d%d",&k,&n,&m)!=EOF)
run();
return ;
}

ural 1500 Pass Licenses (状态压缩+dfs)的更多相关文章

  1. SGU -1500 - Pass Licenses

    先上题目: 1500. Pass Licenses Time limit: 2.5 secondMemory limit: 64 MB A New Russian Kolyan believes th ...

  2. hihocoder 1334 - Word Construction - [hiho一下第170周][状态压缩+DFS]

    题目链接:https://hihocoder.com/problemset/problem/1334 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Given N wo ...

  3. HDU 1198 Farm Irrigation(状态压缩+DFS)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1198 题目: Farm Irrigation Time Limit: 2000/1000 MS (Ja ...

  4. POJ-3279.Fliptile(二进制状态压缩 + dfs) 子集生成

    昨天晚上12点刷到的这个题,一开始一位是BFS,但是一直没有思路.后来推了一下发现只需要依次枚举第一行的所有翻转状态然后再对每个情况的其它田地翻转进行暴力dfs就可以,但是由于二进制压缩学的不是很透, ...

  5. codeforces B - Preparing Olympiad(dfs或者状态压缩枚举)

    B. Preparing Olympiad You have n problems. You have estimated the difficulty of the i-th one as inte ...

  6. 最大联通子数组之和(dfs,记忆化搜索,状态压缩)

    最大联通子数组,这次的题目,我采用的方法为dfs搜索,按照已经取到的数v[][],来进行搜索过程的状态转移,每次对v[][]中标记为1的所有元素依次取其相邻的未被标记为1的元素,将其标记为1,然而,这 ...

  7. poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)

    Description Flip game squares. One side of each piece is white and the other one is black and each p ...

  8. UVA 1508 - Equipment 状态压缩 枚举子集 dfs

    UVA 1508 - Equipment 状态压缩 枚举子集 dfs ACM 题目地址:option=com_onlinejudge&Itemid=8&category=457& ...

  9. uva10160(dfs+状态压缩)

    题意:给出n个点,以及m条边,这些边代表着这些点相连,修一个电力站,若在某一点修一个站,那么与这个点相连的点都可以通电,问所有的点都通电的话至少要修多少个电力站........ 思路:最多给出的是35 ...

随机推荐

  1. python classmethod方法 和 staticmethod

    classmethod() 是一个类方法,用来装饰对应的函数.被classmethod 装饰之后就无需实例化,也不需要在函数中传self,但是被装饰的函数第一个参数需要是cls来表示自身类.可以用来调 ...

  2. JS学习总结之操作文档对象模型

    操作文档对象模型 DOM 结构树 文档对象模型(Document Object Model,简称DOM),是W3C组织推荐的处理可拓展置标语言的标准编程接口.它是一种与平台和语言无关的应用程序接口(A ...

  3. struts2的 defalut-action-ref 的使用

    这个配置的用法有值得注意的地方,所以才记录下来: 一般default-action-refer配置的action是在浏览器中输入的网址只输入到项目时或输入错误的action时 所进入的action,一 ...

  4. Eclipse javax.servlet.jsp.PageContext cannot be resolved to a type 错误解决办法

    不要 直接将jsp-api.jar拷贝到lib目录下,而是通过外部jar包引用.项目 右键->Properties->Libraries->Add External JARS-选择 ...

  5. [2018-11-03]2018年10月28日宁波dotnet社区活动回顾及下次活动预告

    离上次活动,有半年了,汗.之后尽量保证每月一次,以组织为主,多邀请嘉宾来分享. 本次活动不足之处 人手不足:由于活动组织事项受限于人手(目前就我一个,这次活动前后我又应邀给大红鹰学院应届生介绍dotn ...

  6. nginx语法之location详解

    Location语法优先级排列 匹配符 匹配规则 优先级 = 精确匹配 ^~ 以某个字符串开头 ~ 区分大小写的正则匹配 ~* 不区分大小写的正则匹配 !~ 区分大小写不匹配的正则 !~* 不区分大小 ...

  7. Java多线程系列 基础篇06 synchronized(同步锁)

    转载 http://www.cnblogs.com/paddix/ 作者:liuxiaopeng http://www.infoq.com/cn/articles/java-se-16-synchro ...

  8. jQuery 网页禁止复制

    <script type="text/javascript">    $(document).ready(function(){         $('#文本框id') ...

  9. Contiki Ctimer模块

    Ctimer 提供和Etimer类似的功能,只是Ctimer是在一段时间后调用回调函数,没有和特定进程相关联. 而Etimer是在一段时间后发送PROCESS_EVENT_TIMER事件给特定的进程. ...

  10. Contiki事件

    一.事件相关数据结构 1.事件结构体 struct event_data { process_event_t ev; process_data_t data; struct process *p; } ...