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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[2016-02-20][UVA][140][Bandwidth]相关的知识,希望对你有一定的参考价值。

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

UVA - 140
Time Limit: 3000MSMemory Limit: Unknown64bit IO Format: %lld & %llu

 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 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

  • 时间:2016-02-20 16:44:54 星期六
  • 题目编号:UVA 140
  • 题目大意:
    • 给定一个图G(V,E),输出一个节点的序列,使得这个序列是所有序列中,带宽最小的序列(带宽相同时,字典序最小).
    • 带宽:每个节点在该序列中,到相邻节点的距离(从序列上来看)中,最大的那个
  • 分析:枚举所有情况,取最优的解
    • 剪枝优化:枚举过程中,遇到带宽大于等于当前最小带宽的,停止当前的枚举,进入下一个
  • 解题过程遇到问题:
    • 给出来的节点不一定是连续的!!!需要重新定义节点的编号,方便后面枚举
    • 这里的处理方法是,先把图按原字母处理出来,在重新矫正对应的编号  

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef long long LL;
#define CLR(x,y) memset((x),(y),sizeof((x)))
#define FOR(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define FORD(x,y,z) for(int (x)=(y);(x)>=(z);(x)--)
#define FOR2(x,y,z) for((x)=(y);(x)<(z);(x)++)
#define FORD2(x,y,z) for((x)=(y);(x)>=(z);(x)--)
char str[8*10],ch[10];
int G[29][29],vis[28],n,minres,res[10],a[10],g[10][10];
//G保存原图,g保存更正后的图,ch保存出现的节点的字母,a临时保存dfs过程中的顺序,
//res当前最优的顺序,minres当前最优的答案,
void read_in(){
        //清理
        CLR(g,0);CLR(G,0);CLR(vis,0);n = 0;
        char *p = str;
        int u,v,len = strlen(str);
        //后面不上一个 ";‘,方便后面书写
        str[len] = ‘;‘;
        str[len + 1] = ‘\0‘;
        //保存原图,并记录出现的字母
        while(*p){
                u = *p - ‘A‘;
                if(!vis[u]){
                        vis[u] = 1;
                        ch[n++] = *p;
                }
                ++p;++p;
                while(*p != ‘;‘){
                        int v = *p - ‘A‘;
                        if(!vis[v]){
                                vis[v] = 1;
                                ch[n++] = *p;
                        }
                        G[u][v] = G[v][u] = 1;
                        p++;
                }
                p++; 
        }
        sort(ch,ch + n);
        //矫正编号
        FOR(i,0,27){
                FOR(j,0,27){
                        if(G[i][j]){
                                FOR(k,0,n){
                                        if(i == ch[k] - ‘A‘)    u = k;
                                        if(j == ch[k] - ‘A‘)    v = k;
                                }
                                g[u][v] = 1;
                        }
                }
        }
}
int count(int cur){
        int u = a[cur],v;
        FOR(i,0,cur){
                v = a[i];
                if(g[u][v])     return cur - i;
        }
        return 0;
}
void dfs(int cur,int curmax){
        if(cur == n){   
                minres = curmax;
                memcpy(res,a,sizeof(a));
                return ;
        }
        FOR(i,0,n){
                if(!vis[i]){
                        a[cur] = i;
                        vis[i] = 1;
                        int tmp = max(curmax,count(cur));
                        if(tmp < minres)        dfs(cur + 1,tmp);
                        vis[i] = 0;
                }
        }
}
int main(){
        while(gets(str) && str[0] != ‘#‘){
                read_in();
                minres = n + 1;
                CLR(vis,0);
                dfs(0,0);
                FOR(i,0,n)      printf("%c ",ch[res[i]]);
                printf("-> %d\n",minres);    
        }
    return 0;
}  














以上是关于[2016-02-20][UVA][140][Bandwidth]的主要内容,如果未能解决你的问题,请参考以下文章

uva140 Bandwidth

uva 140 bandwidth (好题) ——yhx

假回溯-uva140带宽

Uva 140 Bandwidth

UVa-140 Bandwidth

uva140-暴力枚举