您的位置:首页 > 其它

uva 140 - Bandwidth

2012-07-31 19:32 453 查看



Bandwidth

Givenagraph(V,E)whereVisasetofnodesandEisasetofarcsinVxV,andan
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.Ifmorethanoneorderingproducesthe
samebandwidth,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]

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: