uva 140 Bandwidth
来源:互联网 发布:区姓和欧姓 知乎 编辑:程序博客网 时间:2024/05/01 01:02
题目地址:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_problem&problem=76
题目描述:
Bandwidth
Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an ordering on the elements in V, then the bandwidth of a node v is defined as the maximum distance in the ordering between v and any node to which it is connected in the graph. The bandwidth of the ordering is then defined as the maximum of the individual bandwidths. For example, consider the following graph:
This can be ordered in many ways, two of which are illustrated below:
For these orderings, the bandwidths of the nodes (in order) are 6, 6, 1, 4, 1, 1, 6, 6 giving an ordering bandwidth of 6, and 5, 3, 1, 4, 3, 5, 1, 4 giving an ordering bandwidth of 5.
Write a program that will find the ordering of a graph that minimises the bandwidth.
Input
Input will consist of a series of graphs. Each graph will appear on a line by itself. The entire file will be terminated by a line consisting of a single #. For each graph, the input will consist of a series of records separated by `;'. Each record will consist of a node name (a single upper case character in the the range `A' to `Z'), followed by a `:' and at least one of its neighbours. The graph will contain no more than 8 nodes.
Output
Output will consist of one line for each graph, listing the ordering of the nodes followed by an arrow (->) and the bandwidth for that ordering. All items must be separated from their neighbours by exactly one space. If more than one ordering produces the same bandwidth, then choose the smallest in lexicographic ordering, that is the one that would appear first in an alphabetic listing.
Sample input
A:FB;B:GC;D:GC;F:AGH;E:HD#
Sample output
A B C F G D H E -> 3
题意:给出图的构造,按定义找最小bandwidth.
题解:直接依据字符串搜索来判断两个顶点的相邻状况从而省去建立相应数据结构的代价,当然也可构造相应的数据结构而省去搜索的代价,根据数据规模来看,搜索的代价并不高。然后就是全排列,按定义计算bandwidth,然后选最小即可。
代码:
/*use list to deal with the graph*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <ctype.h>#include <math.h>#include <iostream>#include <algorithm>using namespace std;char GStr[1000]={'\0'};int BandWith=0;//char Permu[8]={'\0'};char Permu[8+5]={'\0'};int MinBand=100;//char MinPermu[8]={'\0'};char MinPermu[8+5]={'\0'};//why plus 5 that would not be excepted?????/*find the adjacent char from the GStr*/int FindAdj(char c,char Adj[],int &AdjLen){int i=0;int Len=strlen(GStr);for(i=0;i<=Len-1;i++){if(GStr[i]==c){if(GStr[i+1]==':')//the repeated is acceptable{int j=i+2;while(j<=Len-1&&GStr[j]!=';'){Adj[AdjLen++]=GStr[j];j++;}}else{int j=i-1;while(j>=0&&GStr[j]!=':'){j--;}if(j>0){Adj[AdjLen++]=GStr[j-1];}}}}Adj[AdjLen]='\0';return(0);}/*Permu[i] and the Adj[j]'s dis in Permu*/int GetPos(char Permu[],int PermuLen,char c){int pos=0;int i=0;for(i=0;i<=PermuLen-1;i++){if(Permu[i]==c){pos=i;break;}}return(pos);}/*for test*/int test(){return(0);}/*main process*/int MainProc(){while(scanf("%s",GStr)!=EOF&&GStr[0]!='#'){MinBand=100;BandWith=0;int Len=strlen(GStr);int PermuLen=0;int i=0;//get the distinct char stringchar PreStr[1000]={'\0'};strcpy(PreStr,GStr);sort(PreStr,PreStr+Len);for(i=0;i<=Len-1;i++){if(isalpha(PreStr[i])){if(PermuLen==0){Permu[PermuLen++]=PreStr[i];}else{if(Permu[PermuLen-1]!=PreStr[i])//distinct string{Permu[PermuLen++]=PreStr[i];}}}}Permu[PermuLen]='\0';//end of the string//use nextpermutation to the Permu sort(Permu,Permu+PermuLen);do{int dis=0;int MaxDis=0;//get the per char's bandwithfor(i=0;i<=PermuLen-1;i++){//find the adjacent char from the GStrchar Adj[8]={'\0'};int AdjLen=0;FindAdj(Permu[i],Adj,AdjLen);//calculate the bandwithint j=0;for(j=0;j<=AdjLen-1;j++){dis=abs(GetPos(Permu,PermuLen,Adj[j])-i);//Permu[i] and the Adj[j]'s dis in Permuif(dis>MaxDis){MaxDis=dis;}}}//MinBandif(MaxDis==MinBand){if(strcmp(MinPermu,Permu)>0){strcpy(MinPermu,Permu);}}else if(MaxDis<MinBand){MinBand=MaxDis;strcpy(MinPermu,Permu);}}while(next_permutation(Permu,Permu+PermuLen));for(i=0;i<=PermuLen-1;i++){printf("%c ", MinPermu[i]);}printf("-> %d\n", MinBand);}return(0);}int main(int argc, char const *argv[]){/* code */MainProc();return 0;}
- UVa 140 - Bandwidth
- uva 140 - Bandwidth
- UVa 140 - Bandwidth
- uva 140 - Bandwidth
- UVa 140 - Bandwidth
- UVA 140 - Bandwidth
- UVa 140 - Bandwidth
- UVA 140 Bandwidth
- uva 140 Bandwidth
- uva 140 Bandwidth
- UVa:140 Bandwidth
- UVa 140 - Bandwidth
- uva 140 - Bandwidth
- UVa 140 Bandwidth
- UVa 140 - Bandwidth
- uva 140 Bandwidth 带宽
- UVA - 140 Bandwidth
- UVA - 140 Bandwidth
- 怎样判断浏览器的类型
- 深入研究Asp.net页面的生命周期 [引用]
- makefile / Android.mk中添加打印信息
- linux创建一定大小文件命令--dd 制作img
- 作为WEB工程师,我们是不是应该积极的推进一下用户浏览器的使用体验?
- uva 140 Bandwidth
- domino and mmc.exe (1936/0x790) has terminated abnormally
- .ini配置文件书写格式(转)
- 如何检测内存泄漏
- 网人5.02和DZ7整合文件及简要说明
- 排序算法总结2
- 【linux 开发】定时器使用setitimer
- 提高php执行效率
- 使用TextView/EditText应该注意的地方