URAL 1500. Pass Licenses enumeration + bitwise operation

Source: Internet
Author: User

Pass Licensestime limit:2.5 Second
Memory limit:64 MB
A New Russian Kolyan believes, which to spend he time in traffic jams are below his dignity. This is what 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 all street of the city belongs to one or several categories, and a driver must has a separate license in order to us e an emergency flashlight in the streets for each category. If a street belongs to several categories, it's sufficient to has a license only for one of the these categories. For each category, a license are issued by a separate city official. Although these officials is different, they accept bribes of the same amount for giving a license. Help Kolyan to find a-a-a-go-from-his-home to-work such-a-can-do-this-on-the-flashlight turned on and have Spen t the minimal amount for bribes. Inputthe input contains the street plan in the following format. There is integersK,N, andMIn the first line, whereKIs the number of the street categories (1≤K≤20),NIs the number of crossroads (2≤N≤30), andMis the number of descriptions of the street segments between crossroads. Each of the nextMLines describes a street segment by three integersv1 v2 C, whereV1 andV2 Is the numbers of the crossroads limiting this segment, andCis its category. Crossroads is numbered from 0 toN–1, categories is numbered from 0 toK–1. For any pair of crossroads no 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) has a emergency flashlight turned on. In the second line, give the list of categories for which licenses must is obtained. The numbers should is separated with spaces. It is guaranteed this 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 Krokhalev
problem Source:Quarter-final of XXXI ACM icpc-yekaterinburg-2006
Tags:Graph theory() difficulty:677


Test instructions: There are n points, K-type traffic card, M-Path. Each road has a corresponding traffic card, the corresponding traffic card may have more than one, you can pass. Ask from 0 to 1, the required traffic card at least a few, and the output is which several.


Practice: Because the data is very small, so you can enumerate traffic cards, complexity is 2^k=10^6. Then Dfs, each point only walks once, so the complexity is n. So the total complexity is 10^7. It took a long time to run a 1.7s.


int mp[30][30]; int k,n,m;//k Cat n point m route int vv[30];int cat=0;int DFS (int now)//0-1{if (now==1) return 1;vv[now]=1;for (int i=0;i<n;i + +) {if (vv[i]==0&& (Mp[now][i]&cat)) {if (Dfs (i)) {//here do not change Vis[i] back to 0, because only to get the arrival, no other conditions,//So each point is only accessed once. Before the change back to 0, has been tle, because the change back to 0, the complexity is n^2. And this complexity only uses N. return 1;} }}return 0;} int Arr;int Main () {scanf ("%d%d%d", &k,&n,&m),//k cat N-point M-side memset (mp,0,sizeof MP); int v1,v2,c;for (int i=0;i <m;i++) {scanf ("%d%d%d", &v1,&v2,&c); mp[v1][v2]|= (1<<c); mp[v2][v1]=mp[v1][v2];} int lim= (1<<k); int Ans=k;arr=lim-1;memset (vv,0,sizeof vv); for (int i=0;i<lim;i++) {int Cnt=0;int tem=i;while ( TEM) {if (tem&1) cnt++;tem>>=1;} if (Cnt>=ans) continue;cat=i; memset (vv,0,sizeof vv); if (Dfs (0)) {ans=cnt;arr=i;}} printf ("%d\n", ans), int fir=1;for (int i=0;i<k;i++) {if (arr& (1<<i)) {if (FIR) {fir=0;printf ("%d", I);} elseprintf ("%d", I);}} Puts (""); return 0;}


URAL 1500. Pass Licenses enumeration + bitwise operation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.