uva 140 宽带

 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.

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 <stdio.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#define N 500
using namespace std;

int vis[N][N],node[N],ans[N],p[N];
char s[N];
int num;
int mmin;

void init()
{
    int f=0;
    for(int i=0;s[i];i++)
    {
        if(isalpha(s[i]))
        {
            if(!node[s[i]-'A'+1])
            {
                node[s[i]-'A'+1]=1;
                num++;
            }

            if(f==0) f=s[i]-'A'+1;
            else
            {
                vis[f][s[i]-'A'+1]=1;
                vis[s[i]-'A'+1][f]=1;
            }
        }
        else if(s[i]==';' && f) f=0;
    }
}

void dfs(int cur,int max)//当前为第几个点以及当前最小带宽
{
    if(cur==num)//全部点都搜索到
    {
        if(mmin>max)//求出各种情况下的最小带宽
        {
            for(int i=0;i<num;i++)
            ans[i]=p[i];
            mmin=max;
        }
        return;
    }

    for(int i=1;i<=26;i++)
    if(node[i])
    {
        for(int j=cur-max-1;j>=0;j--)
        if(vis[i][p[j]])
        max=cur-j;

        if(max>mmin) return;

        node[i]=0;
        p[cur]=i;
        dfs(cur+1,max);
        node[i]=1;
    }
}

int main()
{
    while(scanf("%s",s)&&s[0]!='#')
    {
        memset(node,0,sizeof node);
        memset(vis,0,sizeof vis);

        num=0;
        init();//预处理下各点关系

        mmin=234567899;

        for(int i=1;i<=26;i++)
        if(node[i])
        {
            node[i]=0;
            p[0]=i;
            dfs(1,0);
            node[i]=1;
        }

        for(int i=0;i<num;i++)
        printf("%c ",ans[i]+'A'-1);
        printf("-> %d\n",mmin);

    }
    return 0;
}




你可能感兴趣的:(uva 140 宽带)