Description

 

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

A:FB;B:GC;D:GC;F:AGH;E:HD
#
A B C F G D H E -> 3

这道题没什么特别的,深搜里多加个条件,即有从此路到彼路的路
#include"iostream"
#include"cstring"
#include"cstdio"
#include"map"
#include"algorithm"
using namespace std; map<char,map<char,int> >m; int len,w,ans;
int book[100];
int a[100];
int an[100]; int count(char *p)
{
int j=0;
for(char i='A';i<='Z';i++)
if(strchr(p,i))
{ j++; } return j;
} void DFS(int step, int bw)
{
if(step==len)
{
ans=bw;
memcpy(an,a,100);
return;
} for(int i=0;i<len;i++)
{
if(!book[i])
{
book[i]=1;
a[step]=i;
int w=0;
for(int j=0;j<step;j++)
{
if(m[i+'A'][a[j]+'A'])
{
w=step-j;
break;
}
}
int ibw=max(bw, w);
if(ibw<ans)
DFS(step+1, ibw);
book[i]=0;
}
}
}
int main()
{
char ab[300];
while(gets(ab) && ab[0]!='#')
{
len=count(ab);
char*p=strtok(ab, ";");
while(p)
{
int t=p[0];
++p;
while(*(++p))
{
m[t][*p]=1;
m[*p][t]=1;
}
p=strtok(0, ";");
} ans=len;
DFS(0, 0); for(int i=0;i<len;i++)
{
printf("%c ", an[i]+'A');
}
printf("-> %d\n", ans);
} return 0;
}

第二周习题O题的更多相关文章

  1. oracle直通车第二周习题

    1.教材第二章课后作业 1,2,3,4题. 答:1. 创建一查询,显示与Blake在同一部门工作的雇员的项目和受雇日期,但是Blake不包含在内. 2. 显示位置在Dallas的部门内的雇员姓名.变化 ...

  2. 第二周习题F

    Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multiplications: x2  ...

  3. 面向对象程序设计--Java语言第二周编程题:有秒计时的数字时钟

    有秒计时的数字时钟 题目内容: 这一周的编程题是需要你在课程所给的时钟程序的基础上修改而成.但是我们并不直接给你时钟程序的代码,请根据视频自己输入时钟程序的Display和Clock类的代码,然后来做 ...

  4. 20145213《Java程序设计》第二周学习总结

    20145213<Java程序设计>第二周学习总结 教材学习内容总结 本周娄老师给的任务是学习教材的第三章--基础语法.其实我觉得还蛮轻松的,因为在翻开厚重的书本,一股熟悉的气息扑面而来, ...

  5. 20155303 2016-2017-2 《Java程序设计》第二周学习总结

    20155303 2016-2017-2 <Java程序设计>第二周学习总结 教材学习内容总结 『注意』 "//"为单行批注符: "/*"与&quo ...

  6. 20172328 2018—2019《Java软件结构与数据结构》第二周学习总结

    20172328 2018-2019<Java软件结构与数据结构>第二周学习总结 概述 Generalization 本周学习了第三章集合概述--栈和第四章链式结构--栈.主要讨论了集合以 ...

  7. 2018-2019-3《Java程序设计》第二周学习总结

    学号20175329 2018-2019-3<Java程序设计>第二周学习总结 教材学习内容总结      第二三章与我们所学习的C语言有很多的相似点,在这里我想主要就以我所学习的效果来讨 ...

  8. # 20175329 2018-2019-2 《Java程序设计》 第二周学习总结

    学号 2018-2019-3<Java程序设计>第二周学习总结 教材学习内容总结      第二三章与我们所学习的C语言有很多的相似点,在这里我想主要就以我所学习的效果来讨论一下JAVA与 ...

  9. # 20175329 2018-2019-2 《Java程序设计》第二周学习总结

    # 学号 2018-2019-3<Java程序设计>第三周学习总结 ## 教材学习内容总结 第二三章与我们所学习的C语言有很多的相似点,在这里我想主要就以我所学习的效果来讨论一下JAVA与 ...

随机推荐

  1. [浏览器美化]去除 Firefox 当前选中标签页顶端的线条

    Firefox 当前选中的标签页的最上方会显示有一条线,我觉得很丑,如图: 在地址栏输入 about:support ,然后点击打开目录. 在此目录中创建一个 chrome 文件夹(若没有),然后进入 ...

  2. Oracle异机恢复

    RMAN异机恢复注意事项:1.RMAN 异机恢复的时候,db_name必须相同. 如果说要想改成其他的实例名,可以在恢复成功后,用nid 命令修改. 实例名的信息会记录到控制文件里,所以如果在恢复的时 ...

  3. hihoOffer收割练习20题目2

    题目2 : SCI表示法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 每一个正整数 N 都能表示成若干个连续正整数的和,例如10可以表示成1+2+3+4,15可以表示 ...

  4. time模块,datetime模块

    time模块 time模块是包含各方面对时间操作的函数. 尽管这些常常有效但不是所有方法在任意平台中有效. 时间相关的操作,时间有三种表示方式: 时间戳               1970年1月1日 ...

  5. 414 Third Maximum Number 第三大的数

    给定一个非空数组,返回此数组中第三大的数.如果不存在,则返回数组中最大的数.要求算法时间复杂度必须是O(n).示例 1:输入: [3, 2, 1]输出: 1解释: 第三大的数是 1.示例 2:输入: ...

  6. 归并排序算法及其JS实现

    归并排序(MERGE-SORT)是利用归并的思想实现的排序方法,该算法采用经典的分治(divide-and-conquer)策略(分治法将问题分(divide)成一些小的问题然后递归求解,而治(con ...

  7. Python学习 Day 4 函数 切片 迭代 列表生成式 生成器

    定义函数 def my_abs(x):#求绝对值的my_abs函数 if x >= 0: return x else: return –x def nop():#空函数 pass#占位符 参数检 ...

  8. SQL Server中行列转置方法

    PIVOT用于将列值旋转为列名(即行转列),在SQL Server 2000可以用聚合函数配合CASE语句实现 PIVOT的一般语法是:PIVOT(聚合函数(列) FOR 列 in (…) )AS P ...

  9. .Net Mvc EasyUI DataGrid 分页

    由于项目的需要,最近一直在学习 .net MVC 和EasyUI.上周写了一个<.Net Mvc 返回Json,动态生成EasyUI Tree>,今天再写一个EasyUI中另一个重要的组件 ...

  10. (二)Redis for 阿里云公网连接

    目录 (一)Redis for Windows正确打开方式 (二)Redis for 阿里云公网连接 (三)Redis for StackExchange.Redis 阿里云目前仅支持内网连接Redi ...