首页 > 代码库 > Bandwidth

Bandwidth

Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and anordering 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:

picture25

This can be ordered in many ways, two of which are illustrated below:

picture47

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.

Sample input

A:FB;B:GC;D:GC;F:AGH;E:HD
#

Sample output

A B C F G D H E -> 3
题意:全排列的一道题。题中给出每一个点相邻的点;A相邻得点就是F、B;然后每一次全排列后。找出相邻点的距离,选出一个最大值;然后在这些最大值中找出一个最小值;
# include <cstdio>
# include <vector>
# include <cstring>
# include <algorithm>
# include <iostream>
using namespace std;

int id[256],letter[30];

int main()
{
    char c[1000];
    while(scanf("%s",c)!=EOF&&c[0]!='#')
    {
        int i,j,p,q,n=0;
        for(char x='A';x<='Z';x++)
            if(strchr(c,x)!=NULL)
            {
                id[x] = n++;    //给字母编号;
                letter[id[x]] = x;    //储存编号所对应的字母;
            }
        int len=strlen(c);
        p=q=0;
        vector < int > u,v;
        for(;;)     //将对应关系用两个数组表示;
        {
            while(p<len&&c[p]!=':')    p++;
            if(p==len)    break;
            while(q<len&&c[q]!=';')    q++;
            for(i=p+1;i<q;i++)
            {
                u.push_back(id[c[p-1]]);
                v.push_back(id[c[i]]);
            }
            p++;q++;
        }
        int pos[10],P[10],bestp[10],ans=n;

        for(i = 0; i < n; i++) P[i] = i;   //储存字母下标;
        do
        {
            for(i = 0; i < n; i++) pos[P[i]] = i; // 每个字母的位置

            int bandwidth=0;
            for(i=0;i<u.size();i++)    //求出最长距离;
                bandwidth=max(bandwidth,abs(pos[u[i]]-pos[v[i]]));
            if(bandwidth<ans)
            {
                ans=bandwidth;
                memcpy(bestp,P,sizeof(P));
            }
        }
        while(next_permutation(P,P+n));//全排列;调用函数;可以自动全排列;

        for(i = 0; i < n; i++) printf("%c ", letter[bestp[i]]);
            printf("-> %d\n", ans);
    }
    return 0;
}

Bandwidth