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 ...
- HTML5 History对象,Javascript修改地址栏而不刷新页面
一.History对象 History 对象包含用户(在浏览器窗口中)访问过的 URL. History 对象是 window 对象的一部分,可通过 window.history 属性对其进行访问. ...
- Android开发手记(15) 拨打电话和收发短信
1.Intent简介 Android组价之间的通信,由Intent来协助完成.Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Android则根据此Intent的描述,负责找到 ...
- 顶部图片放大回弹效果Scrollview ---- 各应用中常见的自定义View 解析
原理并不难. 代码量也不大. 非常简洁 . 先来个效果图 再上一波代码. public class SpecialScrollView extends ScrollView implements ...
- Web服务图片压缩,nginx+lua生成缩略图
背景 目前而言,用移动端访问Web站点的用户越来越多,图片对流量的消耗是比较大的,之前一个用户用我们网站的app浏览的时候,2个小时耗去了2个G的流量,这是个很严重的问题,需要对图片进行压缩,减少对用 ...
- POJ3185 The Water Bowls(反转法or dfs 爆搜)
POJ3185 The Water Bowls 题目大意: 奶牛有20只碗摆成一排,用鼻子顶某只碗的话,包括左右两只在内的一共三只碗会反向,现在给出碗的初始状态,问至少要用鼻子顶多少次才能使所有碗都朝 ...
- javascript知识图谱
- js性能优化--学习笔记
<高性能网站建设进阶指南>: 1.使用局部变量,避免深入作用域查找,局部变量是读写速度最快的:把函数中使用次数超过一次的对象属性和数组存储为局部变量是一个好方法:比如for循环中的.len ...
- 在jQuery.getData中renderCallback使用不同创建方式的结果
JavaScript 中需要创建函数的话,有两种方法:函数声明.函数表达式,各自写法如下:// 方法一:函数声明function foo() {}// 方法二:函数表达式var foo = funct ...
- memcache 数据库信息存储到数据库减少IO 操作
在实际应用中,通常会把数据库查询的结果集保存到 memcached 中,下次访问时直接从 memcached 中获取,而不再做数据库查询操作,这样可以在很大程度上减轻数据库的负担.通常会 将 SQL ...