Start with the question:
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 |
Question: Give N points, M sides (undirected), and have k driver's licenses. If you want to pass each edge, you need a driver's license, ask how many drivers you need from to and output them.
Practice: compress the status and perform a brute-force check to verify the validity of the software. The minimum required driver's license is recorded.
S [I] [J] indicates which types of driver licenses can be used from I-> J. Then, enumerate different driver's licenses and find the minimum required number.
Code:
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 */
Sgu-1500-pass licenses