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[] = {, , -, , -, -, , };
const int dc[] = {-, , , , -, , -, };
const int MOD = 1e9 + ;
const double pi = acos(-1.0);
const double eps = 1e-;
const int MAXN = + ;
const int MAXT = + ;
using namespace std;
char s[MAXN];
set<int> node[];
vector<int> v;
int t[];
void init(){
for(int i = ; i < ; ++i){
node[i].clear();
}
v.clear();
memset(t, , sizeof t);
int len = strlen(s);
for(int i = ; i < len; ++i){
if(s[i] == ':'){
int tmp = s[i - ] - 'A';
while(){
++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) == ){
if(s[] == '#') return ;
init();
for(int i = ; i < ; ++i){
if(node[i].size()){
v.push_back(i);
}
}
int len = v.size();
int ans = INT_M_INF;
do{
int tmp = ;
for(int i = ; i < len; ++i){
for(int j = ; j < i; ++j){
if(node[v[i]].count(v[j])){
tmp = max(tmp, i - j);
}
}
}
if(tmp < ans){
ans = tmp;
for(int i = ; i < len; ++i){
t[i] = v[i];
}
}
}while(next_permutation(v.begin(), v.end()));
for(int i = ; i < len; ++i){
printf("%c ", 'A' + t[i]);
}
printf("-> %d\n", ans);
}
return ;
}
上一篇:LEETCODE —— binary tree [Same Tree] && [Maximum Depth of Binary Tree]


下一篇:UEditor工具栏上自定义按钮、图标、事件和右击菜单添加自定义按钮