uva 140 - Bandwidth
2012-07-31 19:32
453 查看
Bandwidth |
orderingontheelementsinV,thenthebandwidthofanode
visdefinedasthemaximumdistanceintheorderingbetweenvandanynodetowhichitisconnectedinthegraph.Thebandwidthoftheorderingisthendefinedasthemaximumoftheindividualbandwidths.Forexample,considerthefollowinggraph:
Thiscanbeorderedinmanyways,twoofwhichareillustratedbelow:
Fortheseorderings,thebandwidthsofthenodes(inorder)are6,6,1,4,1,1,6,6givinganorderingbandwidthof6,and5,3,1,4,3,5,1,4givinganorderingbandwidthof5.
Writeaprogramthatwillfindtheorderingofagraphthatminimisesthebandwidth.
Input
Inputwillconsistofaseriesofgraphs.Eachgraphwillappearonalinebyitself.Theentirefilewillbeterminatedbyalineconsistingofasingle#.Foreachgraph,theinputwillconsistofaseriesofrecordsseparatedby`;'.Eachrecordwillconsistofanodename(asingleuppercasecharacterinthetherange`A'to`Z'),followedbya`:'andatleastoneofitsneighbours.Thegraph
willcontainnomorethan8nodes.
Output
Outputwillconsistofonelineforeachgraph,listingtheorderingofthenodesfollowedbyanarrow(->)andthebandwidthforthatordering.Allitemsmustbeseparatedfromtheirneighboursbyexactlyonespace.Ifmorethanoneorderingproducesthesamebandwidth,thenchoosethesmallestinlexicographicordering,thatistheonethatwouldappearfirstinanalphabeticlisting.
Sampleinput
A:FB;B:GC;D:GC;F:AGH;E:HD #
Sampleoutput
ABCFGDHE->3
开始以为可以取26个大写字母以为有26种,后来看到题目说了最多8个点......没有什么技巧,无脑回溯过了。相同时输出字典序小的
#include<stdio.h> #include<string.h> #include<ctype.h> intn,max,map[26][26],visit[26]; chars[10],s1[10];//用字符串保存结果待会比较字典序用库函数比较方便 intdfs(intm) {inti,j,min; if(m>=n) {min=0; for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) if((map[s1[i]-'A'][s1[j]-'A']==1)&&(j-i>min))min=j-i; if(min<max){max=min;strcpy(s,s1);} if((min==max)&&(strcmp(s,s1)>0))strcpy(s,s1); return0; } for(i=0;i<26;i++) if(visit[i]==1) {visit[i]=0; s1[m]=i+'A'; dfs(m+1); visit[i]=1; } } intmain() {charch,CH; inti,j; while(scanf("%c",&ch)&&ch!='#') {memset(map,0,sizeof(map)); memset(visit,0,sizeof(visit)); while(scanf("%c",&CH)&&CH!='\n') {if(isalpha(CH)) {map[ch-'A'][CH-'A']=1;map[CH-'A'][ch-'A']=1; visit[ch-'A']=1;visit[CH-'A']=1; } if(CH==';')scanf("%c",&ch); } n=0;max=9999; for(i=0;i<26;i++) if(visit[i]){s =i+'A';++n;} s ='\0';s1 ='\0'; dfs(0); for(i=0;s[i]!='\0';i++) printf("%c",s[i]); printf("->%d\n",max); } return0; }
[/code]
相关文章推荐
- 带宽(Bandwidth,UVa 140
- uva 140 Bandwidth 带宽
- UVA 140_Bandwidth
- UVa 140 带宽 Bandwidth
- uva140 - Bandwidth
- UVA - 140 Bandwidth
- UVA-140 Bandwidth 带宽
- UVa 140:Bandwidth(暴力)
- UVA-140 Bandwidth
- UVa140- Bandwidth
- uva140 - Bandwidth
- UVA140 - Bandwidth
- uva 140 Bandwidth(全排列+递归)
- UVA 140(p196)----Bandwidth
- UVA140 ——bandwidth(搜索)
- UVa 140 - Bandwidth(回溯)
- UVA 140——Bandwidth(暴力)
- UVA - 140 Bandwidth
- UVA - 140 Bandwidth(全排列枚举)
- UVa 140 - Bandwidth