首页 > 代码库 > UVA - 140 Bandwidth(带宽)(全排列)

UVA - 140 Bandwidth(带宽)(全排列)

题意:给定图,求是带宽最小的结点排列。

分析:结点数最多为8,全排列即可。顶点范围是A~Z。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int MAXN = 100 + 10;
const int MAXT = 10000 + 10;
using namespace std;
char s[MAXN];
set<int> node[30];
vector<int> v;
int t[10];
void init(){
    for(int i = 0; i < 30; ++i){
        node[i].clear();
    }
    v.clear();
    memset(t, 0, sizeof t);
    int len = strlen(s);
    for(int i = 0; i < len; ++i){
        if(s[i] == :){
            int tmp = s[i - 1] - A;
            while(1){
                ++i;
                if(i == len) break;
                if(s[i] == ;) break;
                node[tmp].insert(s[i] - A);
                node[s[i] - A].insert(tmp);
            }
        }
    }
}
int main(){
    while(scanf("%s", s) == 1){
        if(s[0] == #) return 0;
        init();
        for(int i = 0; i < 30; ++i){
            if(node[i].size()){
                v.push_back(i);
            }
        }
        int len = v.size();
        int ans = INT_M_INF;
        do{
            int tmp = 0;
            for(int i = 0; i < len; ++i){
                for(int j = 0; j < i; ++j){
                    if(node[v[i]].count(v[j])){
                        tmp = max(tmp, i - j);
                    }
                }
            }
            if(tmp < ans){
                ans = tmp;
                for(int i = 0; i < len; ++i){
                    t[i] = v[i];
                }
            }
        }while(next_permutation(v.begin(), v.end()));
        for(int i = 0; i < len; ++i){
            printf("%c ", A + t[i]);
        }
        printf("-> %d\n", ans);
    }
    return 0;
}

 

UVA - 140 Bandwidth(带宽)(全排列)