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;
}

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. 千万数量级分页存储过程 +AspNetPager现实分页

    存储过程 USE [ForeignTradeDB] GO /****** Object: StoredProcedure [dbo].[CommonGetDataPager] Script Date: ...

  2. ASP.NET5中间件

    小的应用组件可以包含到Http请求管道当中,ASP.NET5 集成了中间件,被包在了应用程序的Configure方法当中. 1. 什么是中间件 中间件是一组被装到应用程序管道的请求和响应中的组件.每一 ...

  3. 导入excel错误:外部表不是预期的格式 解决方案(Oledb)

    -----转载:http://blog.csdn.net/zhou349398998/article/details/8740424 环境:win7+iis7+Office2007 在asp.net网 ...

  4. javascript入门学习笔记

    <button type="button" onclick="alert('Welcome!')">点击这里</button>alert ...

  5. 31.Spring-开发流程.md

    [toc] 1.简单开发流程 1.1引用类库 基本类库: ## 1.2创建spring配置文件,文件的名称为固定格式:applicationContext.xml或者bean.xml: <?xm ...

  6. centos账户管理命令(root权限)

    cat /etc/passwd | grep -v /sbin/nologin | cut -d : -f 1        查看所有用户 userdel -r 用户名           -删除用户 ...

  7. C# 构造函数如何调用父类构造函数或其他构造函数

    class C : B{    C() : base(5)      // call base constructor B(5)    {  }    C(int i) : this()  // ca ...

  8. @font-face扒站的步骤

    今天模仿百度首页手机版的时候遇到的@font-face的问题,现在整理一下. 问题:图中红色区域,在拷贝F12样式的时候,并没有出现这些小图标.        图1:百度的效果             ...

  9. js 获取页面高度和宽度(兼容 ie firefox chrome),获取鼠标点击位置

    <script> //得到页面高度 var yScroll = (document.documentElement.scrollHeight >document.documentEl ...

  10. 获取win7时区所有信息

    打开命令行工具: tzutil /l # 或者输入到文件中tzutil /l > data.txt # -*- utf-8 -*- """获取win7所有时区信息, ...