uva 140
思路:暴力+剪枝
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的更多相关文章
- 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 ...
- UVa 140 (枚举排列) Bandwidth
题意较复杂,请参见原题=_=|| 没什么好说的,直接枚举每个排列就好了,然后记录最小带宽,以及对应的最佳排列. STL里的next_permutation函数真是好用. 比较蛋疼的就是题目的输入了.. ...
- 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 ...
- UVA 140 Bandwidth
题意: 给出一个n个节点的图G,和一个节点的排列,定义节点i的带宽为i和相邻节点在排列中的最远距离,而所有带宽的最大值就是图的带宽,求让图的带宽最小的排列. 分析: 列出所有可能的排列,记录当前找到的 ...
- UVA - 140 Bandwidth(带宽)(全排列)
题意:给定图,求是带宽最小的结点排列. 分析:结点数最多为8,全排列即可.顶点范围是A~Z. #pragma comment(linker, "/STACK:102400000, 10240 ...
- UVa 140 带宽
题意:给出一个n个结点的图G和一个结点的排列,定义结点的带宽为i和相邻结点在排列中的最远距离,求出让带宽最小的结点排列. 思路:用STL的next_permutation来做确实是很方便,适当剪枝一下 ...
- UVA 140 Brandwidth 带宽 (dfs回溯)
看到next_permutation好像也能过╮(╯▽╰)╭ 这题学习点: 1.建图做映射 2.通过定序枚举保证字典序最小 3.strtok,sscanf,strchr等函数又复习了一遍,尽管程序中没 ...
- UVA 140 Bandwidth (dfs 剪枝 映射)
题意: 给定一个n个结点的图G和一个结点的排列, 定义结点i的带宽b(i)为i和相邻结点在排列中的最远距离, 所有b(i)的最大值就是这个图的带宽, 给定G, 求让带宽最小的结点排列. 给定的图 n ...
- UVa 140 Bandwidth【枚举排列】
题意:给出n个节点的图,和一个节点的排列,定义节点i的带宽b[i]为i和其相邻节点在排列中的最远的距离,所有的b[i]的最大值为这个图的带宽,给一个图,求出带宽最小的节点排列 看的紫书,紫书上说得很详 ...
随机推荐
- HDU 3917 最大权闭合图 求最小割
具体参考http://blog.csdn.net/power721/article/details/6665750 TODO //#pragma comment(linker, "/STAC ...
- thinkphp 分组、页面跳转与ajax
本节课大纲: 一.多应用配置技巧 二.使用分组 三.页面跳转 $this->success('查询成功',U('User/test')); $this->redirect('User/te ...
- java序列化对象 插入、查询、更新到数据库
java序列化对象 插入.查询.更新到数据库 : 实现代码例如以下: import java.io.ByteArrayInputStream; import java.io.ByteArrayOutp ...
- 分布式文件系统GlusterFS
转自于:http://www.cnblogs.com/zitjubiz/archive/2012/11/30/Distributed_File_System_glusterFS.html Gluste ...
- C++多态原理
C++的多态性是通过动态绑定实现的 非虚函数是在编译时绑定的; 通过对象进行的函数(虚函数,非虚函数)也是编译时绑定的; C++编译器在编译的时候,要确定每个对象调用的函数(要求此函数是非虚函数)的地 ...
- C# 课堂总结2-数据类型及转换方式
一.输入输出语句 Console.ReadLine(); 会等待直到用户按下回车,一次读入一行Console.ReadKey(); 则是等待用户按下任意键,一次读入一个字符. 二.数据类型 主要掌握: ...
- Solr基础理论与维护管理快速上手(含查询参数说明)
1. solr基础 因为 Solr 包装并扩展了 Lucene,所以它们使用很多相同的术语.更重要的是,Solr 创建的索引与 Lucene 搜索引擎库完全兼容.通过对 Solr 进行适当的配置,某些 ...
- 当JAVA集合移除自身集合元素时发生的诸多问题
一段代码目的是想删除集合中包括"a"字符串的集合项: public class TestForeach { public static void main(String[] arg ...
- No http handler was found for request type 'GET'
asp.net 运用统计图表的时候,在本地运行,没有问题,发布到服务器上的IIS后,就报这个错误,具体错误 看图 这个图片真恶心,网上找了下,资料不多,但是还是有个能真正解决问题的, 解决方法是,在w ...
- Delphi控件下载网
http://delphi-z.ru/index.php http://developer.team/delphi/