欢迎使用CSDN-markdown编辑器

来源:互联网 发布:类似于完美世界 知乎 编辑:程序博客网 时间:2024/06/06 17:48

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 rangeA’ 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 <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <vector>#include <cmath>using namespace std;const int maxn=10;char input[1000+10],letter[maxn];int main(){    while(scanf("%s",input)==1&&input[0]!='#')    {        int len=strlen(input),n=0;        int id[256];        memset(id,0,sizeof(id));        for(char i='A';i<='Z';i++)        {            if(strchr(input,i)!=NULL)            {                id[i]=n++;                letter[id[i]]=i;            }        }        vector<int>u,v;        int p=0,q=0;        for(;;)        {            while(input[p]!=':'&&p<len)p++;            if(p==len)break;            while(input[q]!=';'&&q<len)q++;            for(int i=p+1;i<q;i++)            {                u.push_back(id[input[p-1]]);                v.push_back(id[input[i]]);            }            p++;q++;        }        int P[maxn],bestP[maxn],pos[maxn],ans=n;        for(int i=0;i<n;i++)            P[i]=i;        do{            for(int i=0;i<n;i++)                pos[P[i]]=i;            int bandwdith=0;            for(int i=0;i<u.size();i++)//            {                bandwdith=max(bandwdith,abs(pos[u[i]]-pos[v[i]]));            }            if(bandwdith<ans)            {                for(int i=0;i<n;i++)                    bestP[i]=P[i];                ans=bandwdith;            }        }while(next_permutation(P,P+n));        for(int i=0;i<n;i++)            printf("%c ",letter[bestP[i]]);        printf("-> %d\n",ans);    }    return 0;}

附译文:
带宽
给定一个图(V,E)V是一组节点和E是一组在VxV弧,和第五元素的排序,然后定义节点的带宽V V之间的最大距离排序和任何节点连接图中。订购的带宽就定义为个人的最大带宽。例如,考虑下面的图:
这可以下令在很多方面,其中两个是所示:
对于这些序,节点的带宽(为了)6、6、1、4、1、1、6、6给一个订购6的带宽,和5、3、1、4、3、5、1、4给一个订购5带宽。
编写一个程序,会发现的顺序图最小带宽。
输入
输入将包含一系列的图表。每个图都将出现在一行本身。整个文件将终止一条线组成的一个#。对于每一个图,输入将包含一系列的记录用“;”隔开。每个记录将由一个节点名(一个大写字符“a”范围的“Z”),后跟一个“:”和至少一个邻国。图将包含不超过8节点。
输出
为每个图形输出将包含一行,清单的顺序节点紧随其后的是一个箭头(- >)和带宽要求。所有项目必须分开他们的邻居刚好由一个空间。如果不止一个排序产生相同的带宽,然后在词典排序,选择最小的,是将第一次出现在一个字母清单。
样例输入

0 0
原创粉丝点击