第二周习题O题

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;
}
时间: 2024-07-31 04:08:14

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

第二周习题F

Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multiplications: x2 = xxx,     x3 = x2xx,     x4 = x3xx,     ...  ,     x31 = x30xx. The operation of squaring can appreciably shorten the sequence of multiplications. Th

20165232 第二周学习总结

20165232 第二周学习总结 1:带包的代码如何编译运行 代码编写完毕后,先Javac编译,再用mkdir以打包文件的文件名创建一个新文件夹,之后将编写的字节码文件用co命令拷贝到新的文件夹中,最后再运行. 2:一些知识点总结 第三章基础语法 3.1 类型.变量与运算符 类型 基本类型:整数(short.int.long).字节(byte).浮点数(float.double).字符(char).布尔(boolean) 常用格式控制符号 运算符 ==表示相等,=是指定运算,%运算结果是除法后的

20172330 2018-2019-1 《程序设计与数据结构》第二周学习总结

20172330 2018-2019-1 <程序设计与数据结构>第二周学习总结 教材学习内容总结 集合 1.集合是一种对象,类似于保存其他对象的存储库 集合中的元素通常是按照他们添加到集合的顺序,或者是按元素之间的某种内在联系来组织的. 2.抽象数据类型(ADT)是由数据和在该数据上所实施的具体操作构成的集合. 3.栈是一种线性集合,元素按后进先出(LIFO)的方法进行处理,有push,pop,peek,isempty,size等常用操作 4.栈是用于计算后缀表达式的理想数据结构 5.Java

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

学号 2018-2019-3<Java程序设计>第三周学习总结 教材学习内容总结? ? ? 第二三章与我们所学习的C语言有很多的相似点,在这里我想主要就以我所学习的效果来讨论一下JAVA与C语言的不同之处? ? ? ·数组使用方式不同? ? ? ? ? ? ? ? 在C语言中设置数组需要在设置变量后加上数组的容量,但是在JAVA中不能添加容量因为在JAVA中数组作为动态变量其大小可以变化? ? ? ·for语句? ? ? ? ? ? ? ? 可以定义变量类型并且在之前的学习中也没有见到过for

20155336 2016-2017-2《JAVA程序设计》第二周学习总结

20155336 2016-2017-2 <JAVA 程序设计>第二周学习总结 教材学习内容 1: GIT版本检测 2: JAVA中基本类型 整数 字节 浮点数 字符 布尔(▲) 通过API可以得知各个类型可存储的数值范围 public class Range {public static void main(String[] args){ //byte.short.int.long的范围 System.out.printf("%d~%d%n", Byte.MIN_VALU

解题报告——2018级2016第二学期第二周作业

解题报告——2018级2016第二学期第二周作业 D:迷宫问题 题目描述: 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, }; 它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线. 输入 一个5 × 5的二维数组,表示一个迷宫.数据保证有唯一解. 输出 左上角到右

20145326《Java程序设计》第二周学习总结

20145326<Java程序设计>第二周学习总结 教材学习内容总结 本周学习教材第三章,本章主要讲述了java语言中的一些基础语法,java是个支持面向对象的程序语言,但在正式进入面向对象支持语法的探讨前,对于类型.变量.运算符.流程控制等,这些各种程序语言都会有的基本语法元素,还是要有一定的基础.虽然各种程序语言都有这些基本语法元素,但千万别因此而轻忽它们,因为各种程序语言都有其诞生的宗旨与演化过程,对这些基本语法元素,也就会有其独有的特性. 1. 类型 在java的世界中,并非每个东西都

暑假第二周总结

第二周.比赛外时间较上周有了一些进步,不过比赛状态却大不如前. 打了三场个人赛.卡题问题比较严重(尤其round 4最简单的B题卡了4个小时)加上之前的三场总排名应该是第一名.不过罚时仍然是居高不下占据第一.. 组队赛打了两场.一场rank1,一场垫底2333. 由于组内分工,我发现自己对孙子定理.高斯消元等基本数学问题与线段树等基本数据结构变得生疏.好像有两年没写线段树了.. 难题仍然有好多不会,不过比赛之后也在思考,也学到了一些新的东西.我发现如果每次比赛只做会做的题目,只能锻炼思维能力和对

201671010103 2016-2017-2 《Java程序设计》第二周学习心得

时间真的很快,第二周又过去了,但Java第三章的知识点并没有完全掌握. 在周四老师讲题之前,我很成功地把c语言和Java完全混淆了,在做题时,看到选项根本就不能确定对错,比如:Java区分大小写,如果大小写发生错误,程序根本就运行不出来:Java中的标识符包括美元符号但不能使用Java保留字作为变量名:数据类型中整型还包括了字节型的byte等,这都和c语言有很大差别. 其实归根结底,自己还是不太适应翻转课堂这种教学方法,因为平时各种作业本来就多,想要在上课之前认真去学习整整一章的内容确实有些困难