思路:暴力+剪枝

uva140

wa了好多次……数组开小了……!!!

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <stack>
#include <cctype>
#include <string>
#include <malloc.h>
#include <queue>
#include <map> using namespace std;
const int INF = 0xffffff;
const double esp = 10e-;
const double Pi = * atan(1.0);
const int Maxn = ;
const int mod = ;
const int dr[] = {,,-,,-,,-,};
const int dc[] = {,,,-,,-,-,};
//int dir2[8][2] = {{-1,0},{0,-1},{-1,1},{1,-1},{-1,-1},{1,0},{0,1},{1,1}}; bool graph[Maxn][Maxn];
int arr[Maxn];
int n;
int Min;
int tot;
bool visit[Maxn];
int ans[Maxn];
int init[Maxn]; void dfs(int cur){
if(tot >= Min)
return;
if(cur == n){
Min = min(Min,tot);
for(int i = ;i < n;i++){
ans[i] = arr[i];
}
return;
}
for(int i = ;i < n;i++){
if(!visit[ init[i] ]){
visit[init[i]] = ;
arr[cur] = init[i];
int tmp = ;
for(int j = ;j < cur;j++){
if(graph[ arr[j] ][ init[i] ]){
int tt = abs(cur - j);
tmp = max(tmp,tt);
if(tmp > Min)
break;
}
}
int tt = tot;
tot = max(tmp,tot);
dfs(cur+);
visit[init[i]] = ;
tot = tt;
}
}
}
char str[];
int main()
{
#ifndef ONLINE_JUDGE
freopen("inpt.txt","r",stdin);
#endif
while(scanf("%s",str) != EOF){
if(str[] == '#')
break;
int len = strlen(str);
n = ;
memset(graph,,sizeof(graph));
memset(arr,,sizeof(arr));
memset(visit,,sizeof(visit));
for(int i = ;i < len;i++){
if(str[i] == ' ')
continue;
int a = str[i] - 'A';
if(!arr[a]){
init[n++] = str[i] - 'A';
arr[a] = ;
}
for(i = i+;str[i] != ';' && i < len;i++){
if(!isalpha(str[i]))
continue;
int b = str[i] - 'A';
graph[a][b] = ;
graph[b][a] = ;
if(!arr[b]){
init[n++] = str[i] - 'A';
arr[b] = ;
}
}
}
Min = INF;
tot = ;
sort(init,init+n);
dfs();
for(int i = ;i < n;i++){
printf("%c ",ans[i] + 'A');
}
printf("-> %d\n",Min);
}
return ;
}

uva 140的更多相关文章

  1. uva 140 bandwidth (好题) ——yhx

     Bandwidth  Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an orde ...

  2. UVa 140 (枚举排列) Bandwidth

    题意较复杂,请参见原题=_=|| 没什么好说的,直接枚举每个排列就好了,然后记录最小带宽,以及对应的最佳排列. STL里的next_permutation函数真是好用. 比较蛋疼的就是题目的输入了.. ...

  3. UVA 140 (13.07.29)

     Bandwidth  Given a graph (V,E) where V is a set of nodes and E is a set of arcsin VxV, and anorderi ...

  4. UVA 140 Bandwidth

    题意: 给出一个n个节点的图G,和一个节点的排列,定义节点i的带宽为i和相邻节点在排列中的最远距离,而所有带宽的最大值就是图的带宽,求让图的带宽最小的排列. 分析: 列出所有可能的排列,记录当前找到的 ...

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

    题意:给定图,求是带宽最小的结点排列. 分析:结点数最多为8,全排列即可.顶点范围是A~Z. #pragma comment(linker, "/STACK:102400000, 10240 ...

  6. UVa 140 带宽

    题意:给出一个n个结点的图G和一个结点的排列,定义结点的带宽为i和相邻结点在排列中的最远距离,求出让带宽最小的结点排列. 思路:用STL的next_permutation来做确实是很方便,适当剪枝一下 ...

  7. UVA 140 Brandwidth 带宽 (dfs回溯)

    看到next_permutation好像也能过╮(╯▽╰)╭ 这题学习点: 1.建图做映射 2.通过定序枚举保证字典序最小 3.strtok,sscanf,strchr等函数又复习了一遍,尽管程序中没 ...

  8. UVA 140 Bandwidth (dfs 剪枝 映射)

    题意: 给定一个n个结点的图G和一个结点的排列, 定义结点i的带宽b(i)为i和相邻结点在排列中的最远距离, 所有b(i)的最大值就是这个图的带宽, 给定G, 求让带宽最小的结点排列. 给定的图 n ...

  9. UVa 140 Bandwidth【枚举排列】

    题意:给出n个节点的图,和一个节点的排列,定义节点i的带宽b[i]为i和其相邻节点在排列中的最远的距离,所有的b[i]的最大值为这个图的带宽,给一个图,求出带宽最小的节点排列 看的紫书,紫书上说得很详 ...

随机推荐

  1. awk 工具简介NF-NR

    相较于 sed 常常作用于一整个行的处理, awk 则比较倾向于一行当中分成数个『字段』来处理. 因此,awk 相当的适合处理小型的数据数据处理呢!awk 通常运作的模式是这样的:[root@linu ...

  2. perl 回调必须是函数引用

    [root@wx03 lib]# cat a1.pl use AE; use AnyEvent; ##定义watch my $t = AnyEvent->timer( after => 0 ...

  3. GDOI2015——已成梦

    今年GDOI(2015)在韶关北江中学(没记错的话应该是武江区)举行,感觉这五天就是一场梦,一场包含苦辣的梦. Day0 坐了一个上午的车,而且车内的空气又不好,感觉整个人都累倒下了. 到了北江之后吃 ...

  4. Savitzky-Golay滤波器(2)

    前几天写过一篇介绍 Savitzky-Golay滤波器的文章, 没想到最近做项目还真的用上了. 因此就顺便写了个 C 语言的自动计算生成 SG 滤波器系数的程序.利用这里的代码可以生成任意阶数的 SG ...

  5. codeforces 437C The Child and Toy

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  6. 多图片/文件上传 - SwfUpload/PlUpload

    <文件上传利器SWFUpload使用指南> <前端上传组件Plupload使用指南>

  7. Eclipse用法和技巧十八:减少不必要的输入

    写代码的时候,很多人都有一个原则,尽量上输入.依靠IDE自动生成的代码,一般可读性,排版什么的都还是不错的,最主要的一般不会有什么低级错误.今天介绍几个在eclipse环境中,常用的依靠eclipse ...

  8. 程序启动报错:ORA-12505;PL/SQL却可以登录的解决方法

    一.异常{ ORA-12505, TNS:listener does not currently know of SID given in connect descriptor The Connect ...

  9. 经典系统windows xp sp3官方原版下载(附XP序列号)

    经典系统windows xp sp3官方原版下载(附XP序列号)    在过几日,微软最新的操作系统windows 7就将像外发售了!虽然Vista操作系统已经问世多年,但XP系统仍然占据着王位,在市 ...

  10. 挺苹果的声音,iPhone 5s的两处进步

    苹果iPhone 5s发布后的两处重大进步让我很关注,但看了网上众多网友的点评,又深深的被中国当前手机发烧友圈的这种屌丝文化所震撼,这不是一条正确的道路,这将把中国的手机产业引向歧途,所以我不得不说几 ...