UVA140 ——bandwidth(搜索)
Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an ordering on the elements in V, then the bandwidth of a node v is defined as the maximum distance in the ordering between v and any node to which it is connected in the graph. The bandwidth of the ordering is then defined as the maximum of the 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 minimises the bandwidth.
Input
Input will consist of a series of graphs. Each graph will appear on a line by itself. The entire file will be terminated by a line consisting of a single #. For each graph, the input will consist of a series of records separated by `;'. Each record will consist of a node 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 will contain no more than 8 nodes.
Output
Output will consist of one line for each graph, listing the ordering of the nodes followed by an arrow (->) and the bandwidth for that ordering. All items must be separated from their neighbours by exactly one space. If more than one ordering produces the same bandwidth, then choose the smallest in lexicographic ordering, that is the one that would 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
求出排列好后,相连的两个值之间存在的最大值,然后找出最大值最小的那一组
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; int maps[30][30];
int hav[30];
int p[10],a[10];
int ans[10],n,pmax,sum;
int work() //如果相连,求它们之间距离的最大值
{
int tmax = 0;
for(int i=1; i<n; i++)
{
for(int j=i+1; j<n; j++)
{
if(maps[a[i]][a[j]])
{
if(j - i>tmax)
tmax=j-i;
}
}
}
return tmax;
}
void dfs(int cur)
{
int flag;
if(cur==n)
{
sum=work();
if(pmax>sum) //找出最大距离最小的那一组
{
pmax=sum;
memcpy(ans,a,sizeof(a));
}
return ;
}
else
{
for(int i=1; i<n; i++)
{
flag=1;
a[cur]=p[i];
for(int j=1; j<cur; j++)
{
if(a[j]==a[cur])
{
flag=0;
break;
}
}
if(flag)
dfs(cur+1);
}
}
}
int main()
{
char str[100];
char c;
int len,i,pre,now;
while(gets(str)&&strcmp(str,"#"))
{
n=1,pmax = 0x3f3f3f3f;
len=strlen(str);
memset(maps,0,sizeof(maps));
memset(hav,0,sizeof(hav));
memset(p,0,sizeof(p));
for(i=0; i<len; i++)
{
c=str[i];
if(str[i+1]==':')
{
pre=c-'A'+1;
hav[pre]++;
}
else if(c>='A'&&c<='Z')
{
now=c-'A'+1;
hav[now]++;
maps[now][pre]=maps[pre][now]=1;
}
}
for(i=0; i<27; i++)
{
if(hav[i])
p[n++]=i;
}
dfs(1);
for(i=1; i<n; i++)
printf("%c ",ans[i]+'A'-1);
printf("-> %d",pmax);
printf("\n");
}
return 0;
}
UVA140 ——bandwidth(搜索)的更多相关文章
- uva140 - Bandwidth
Bandwidth Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an orderi ...
- UVa140 Bandwidth 小剪枝+双射小技巧+枚举全排列+字符串的小处理
给出一个图,找出其中的最小带宽的排列.具体要求见传送门:UVa140 这题有些小技巧可以简化代码的编写. 本题的实现参考了刘汝佳老师的源码,的确给了我许多启发,感谢刘老师. 思路: 建立双射关系:从字 ...
- Uva140 Bandwidth 全排列+生成测试法+剪枝
参考过仰望高端玩家的小清新的代码... 思路:1.按字典序对输入的字符串抽取字符,id[字母]=编号,id[编号]=字母,形成双射 2.邻接表用两个vector存储,存储相邻关系 ...
- UVa140 Bandwidth 【最优性剪枝】
题目链接:https://vjudge.net/contest/210334#problem/F 转载于:https://www.cnblogs.com/luruiyuan/p/5847706.ht ...
- 递归回溯 UVa140 Bandwidth宽带
本题题意:寻找一个排列,在此排序中,带宽的长度最小(带宽是指:任意一点v与其距离最远的且与v有边相连的顶点与v的距离的最大值),若有多个,按照字典序输出最小的哪一个. 解题思路: 方法一:由于题目说结 ...
- UVA-140 Bandwidth (回溯+剪枝)
题目大意:求一个使带宽最小的排列和最小带宽.带宽是指一个字母到其相邻字母的距离最大值. 题目分析:在递归生成全排列的过程中剪枝,剪枝方案还是两个.一.当前解不如最优解优时,减去:二.预测的理想解不必最 ...
- 7-6 Bandwidth UVA140
没有清空向量导致debug了好久 这题难以下手 不知道怎么dfs 原来是用排序函数. letter[n]=i; id[i]=n++; 用来储存与设置标记十分巧妙 for(;;) { while(s[ ...
- uva 140 bandwidth (好题) ——yhx
Bandwidth Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an orde ...
- UVa OJ 140 - Bandwidth (带宽)
Time limit: 3.000 seconds限时3.000秒 Problem问题 Given a graph (V,E) where V is a set of nodes and E is a ...
随机推荐
- Alpha冲刺Day6
Alpha冲刺Day6 一:站立式会议 今日安排: 由张梨贤继续完成前一天委托第三方剩余的内容,并完成委托情况查看这一子模块 由黄腾飞继续完成前一天企业自查风险管理剩余的内容,并完成风险上报这一子模块 ...
- Scrum 冲刺 第七日
Scrum 冲刺 第七日 站立式会议 燃尽图 今日任务安排 项目发布说明 站立式会议 返回目录 燃尽图 返回目录 今日任务安排 返回目录 项目发布说明 本版本的新功能 不只是简单打地鼠,还有一些不能打 ...
- 201421123042 《Java程序设计》第9周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 2. 书面作业 本次作业题集集合 1. List中指定元素的删除(题集题目) 1.1 实验总结.并回答:列举至 ...
- windows系统下安装 node.js (node.js安装及环境配置)
node.js简介 Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境. Node.js 使用了一个事件驱动.非阻塞式 I/O 的模型,使其轻量又高效. Node. ...
- 火车头采集器对接织梦cms图集发布时, 采集网上图片超时的解决方法
背景介绍: 火车头采集器对接织梦cms图片集发布时, 对于多张(超过30张)大图片时, 经常会出现图集发布超时的情况. 问题分析: 因为php对于资源的处理有默认的超时时间30秒, 而我尝试了好多方 ...
- STL之queue
描述 使用STL中的queue,完成入队.出队.获取队首.获取队尾等基本操作. 部分代码已经给出,请补充完整,提交时请勿包含已经给出的代码. int main() { queue<int> ...
- 在bootstrap中让竖向排列的输入框水平排列
在bootstrap中可以使用自带的样式标记来控制样式,但是同时可以利用最原始的css样式来解决达到需求 如下所示可以看出来两个inline-block就可以使得两个水平排列 block和inline ...
- New UWP Community Toolkit - DropShadowPanel
概述 UWP Community Toolkit 中有一个为 Frmework Element 提供投影效果的控件 - DropShadowPanel,本篇我们结合代码详细讲解 DropShado ...
- python 模块部分补充知识
一.hashlib hashlib 模块主要用于加密相关的操作,代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法. 实例 ...
- MySQL一些中重要命令
前言: 最近在面试的过程中,深感对MySQL一些重要命令的缺失.借着这个机会,补补这块的知识.不让自己只会增删查改,懂一些高级的东西 limit 用法 order by 用法 in 和 between ...