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 ...
随机推荐
- jsp引入struts标签,引入自己写的jquery需要注意的问题
1.使用struts2标签的时候在jsp页面开头引入这句话: <%@ taglib prefix="s" uri="/struts-tags"%> ...
- jquery获取value值
$(function(){ alert(1); var a=$("#a004").val(); var a1=$("#b004").val(); //.val就 ...
- Java数字、货币值和百分数等的格式化处理
如果我们用下列语句输出一个数 System.out.println(123456.789); 将会在Console看到输出 123456.789 那么如何得到123,456.789这种格式化的输出呢? ...
- tomcat启动项目内存溢出问题
catalina.bat文件的第二行加下面的即可: 注意最大内存设置,和系统的内存有关系 set JAVA_OPTS=%JAVA_OPTS% -Xms512m -Xmx1024m -XX:PermSi ...
- Swift - 02 - 常量和变量
//: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...
- mysql hash索引优化
创建表 CREATE TABLE `t1` ( `id` int(11) NOT NULL AUTO_INCREMENT, `msg` varchar(20) NOT NULL DEFAULT ' ...
- windows8.1 下搭建配置apache+php+mysql
软件版本: apache:Apache 2.4.10 Win64 http://www.apachelounge.com/download/VC11/binaries/httpd-2.4.10- ...
- Python自动化运维之12、面向对象进阶
上一篇<面向对象基础>文章介绍了面向对象基本知识: 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使用(可以讲多函数中公 ...
- Python按行读取文件
1:readline() file = open("sample.txt") while 1: line = file.readline() if not line: break ...
- 我的sublime常用快捷键
sublime一般被应用于前端开发,在实际开发中,我们常用的sublime快捷键有哪些呢?这里汇总一下,常用的排在前面. 常用快捷键 Ctrl+Shift+P:打开命令面板 Ctrl+D:选择重复单词 ...