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

  1. A:FB;B:GC;D:GC;F:AGH;E:HD
  2. #

Sample output

  1. A B C F G D H E -> 3
  2.  
  3. 题意有点难理解:
  4. 给出一系列有线连接的点的序列
  5. 如样例: A:FB 代表A点与FB点有连线
  6. 然后我们可以对这些点进行排列, 有线连接的不一定是相邻的
  7. 对于每一组可能的排列, 我们都要找出该组中, 距离最长的两个点的距离
  8. 最后, 对于所有可能的排列所得到的最长距离中, 选最长距离最小的那个
  9.  
  10. 做法: 网上很多都是回溯法, 但是这题明显可以用暴力!
  11.  
  12. AC代码:
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<algorithm>
  4.  
  5. using namespace std;
  6.  
  7. char date[100];
  8. char ans[10];
  9. int Min = 100;
  10.  
  11. int cmp(char a, char b) {
  12. return a < b;
  13. }
  14.  
  15. int dis(int a, int b) {
  16. if(a > b)
  17. return a - b;
  18. else
  19. return b - a;
  20. }
  21.  
  22. int main() {
  23. while(gets(date) != NULL) {
  24. char map[10];
  25. if(date[0] == '#')
  26. break;
  27. int len = strlen(date);
  28. int k = 0;
  29. for(int i = 0; i < len; i++) {
  30. int mark = 1;
  31. for(int j = 0; j < k; j++) {
  32. if(map[j] == date[i])
  33. mark = 0;
  34. }
  35. if(mark && date[i] >= 'A' && date[i] <= 'Z')
  36. map[k++] = date[i];
  37. }
  38. sort(map, map+k, cmp);
  39. int d, cur_max;
  40. do{
  41. cur_max = 1;
  42. int a, b;
  43. int flag = 1;
  44. for(int i = 0; i < len; i++) {
  45. if(date[i] == ';') {
  46. flag = 1;
  47. continue;
  48. }
  49. if(date[i] == ':') {
  50. flag = 0;
  51. continue;
  52. }
  53. if(flag == 1 && date[i] >= 'A' && date[i] <= 'Z') {
  54. for(int j = 0; j < k; j++)
  55. if(map[j] == date[i]) {
  56. a = j;
  57. break;
  58. }
  59. }
  60. else if(flag == 0 && date[i] >= 'A' && date[i] <= 'Z') {
  61. for(int j = 0; j < k; j++) {
  62. if(map[j] == date[i]) {
  63. b = j;
  64. d = dis(a, b);
  65. if(d > cur_max) {
  66. cur_max = d;
  67. break;
  68. }
  69. }
  70. }
  71. }
  72. }
  73. if(cur_max < Min) {
  74. Min = cur_max;
  75. for(int i = 0; i < k; i++)
  76. ans[i] = map[i];
  77. }
  78. }while(next_permutation(map, map+k));
  79. for(int i = 0; i < k; i++) {
  80. printf("%c ", ans[i]);
  81. }
  82. printf("-> %d\n", Min);
  83. memset(date, 0, sizeof(date));
  84. Min = 100;
  85. }
  86. return 0;
  87. }

UVA 140 (13.07.29)的更多相关文章

  1. UVA 10392 (13.07.28)

    Problem F: Factoring Large Numbers One of the central ideas behind much cryptography is that factori ...

  2. UVA 299 (13.07.30)

     Train Swapping  At an old railway station, you may still encounter one of the lastremaining ``train ...

  3. UVA 568 (13.07.28)

     Just the Facts  The expression N!, read as `` N factorial," denotes the product of the first N ...

  4. UVA 408 (13.07.28)

     Uniform Generator  Computer simulations often require random numbers. One way to generatepseudo-ran ...

  5. Feb 5 13:07:52 plugh rsyslogd-2177: imuxsock begins to drop messages from pid 12105 due to rate-limiting

    FROM:https://www.nri-secure.co.jp/ncsirt/2013/0218.html SANSインターネットストームセンターのハンドラであるJohannes Ullrichが ...

  6. 07/29/2013 02:10:02 AM - CMDPHP: Poller[0] Host[6] DS[10] WARNING: Result from SNMP not valid. Partial Result: U

    snmpwalk -c public -v2c  客户端ip地址  自定义的oid  能取到数据,但是服务器端就是图片一片空白 rrdtool fetch 文件名.rrd 查看到的全都是nan cac ...

  7. UVA 10194 (13.08.05)

    :W Problem A: Football (aka Soccer)  The Problem Football the most popular sport in the world (ameri ...

  8. Saving James Bond - Easy Version 原创 2017年11月23日 13:07:33

    06-图2 Saving James Bond - Easy Version(25 分) This time let us consider the situation in the movie &q ...

  9. 【2018.07.29】(深度优先搜索/回溯)学习DFS算法小记

    参考网站:https://blog.csdn.net/ldx19980108/article/details/76324307 这个网站里有动态图给我们体现BFS和DFS的区别:https://www ...

随机推荐

  1. IIS支持下载.config后缀名的文件

    这里用.config举例,其他类型文件同理. 设置MIME点击右侧添加,文件扩展名填写.config,MIME类型填写application/octet-stream.或者添加.*,将所有未列出的文件 ...

  2. 第十二章作业 MemoryBugs-master项目优化笔记

    作业要求: 下载bug项目:https://github.com/lzyzsd/MemoryBugs,请注意配合使用MemoryMonitor, AllocationTracker以及HeapDump ...

  3. MapReduce中的排序

           hadoop的计算模型就是map/reduce,每一个计算任务会被分割成很多互不依赖的map/reduce计算单元,将所有的计算单元执行完毕后整个计算任务就完成了.因为计算单元之间互不依 ...

  4. cal命令详解与练习

    cal: 显示日历. 命令格式: cal [-smjy13] [[[day] month] year] 参数说明 -1 显示当前月日历 -3 显示当前月前后3月的日历 -s 以星期天为第一天显示 -m ...

  5. oracle常用SQL总结

    这里我们介绍的是 40+ 个非常有用的 Oracle 查询语句,主要涵盖了日期操作,获取服务器信息,获取执行状态,计算数据库大小等等方面的查询.这些是所有 Oracle 开发者都必备的技能,所以快快收 ...

  6. python的bind函数

    # -*- coding:utf-8 -*- class Functor(object): def __init__(self, func, index=0, *args, **kwargs): se ...

  7. getsockname和getpeername函数

    这2个函数或者返回与某个套接字关联的本地协议地址(getsockname),或者返回与某个套接字关联的外地协议地址(getpeername) int getsockname(int sockfd, s ...

  8. TCP状态转换图

    注意: connect函数导致当前套接字从CLOSE状态(该套接字自从由socket函数创建以来一直所处的状态)转移到SYN_SENT状态 若成功则再转移到ESTABLISHED状态, 若connec ...

  9. unwrap_uvw 笔记

    <integer><Unwrap_UVW>.numberVerticesByNode <node>node --返回图顶点的对应于给定节点的Unwrap_UVW点总 ...

  10. FLASK安装--兼收EZ_INSTALL及PIP

    参考URL: http://www.cnblogs.com/haython/p/3970426.html http://www.pythondoc.com/flask/installation.htm ...