UVA - 140 Bandwidth: 暴力 回溯

题目点此跳转

Given a graph (V, E) where V is a set of nodes and E is a set of arcs in V ×V , 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 graph on the right:
这里写图片描述
 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

思路

 题目意思是给出一个n(n≤8)个结点的图G和一个结点的排列,定义结点i的带宽b(i)为i和相邻结点在排列中的最远距离,而所有b(i)的最大值就是整个图的带宽。给定图G,求出让带宽最小的结点排列。

 最多8个顶点,直接暴力回溯即可。
 使用一个pos数组记录每个顶点所放的位置,递归到终点时根据pos数组维护带宽即可。
 考虑两个剪枝(代码里没有给出,也用了0ms过了):一个是将当前两个结点的距离与已找到的最小带宽k比较,或此距离大于等于k,则剪掉;另一个是当搜索到u点时, 如果u后面还有m个结点,但是m>k,此时最好的情况是m,应该剪掉。

代码

#include <algorithm>
#include <iostream>
#include <sstream>
#include <utility>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <cstring>
#include <cstdio>
#include <cmath>
#define met(a,b) memset(a, b, sizeof(a));
#define IN freopen("in.txt", "r", stdin);
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int maxn = 3e1;
const int INF = 0x7fffffff;

string s, str;
char c, d;
vector<int> m[maxn];
bool vis[maxn], v[maxn];
int num, ans[maxn], aid, res, pos[maxn], Ot[maxn];

void init() {
    for(int i = 0; i < 26; ++i) m[i].clear();
    met(vis, 0); met(v, 0); met(pos, 0);
    num = 0; aid = 0; res = INF;
}

void dfs(int x, int deep) {
    if(deep == num) {
        int ret = 0;
        for(int i = 0; i < 26; ++i) if(vis[i]) {
            for(int j = 0; j < m[i].size(); ++j) {
                ret = max(ret, abs(pos[m[i][j]] - pos[i]));
                if(ret >= res) return;
            }
        }
        if(res > ret) {
            res = ret;
            memcpy(Ot, ans, sizeof(ans));
        }
        return;
    }
    for(int i = 0; i < 26; ++i) if(vis[i] && !v[i]){
        v[i] = 1; ans[aid++] = i; pos[i] = deep+1;
        dfs(i, deep+1); v[i] = 0; --aid; pos[i] = 0;
    }

}

int main() {
    #ifdef _LOCAL
    IN;
    #endif // _LOCAL

    while(getline(cin, s)) {
        if(s[0] == '#') break; init();
        for(int i = 0; i < s.length(); ++i) {
            if(s[i] == ';') s[i] = ' ';
        }
        stringstream ss(s);
        while(ss>>c>>d>>str) {
            int u = c-'A';
            if(!vis[u]) vis[u] = 1, ++num;
            for(int i = 0; i < str.length(); ++i) {
                int v = str[i]-'A'; if(!vis[v]) vis[v] = 1, ++num;
                m[u].push_back(v); m[v].push_back(u);
            }
        }
        for(int i = 0; i < 26; ++i) if(vis[i] && !v[i]){
            v[i] = 1; ans[aid++] = i; pos[i] = 1;
            dfs(i, 1); v[i] = 0; --aid; pos[i] = 0;
        }
        for(int i = 0; i < num; ++i) printf("%c ", Ot[i]+'A');
        printf("-> %d\n", res);
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值