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的更多相关文章

  1. UVa 140 Bandwidth【枚举排列】

    题意:给出n个节点的图,和一个节点的排列,定义节点i的带宽b[i]为i和其相邻节点在排列中的最远的距离,所有的b[i]的最大值为这个图的带宽,给一个图,求出带宽最小的节点排列 看的紫书,紫书上说得很详 ...

  2. UVA 140 Bandwidth

    题意: 给出一个n个节点的图G,和一个节点的排列,定义节点i的带宽为i和相邻节点在排列中的最远距离,而所有带宽的最大值就是图的带宽,求让图的带宽最小的排列. 分析: 列出所有可能的排列,记录当前找到的 ...

  3. UVA - 140 Bandwidth(带宽)(全排列)

    题意:给定图,求是带宽最小的结点排列. 分析:结点数最多为8,全排列即可.顶点范围是A~Z. #pragma comment(linker, "/STACK:102400000, 10240 ...

  4. UVA 140 Bandwidth (dfs 剪枝 映射)

    题意: 给定一个n个结点的图G和一个结点的排列, 定义结点i的带宽b(i)为i和相邻结点在排列中的最远距离, 所有b(i)的最大值就是这个图的带宽, 给定G, 求让带宽最小的结点排列. 给定的图 n ...

  5. UVa 489 HangmanJudge --- 水题

    UVa 489 题目大意:计算机给定一个单词让你猜,你猜一个字母,若单词中存在你猜测的字母,则会显示出来,否则算出错, 你最多只能出错7次(第6次错还能继续猜,第7次错就算你失败),另注意猜一个已经猜 ...

  6. UVa 1585 Score --- 水题

    题目大意:给出一个由O和X组成的串(长度为1-80),统计得分. 每个O的分数为目前连续出现的O的个数,例如,OOXXOXXOOO的得分为1+2+0+0+1+0+0+1+2+3 解题思路:用一个变量t ...

  7. 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 ...

  8. UVa 140 (枚举排列) Bandwidth

    题意较复杂,请参见原题=_=|| 没什么好说的,直接枚举每个排列就好了,然后记录最小带宽,以及对应的最佳排列. STL里的next_permutation函数真是好用. 比较蛋疼的就是题目的输入了.. ...

  9. 【例题 7-6 UVA - 140】Bandwidth

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 暴力做就好. O(8!*26^2) [代码] /* 1.Shoud it use long long ? 2.Have you ev ...

随机推荐

  1. SQL转换全角/半角函数

    /****** SQL转换全角/半角函数 开始******/ CREATE FUNCTION ConvertWordAngle ( ), --要转换的字符串 @flag bit --转换标志,0转换成 ...

  2. MVC调试时遇到的URL问题

    最近接手一个项目时遇到了点问题,事情是这样的,本人拿到源码准备F5准备试跑看一下,原本是应该打开 http:/localhost/Home/Login,结果程序直接跳到 http://localhos ...

  3. FASTSOCKET

    FASTSOCKET It looks like there are like 3 separate optimizations, but I think the most important one ...

  4. Java基本概念(1)什么是Java

    什么是Java Java是一种开发语言(核心特点:跨平台,面向对象,名称由来看这里:J2EE里面的2是什么意思),对于开发者来讲,Java基本等于Jdk. Jdk的版本介绍看这里:Java都有那些版本 ...

  5. Hibernate —— 概述与 HelloWorld

    一.Hibernate 概述 1.Hibernate 是一个持久化框架 (1)从狭义的角度来讲,“持久化” 仅仅指把内存中的对象永久的保存到硬盘中的数据库中. (2)从广义的角度来讲,“持久化” 包括 ...

  6. php中防止SQL注入的方法

    [一.在服务器端配置] 安全,PHP代码编写是一方面,PHP的配置更是非常关键. 我们php手手工安装的,php的默认配置文件在 /usr/local/apache2/conf/php.ini,我们最 ...

  7. 一个有趣的CM

    系统 : Windows xp 程序 : Crackme#3 - Self Destructed 程序下载地址 :http://pan.baidu.com/s/1kVxwlaZ 要求 : 注册机编写 ...

  8. scrollview 图片放大 捏合 瓦片地图 相关注意事项

    就职文博公司要为博物馆做APP 涉及到瓦片地图的编写 在这里总结一些开发中遇到的问题 (将会不断更新 也是学习阶段) 着急写项目的同学 可以直接看code4上现成的瓦片地图代码:http://www. ...

  9. iOS通知中心升级 -可设置按优先级执行block

    简单介绍下,这是需求驱动中发现iOS的NotificationCenter有很多功能无法实现,于是对其进行了一层包装.相当于手动管理观察者栈和监听者期望执行的事件,因此可以为其添加了很多新增的功能,将 ...

  10. WWDC 后苹果最新 App Store 审核条款!

        WWDC 2016 大会之后,苹果公司发布了四个全新平台:iOS,macOS,watchOS 和 tvOS.并且在此之后,苹果应用商店审核条款也同时进行了更新——貌似不算进行了更新,简直就是重 ...