码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-01-15 22:56:08      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:blog   pac   iostream   全排列   []   print   can   string   顶点   

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

分析:结点数最多为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(带宽)(全排列)

标签:blog   pac   iostream   全排列   []   print   can   string   顶点   

原文地址:http://www.cnblogs.com/tyty-Somnuspoppy/p/6288041.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!