UVA 140 (13.07.29)
| 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)的更多相关文章
- UVA 10392 (13.07.28)
Problem F: Factoring Large Numbers One of the central ideas behind much cryptography is that factori ...
- UVA 299 (13.07.30)
Train Swapping At an old railway station, you may still encounter one of the lastremaining ``train ...
- UVA 568 (13.07.28)
Just the Facts The expression N!, read as `` N factorial," denotes the product of the first N ...
- UVA 408 (13.07.28)
Uniform Generator Computer simulations often require random numbers. One way to generatepseudo-ran ...
- 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が ...
- 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 ...
- UVA 10194 (13.08.05)
:W Problem A: Football (aka Soccer) The Problem Football the most popular sport in the world (ameri ...
- 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 ...
- 【2018.07.29】(深度优先搜索/回溯)学习DFS算法小记
参考网站:https://blog.csdn.net/ldx19980108/article/details/76324307 这个网站里有动态图给我们体现BFS和DFS的区别:https://www ...
随机推荐
- RESTful Web Services简单介绍
近几年,RESTful Web Services渐渐开始流行,大量用于解决异构系统间的通信问题.很多网站和应用提供的API,都是基于RESTful风格的Web Services,比较著名的包括Twit ...
- target,currentTarget,delegateTarget,srcElement
第一种情况:就是IE9+和其他现代浏览器,支持addEventListener方法.其结果是: this总是等于currentTarget currentTarget总是事件监听者 target总是事 ...
- iOS使用阿里云OSS对象存储 (SDK 2.1.1)
最近项目中用到了阿里云OSS对象存储,用来存储APP中图片.音频等一些数据.但坑爹的阿里云居然在11月20日将SDK版本更新到了2.1.1,然而网上给出的教程都是1.*版本的(针对iOS),两个版本所 ...
- iOS 中Window优先级的问题
在项目中,视频播放时候遇到网络切换需要弹出AlertView提醒用户,忽然发现转屏的时候播放View加到KeyWindow的时候把AleryView挡住了.如图 因为转屏的时候视图是直接加载到 [UI ...
- Java中异常的基本应用(一)
在Java中,我们把异常当做一种对象来处理,正是异常机制的引入,使得我们的程序更加健壮.异常指示了一个不正常的条件,或者一个错误条件,简单地说就是一个中断了正常的指令流的事件.程序控制将无条件的抛至一 ...
- js性能优化--学习笔记
<高性能网站建设进阶指南>: 1.使用局部变量,避免深入作用域查找,局部变量是读写速度最快的:把函数中使用次数超过一次的对象属性和数组存储为局部变量是一个好方法:比如for循环中的.len ...
- javascript Array类型 方法大全
1,创建数组 //第一种是使用Array构造函数 var colors = new Array(); var colors = new Array(20); //创建length为20的数组 var ...
- JQUERY1.9学习笔记 之基本过滤器(十) 非选择器
非选择器jQuery( ":not(selector)" ) 例:找出所有input标签为非"checked"的,并且高亮其邻居元素span. <!DOC ...
- [Python笔记]第六篇:文件处理
本篇主要内容:open文件处理函数的使用 open函数,该函数用于文件处理 操作文件时,一般需要经历如下步骤: 打开文件 操作文件 一.打开文件 文件句柄 = open('文件路径', '模式') 打 ...
- USACO1.5 Checker Challenge(类n皇后问题)
B - B Time Limit:1000MS Memory Limit:16000KB 64bit IO Format:%lld & %llu Description E ...