思路:暴力+剪枝

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. POJ 2449 求第K短路

    第一道第K短路的题目 QAQ 拿裸的DIJKSTRA + 不断扩展的A* 给2000MS过了 题意:大意是 有N个station 要求从s点到t点 的第k短路 (不过我看题意说的好像是从t到s 可能是 ...

  2. ZOJ2849 优先队列BFS

    Attack of Panda Virus Time Limit: 3 Seconds      Memory Limit: 32768 KB In recent months, a computer ...

  3. TensorFlow文本与序列的深度模型

    TensorFlow深度学习笔记 文本与序列的深度模型 Deep Models for Text and Sequence 转载请注明作者:梦里风林Github工程地址:https://github. ...

  4. stdcall、cdecl、fastcall、thiscall 、naked call的汇编详解

    函数调用规范   当高级语言函数被编译成机器码时,有一个问题就必须解决:因为CPU没有办法知道一个函数调用需要多少个.什么样的参数.即计算机不知道怎么给这个函数传递参数,传递参数的工作必须由函数调用者 ...

  5. 设计模式(七)组合模式Composite(结构型)

    设计模式(七)组合模式Composite(结构型) 1. 概述 在数据结构里面,树结构是很重要,我们可以把树的结构应用到设计模式里面. 例子1:就是多级树形菜单. 例子2:文件和文件夹目录 2.问题 ...

  6. [cocos2d-x]针对不同的设备,选取不同的自适应图片

    前言: 我们在进行移动设备开发的时候,我们常常会准备不同大小的图片资源以适应不同大小的设备,下面我称普清图片资源和高清图片资源.那么如何做到图片资源的自适应呢?下面我来用一个demo展示一下这个效果的 ...

  7. W5100使用中的常见问题

    来自:成都浩然 越来越多的嵌入式网络系统project师喜欢上了W5100,它集TCP/IP协议栈.以太网的MAC和PHY一体,不仅使系统性能得到非常大的提升,也给产品开发工作带来极大的方便.随着W5 ...

  8. linux路由表配置

    一.原理说明 1.路由表(table)从0到255进行编号,每个编号可以对应一个别名,编号和别名的对应关系在linux下放在/etc/iproute2/rt_tables这个文件里,一般0编号的tab ...

  9. Sql 参数的使用

    代码片段: a)声明实例 1.声明SQLCommand实例. SqlCommand cmd = new SqlCommand(); 2.声明SqlDataAdapter实例. SqlDataAdapt ...

  10. 面试中的Singleton

      引子 “请写一个Singleton.”面试官微笑着和我说. “这可真简单.”我心里想着,并在白板上写下了下面的Singleton实现: 1 class Singleton 2 { 3 public ...