1500. Pass licensestime limit: 2.5 second
Memory limit: 64 MBA 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 his Hummer and had no problems until a recent demo-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. inputthe input contains the street plan in the following format. there are integers
K,
N, And
MIn the first line, where
KIs the number of street categories (1 ≤
K≤ 20 ),
NIs the number of Crossroads (2 ≤
N≤ 30), and
MIs the number of descriptions of street segments between crossroads. Each of the next
MLines describes a street segment by three Integers
V1
V2
C, Where
V1And
V2Are the numbers of the crossroads limiting this segment, and
CIs its category. crossroads are numbered from 0
N-1, categories are numbered from 0
K-1. for any pair of crossroads No two segments of the same category connect these crossroads. outputoutput 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 shoshould be separated with spaces. it is guaranteed that such list is always exist. sample
Input |
Output |
3 3 30 2 00 2 11 2 2 |
20 2 |
Problem Author:Magaz asanov, Alexander mironenko, Anton botov, Evgeny krohalev
Problem Source:Quarter-final of xxxi acm icpc-Yekaterinburg-2006 meaning: K passes, N points, and m edges are given. Each edge belongs to one or more passes. The condition for passing this edge is that there is at least one pass. Calculate the minimum number of passes so that the pass can be sent from 0 to 1. Idea: each edge can be written as a status cost according to the pass. For example, if pass 1 3 is available, it can be expressed as 1010. Then, for the status s of the master pass, the condition for passing this edge is S & cost! = 0. So it's okay to enumerate the status of each host and determine whether the DFS can reach the destination, after half a minute, it turned out to be the condition for the end of the for loop. I was so careless.
#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=1<<30;const double eps=1e-6;const int N = 35;int cost[N][N];int k,n,m;bool vis[N];bool dfs(int s,int u){ if(u==1) return 1; if(vis[u]) return 0; vis[u]=1; for(int v=0;v<n;v++) { if(u==v) continue; if((cost[u][v]&s)) if(dfs(s,v)) return 1; } return 0;}int getcnt(int s){ int i,cnt=0; for(i=0;i<k;i++) if(test(s,i)) cnt++; return cnt;}void print(int s){ for(int i=0;i<k;i++) if(test(s,i)) printf("%d ",i); puts("");}void run(){ memset(cost,0,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+1; for(int i=1;i<(1<<k);i++) { memset(vis,0,sizeof(vis)); int tmp = getcnt(i); if(tmp>=anst) continue; if(dfs(i,0)) { 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 0;}
Ural 1500 pass licenses (State compression + DFS)