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 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.
#include<cstdio>
#include<cstring>
bool use[],app[];
int map[][],n,a[],ans,ans_arr[],alfa[];
void dfs(int fin,int cur)
{
int i,j,k,p,q,x,y,z,cnt,temp;
char c1,c2;
if (cur>=ans) return;
cnt=;
for (i=;i<=n;i++)
if (map[alfa[a[fin]]][alfa[i]]&&!use[i]) cnt++;
if (cnt>=ans) return;
if (fin==n)
{
ans=cur;
for (i=;i<=n;i++)
ans_arr[i]=a[i];
return;
}
fin++;
for (i=;i<=n;i++)
if (!use[i])
{
a[fin]=i;
temp=cur;
use[i]=;
for (j=;j<fin-cur;j++)
if (map[alfa[i]][alfa[a[j]]])
{
temp=fin-j;
break;
}
dfs(fin,temp);
use[i]=;
}
}
int main()
{
char s[],c;
int i,x,y;
while (scanf("%s",s)&&s[]!='#')
{
memset(map,,sizeof(map));
memset(use,,sizeof(use));
memset(a,,sizeof(a));
memset(alfa,,sizeof(alfa));
memset(app,,sizeof(app));
n=;
for (i=;i<strlen(s);)
{
c=s[i++];
x=c-'A'+;
app[x]=;
i++;
while (s[i]!=';'&&i<strlen(s))
{
y=s[i]-'A'+;
app[y]=;
map[x][y]=map[y][x]=;
i++;
}
i++;
}
for (i=;i<=;i++)
if (app[i])
alfa[++n]=i;
ans=0x3f3f3f3f;
dfs(,);
for (i=;i<=n;i++)
printf("%c ",alfa[ans_arr[i]]+'A'-);
printf("-> %d\n",ans);
}
}
搜索+剪枝。
1.目前带宽大于等于已知答案,剪枝。
2.在搜索到某一节点时,与该节点连接的还没有加入排列的点的个数大于等于已知答案,剪枝。(若这些点全都紧跟在此点之后,带宽也为其个数。否则更大。)
读入数据处理的时候注意,要让字母序小的排在前头。
注意各种下标、字母、数字、位置的变量引用。
uva 140 bandwidth (好题) ——yhx的更多相关文章
- UVa 140 Bandwidth【枚举排列】
题意:给出n个节点的图,和一个节点的排列,定义节点i的带宽b[i]为i和其相邻节点在排列中的最远的距离,所有的b[i]的最大值为这个图的带宽,给一个图,求出带宽最小的节点排列 看的紫书,紫书上说得很详 ...
- UVA 140 Bandwidth
题意: 给出一个n个节点的图G,和一个节点的排列,定义节点i的带宽为i和相邻节点在排列中的最远距离,而所有带宽的最大值就是图的带宽,求让图的带宽最小的排列. 分析: 列出所有可能的排列,记录当前找到的 ...
- UVA - 140 Bandwidth(带宽)(全排列)
题意:给定图,求是带宽最小的结点排列. 分析:结点数最多为8,全排列即可.顶点范围是A~Z. #pragma comment(linker, "/STACK:102400000, 10240 ...
- UVA 140 Bandwidth (dfs 剪枝 映射)
题意: 给定一个n个结点的图G和一个结点的排列, 定义结点i的带宽b(i)为i和相邻结点在排列中的最远距离, 所有b(i)的最大值就是这个图的带宽, 给定G, 求让带宽最小的结点排列. 给定的图 n ...
- UVa 489 HangmanJudge --- 水题
UVa 489 题目大意:计算机给定一个单词让你猜,你猜一个字母,若单词中存在你猜测的字母,则会显示出来,否则算出错, 你最多只能出错7次(第6次错还能继续猜,第7次错就算你失败),另注意猜一个已经猜 ...
- UVa 1585 Score --- 水题
题目大意:给出一个由O和X组成的串(长度为1-80),统计得分. 每个O的分数为目前连续出现的O的个数,例如,OOXXOXXOOO的得分为1+2+0+0+1+0+0+1+2+3 解题思路:用一个变量t ...
- 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 ...
- UVa 140 (枚举排列) Bandwidth
题意较复杂,请参见原题=_=|| 没什么好说的,直接枚举每个排列就好了,然后记录最小带宽,以及对应的最佳排列. STL里的next_permutation函数真是好用. 比较蛋疼的就是题目的输入了.. ...
- 【例题 7-6 UVA - 140】Bandwidth
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 暴力做就好. O(8!*26^2) [代码] /* 1.Shoud it use long long ? 2.Have you ev ...
随机推荐
- C# 之 DataReader 和 DataSet 的区别
本文转载自:http://www.cnblogs.com/xinaixia/p/4920630.html 1. 获取数据的方式[1]DataReader 为在线操作数据, DataReader会一直占 ...
- .Net(c#)模拟Http请求之HttpWebRequest封装
一.需求: 向某个服务发起请求获取数据,如:爬虫,采集. 二.步骤(HttpWebRequest): 无非在客户端Client(即程序)设置请求报文(如:Method,Content-Type,Age ...
- jquery制作论坛或社交网站的每天打卡签到特效
效果:http://hovertree.com/texiao/jquery/50/ 现在许多社区,购物等网站都设置签到功能,打开可以收获经验.虚拟币等,提高用户粘性,增加浏览量,是一个不错的功能.本文 ...
- 组合模式 - Composite
Composite Pattern,将对象组合成树形结构以表示’部分-整体’的层次关系,用户对单对象和组合部件的使用具有一致性. 实现方式: 透明方式:接口统一: 安全方式:不统一: 参考:
- centos下安装php扩展php-memcached
说来坎坷,为了安装这个php的扩展php-memcached,连操作系统都换了,从centos5.5升级到了centos6.8!! centos5.5中在安装php扩展php-memcached的依赖 ...
- 走进SVG
什么是SVG?也许现在很多人都听说过SVG的人比较多,但不一定了解什么是SVG:SVG(Scalable Vector Graphics 一大串看不懂的英文)可伸缩矢量图形,它是用XML格式来定义用于 ...
- 2-1 git合并 打tag
tag常用命令(仅供参考): git tag -a V1.4 -m "1.4" 创建一个tag git tag 查看本地tag git tag -d V1.1 删除本地tag g ...
- OC笔记一:Objective-C简介
1.OC简介 全称:Objective-C,是扩充C的面向对象编程语言,主要用于iOS和Mac OS开发. C语言的基础上,增加了一层最小的面向对象语法 完全兼容C语言 可以在OC代码中混入C语言代码 ...
- 用MSF进行提权
在WEB渗透中当我们拿到webshell了,我们可以试试用MSF(metasploit)来进行提权,在MSF里meterpreter很强大的! 我们先用msfvenom生成一个EXE的木马后门. ms ...
- 设置statusBarStyle
设置状态栏的样式, typedef NS_ENUM(NSInteger, UIStatusBarStyle) { UIStatusBarStyleDefault ...