Bandwidth 

Given a graph (V,E) where V is a set of nodes and E is a set of arcsin VxV, and anordering on the elements in V, then the bandwidth of a node v is defined as the maximum distance in theordering between v and any node to which it is connected in thegraph. The bandwidth of the ordering is then defined as the maximum ofthe 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 minimisesthe bandwidth.

Input

Input will consist of a series of graphs. Each graph will appear on aline by itself. The entire file will be terminated by a lineconsisting of a single#. For each graph, the input will consist ofa series of records separated by `;'. Each record will consist of anode 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 willcontain no more than 8 nodes.

Output

Output will consist of one line for each graph, listing the orderingof the nodes followed by an arrow (->) and the bandwidth for thatordering. All items must be separated from their neighbours by exactlyone space. If more than one ordering produces the same bandwidth, thenchoose the smallest in lexicographic ordering, that is the one thatwould 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

题意有点难理解:
给出一系列有线连接的点的序列
如样例: A:FB 代表A点与F点B点有连线
然后我们可以对这些点进行排列, 有线连接的不一定是相邻的
对于每一组可能的排列, 我们都要找出该组中, 距离最长的两个点的距离
最后, 对于所有可能的排列所得到的最长距离中, 选最长距离最小的那个 做法: 网上很多都是回溯法, 但是这题明显可以用暴力! AC代码:
#include<stdio.h>
#include<string.h>
#include<algorithm> using namespace std; char date[100];
char ans[10];
int Min = 100; int cmp(char a, char b) {
return a < b;
} int dis(int a, int b) {
if(a > b)
return a - b;
else
return b - a;
} int main() {
while(gets(date) != NULL) {
char map[10];
if(date[0] == '#')
break;
int len = strlen(date);
int k = 0;
for(int i = 0; i < len; i++) {
int mark = 1;
for(int j = 0; j < k; j++) {
if(map[j] == date[i])
mark = 0;
}
if(mark && date[i] >= 'A' && date[i] <= 'Z')
map[k++] = date[i];
}
sort(map, map+k, cmp);
int d, cur_max;
do{
cur_max = 1;
int a, b;
int flag = 1;
for(int i = 0; i < len; i++) {
if(date[i] == ';') {
flag = 1;
continue;
}
if(date[i] == ':') {
flag = 0;
continue;
}
if(flag == 1 && date[i] >= 'A' && date[i] <= 'Z') {
for(int j = 0; j < k; j++)
if(map[j] == date[i]) {
a = j;
break;
}
}
else if(flag == 0 && date[i] >= 'A' && date[i] <= 'Z') {
for(int j = 0; j < k; j++) {
if(map[j] == date[i]) {
b = j;
d = dis(a, b);
if(d > cur_max) {
cur_max = d;
break;
}
}
}
}
}
if(cur_max < Min) {
Min = cur_max;
for(int i = 0; i < k; i++)
ans[i] = map[i];
}
}while(next_permutation(map, map+k));
for(int i = 0; i < k; i++) {
printf("%c ", ans[i]);
}
printf("-> %d\n", Min);
memset(date, 0, sizeof(date));
Min = 100;
}
return 0;
}

最新文章

  1. OOP感想
  2. Android NDK中的C++调试踩坑标记
  3. 使用xml来显示获取的mysql数据
  4. 实现:TextView自由复制功能
  5. Generic method return type
  6. Target Operator ID has No Access to Upgrade
  7. 分享一个客户端程序(winform)自动升级程序,思路+说明+源码
  8. [转载]ASP.NET MVC 3的分部视图
  9. duilib中ListCtrl控件的实现
  10. [javascript]String添加trim和reverse方法
  11. SUN-LDAP6.3_RHEL 5.0-卸载LDAP
  12. hdu1533解题报告
  13. Java反射机制剖析(一)-定义和API
  14. struts2简单入门-配置文件-struts.xml
  15. day01 格式化输出和while循环的两个小练习
  16. linux--切换ipython解释器到python3
  17. Kafka实战-数据持久化
  18. Linux下 查找大文件
  19. Zookeeper安装使用及JavaAPI使用
  20. vue-自定义全局键盘码

热门文章

  1. spring-data-mongodb查询结果返回指定字段
  2. Linux下解决高并发socket最大连接数所受的各种限制(解除IO限制)
  3. android - 模拟器连接本地tomcat
  4. linux 命令及进程控制
  5. HDU 4632 CF 245H 区间DP(回文)
  6. Linux 0.11下信号量的实现和应用
  7. Hibernate的CRUD
  8. 常用排序算法总结(C#版)
  9. 兄弟连王牌PHP课程送三重豪礼啦!
  10. Win7-IIS7下运行PHP网站(以配置好的drupal网站为例)