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

题意:全排列的一道题。题中给出每一个点相邻的点;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;
}
时间: 2024-10-10 06:04:24

Bandwidth的相关文章

Uva140 Bandwidth 全排列+生成测试法+剪枝

参考过仰望高端玩家的小清新的代码... 思路:1.按字典序对输入的字符串抽取字符,id[字母]=编号,id[编号]=字母,形成双射       2.邻接表用两个vector存储,存储相邻关系       3.先尝试字母编号字典序最小的排列,此为next_permutation的最上排列       4.在最理想的情况下都不能得到比当前最优解更好的方案,则应当剪枝(prune)       5.memcpy(),strchr()方法来自于库函数 测试集: Input:      A:FB;B:GC

light oj 1153 - Internet Bandwidth【网络流无向图】

1153 - Internet Bandwidth   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB On the Internet, machines (nodes) are richly interconnected, and many paths may exist between a given pair of nodes. The total message-carrying cap

UVA820 Internet Bandwidth

很裸的模版题,就是敲起来稍微麻烦一点. #include<bits/stdc++.h> using namespace std; struct Edge { int v,cap; }; const int maxn = 101; vector<Edge> E; vector<int> G[maxn]; #define PB push_back void AddEdge(int u,int v,int c) { G[u].PB(E.size()); E.PB({v,c})

【转】什么是对分带宽/对半带宽 (bisection bandwidth)

转自 http://blog.sina.com.cn/s/blog_8333a3030101dh2p.html 1. 定义: 用一截面将网络划分成对等的两半时(或者两个结点数目都相同的子网)时,穿过该截面的最大传输率. 对分带宽越大,网络的通信能力越强. 2. 计算: 如果每条链路的带宽都已知道的情况下,则以链路带宽计算.否则,假设链路带宽为1. 下面的计算都是假设链路带宽为1. 1) n个结点的全连接网络(Fully interconnected network): 每个子网的结点数目为n/2

uva 140 bandwidth (好题) ——yhx

 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 con

[2016-02-20][UVA][140][Bandwidth]

UVA - 140 Bandwidth Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status 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 

UVa 140 (枚举排列) Bandwidth

题意较复杂,请参见原题=_=|| 没什么好说的,直接枚举每个排列就好了,然后记录最小带宽,以及对应的最佳排列. STL里的next_permutation函数真是好用. 比较蛋疼的就是题目的输入了.. 还有就是不明白,为什么19.20行注释哪错了?? 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int maxn = 10; 5 6 int id[256], letter[maxn]; 7 char in[1000];

Propagation of Visual Entity Properties Under Bandwidth Constraints

1. Introduction The Saga of Ryzom is a persistent massively-multiplayer online game (MMORPG) released in September 2004 throughout Europe and North America, localised in 3 languages so far. It has been developed by Nevrax since 2000, and was taken ov

Bandwidth内存带宽測试工具

本博文为原创,遵循CC3.0协议,转载请注明出处:http://blog.csdn.net/lux_veritas/article/details/24766015 -----------------------------------------------------------------------------------------------------------------------------------------------------------------------