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]的最大值为这个图的带宽,给一个图,求出带宽最小的节点排列 看的紫书,紫书上说得很详 ...
随机推荐
- STRUTS2获得session和request
在struts1中,获得到系统的request或者session对象非常方便,都是按照形参传递的,但是在struts2中,request和session都被隐藏了struts提供两种方式访问sessi ...
- Apache ab 使用说明
第一章 简介 ab是Apache超文本传输协议(HTTP)的性能测试工具.其设计意图是描绘当前所安装的Apache的执行性能,主要是显示你安装的Apache每秒可以处理多少个请求. 第二章 说明 ab ...
- Hadoop实战实例
Hadoop实战实例 Hadoop实战实例 Hadoop 是Google MapReduce的一个Java实现.MapReduce是一种简化的分布式编程模式,让程序自动分布 ...
- vc++窗口的创建过程(MFC消息机制的经典文章)
一.什么是窗口类 在Windows中运行的程序,大多数都有一个或几个可以看得见的窗口,而在这些窗口被创建起来之前,操作系统怎么知道该怎样创建该窗口,以及用户操作该窗口的各种消息交给谁处理呢?所以VC ...
- div无法触发blur事件解决的方法
默认情况下div无法获取焦点,无法触发focus与blur事件,推測span,a等标签也无法触发焦点事件(input:button.及button标签能够触发) 怎样使div触发blur事件:能够给d ...
- UVa 121 - Pipe Fitters
称号:放置在一个圆中的矩形,它要求每个圆的每行或列是切线,问:多少能竖起来. 分析:计算几何.数论.首先计算矩形显示屏,然后计算互显示器(每一行与相邻行相同差1个月)求最大,你可以. 说明:╮(╯▽╰ ...
- 关于apche无缘无故个启动不了,解决方法
1. 对于用户不小心把apache下的conf文件不小心给修改了,可那会导致,启动不了apache, 解决办法可以重新下载一个, 64为 32位 下载地址 http://www.veryhuo.c ...
- CheckBoxList控件
主要介绍:自定义数据.绑定数据库数据.全选,取消全选. 这种方法是绑定已经给定(自定义)的字段(这种方法是绑定给定的值,就是在编写控件时给Text赋的值): 前台代码: <asp:CheckBo ...
- Eclipse使用技巧总结(三)
十六.快速关闭窗口 关闭当前打开窗口 Ctrl + W 关闭当前打开的所有窗口 Ctrl +Shift +F4 十九.重命名 F2 二十.快速回到上次编辑点 Ctrl + Q 二十 ...
- 动态规划之一最长上升子序列LIS
//最长上升子序列 #include<iostream> #include<cstring> using namespace std; const int maxn = 101 ...