zoukankan      html  css  js  c++  java
  • UVA140 ——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:

     

    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

    求出排列好后,相连的两个值之间存在的最大值,然后找出最大值最小的那一组


    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    
    int maps[30][30];
    int hav[30];
    int p[10],a[10];
    int ans[10],n,pmax,sum;
    int work()        //如果相连,求它们之间距离的最大值
    {
        int tmax = 0;
        for(int i=1; i<n; i++)
        {
            for(int j=i+1; j<n; j++)
            {
                if(maps[a[i]][a[j]])
                {
                    if(j - i>tmax)
                        tmax=j-i;
                }
            }
        }
        return tmax;
    }
    void dfs(int cur)
    {
        int flag;
        if(cur==n)
        {
            sum=work();
            if(pmax>sum)      //找出最大距离最小的那一组
            {
                pmax=sum;
                memcpy(ans,a,sizeof(a));
            }
            return ;
        }
        else
        {
            for(int i=1; i<n; i++)
            {
                flag=1;
                a[cur]=p[i];
                for(int j=1; j<cur; j++)
                {
                    if(a[j]==a[cur])
                    {
                        flag=0;
                        break;
                    }
                }
                if(flag)
                    dfs(cur+1);
            }
        }
    }
    int main()
    {
        char str[100];
        char c;
        int len,i,pre,now;
        while(gets(str)&&strcmp(str,"#"))
        {
            n=1,pmax = 0x3f3f3f3f;
            len=strlen(str);
            memset(maps,0,sizeof(maps));
            memset(hav,0,sizeof(hav));
            memset(p,0,sizeof(p));
            for(i=0; i<len; i++)
            {
                c=str[i];
                if(str[i+1]==':')
                {
                    pre=c-'A'+1;
                    hav[pre]++;
                }
                else if(c>='A'&&c<='Z')
                {
                    now=c-'A'+1;
                    hav[now]++;
                    maps[now][pre]=maps[pre][now]=1;
                }
            }
            for(i=0; i<27; i++)
            {
                if(hav[i])
                    p[n++]=i;
            }
            dfs(1);
            for(i=1; i<n; i++)
                printf("%c ",ans[i]+'A'-1);
            printf("-> %d",pmax);
            printf("
    ");
        }
        return 0;
    }
    

      

  • 相关阅读:
    linux下清空文件的几种方式以及对比
    远程桌面连接无法验证您希望连接的计算机的身份-mac连接远程桌面
    Linux配置临时IP和网关命令
    linux(centos、ubuntu)网卡配置文件不生效
    负载均衡
    Zookeeper基础使用机制原理
    高性能RPC框架选型
    事务隔离机制
    一致性协议Raft
    机器学习入门
  • 原文地址:https://www.cnblogs.com/Przz/p/5409805.html
Copyright © 2011-2022 走看看