UVA140-Bandwidth(搜索剪枝)
Problem UVA140-Bandwidth
Time Limit: 3000 mSec
Problem Description
Given a graph (V, E) where V is a set of nodes and E is a set of arcs in V ×V , and an ordering on the elements in V , then the bandwidth of a node v is defined as the maximum distance in the ordering between v and any node to which it is connected in the graph. The bandwidth of the ordering is then defined as the maximum of the individual bandwidths. For example, consider the graph on the right: This can be ordered in many ways, two of which are illustrated below: For these orderings, the bandwidths of the nodes (in order) are 6, 6, 1, 4, 1, 1, 6, 6 giving an ordering bandwidth of 6, and 5, 3, 1, 4, 3, 5, 1, 4 giving an ordering bandwidth of 5. Write a program that will find the ordering of a graph that minimises the bandwidth.
Input
Input will consist of a series of graphs. Each graph will appear on a line by itself. The entire file will be terminated by a line consisting of a single ‘#’. For each graph, the input will consist of a series of records separated by ‘;’. Each record will consist of a node name (a single upper case character in the the range ‘A’ to ‘Z’), followed by a ‘:’ and at least one of its neighbours. The graph will contain no more than 8 nodes.
Output
Output will consist of one line for each graph, listing the ordering of the nodes followed by an arrow (->) and the bandwidth for that ordering. All items must be separated from their neighbours by exactly one space. If more than one ordering produces the same bandwidth, then choose the smallest in lexicographic ordering, that is the one that would appear first in an alphabetic listing.
Sample Input
Sample Ouput
A B C F G D H E -> 3
题解:回溯法,剪枝主要有两点:最优化剪枝,这是显然的;如果搜索到u节点,此时u节点的孩子节点还有m个没有确定,那么最理想的情况下带宽也至少是m,因此如果m > 当前最小带宽,就完全可以剪枝。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#define INF 0x3f3f3f3f
using namespace std; const int maxl = +;
const int kind = ;
char str[maxl];
int n,Min = INF,id[];
int res[maxl];
char converse[kind];
bool vis[kind];
vector< vector<int> > child(kind); int pos_find(const int *num,int len,int tar){
for(int k = ;k < len;k++){
if(num[k] == tar) return k;
}
return -;
} bool check(const int *ans,int &wide){
int u,v;
for(int i = ;i < n;i++){
u = ans[i];
for(int j = ;j < child[u].size();j++){
v = child[u][j];
int pos = pos_find(ans,n,v);
wide = max(wide,abs(i-pos));
if(wide > Min) return false;
}
}
return true;
} void dfs(int *ans,int cur,int wide){
//printf("cur:%d\n",cur);
if(wide > Min) return;
if(cur == n){
if(check(ans,wide)){
if(wide < Min){
Min = wide;
memcpy(res,ans,n*sizeof(int));
}
else if(wide == Min){
if(res[] == -) memcpy(res,ans,n*sizeof(int));
else{
int p = ;
while(res[p] == ans[p]) p++;
if(p!=n && ans[p]<res[p]) memcpy(res,ans,n*sizeof(int));
}
}
}
return;
}
for(int u = ;u < n;u++){
if(!vis[u]){
int cnt = ,tmp = wide;
bool ok = true;
for(int j = ;j < child[u].size();j++){
int v = child[u][j];
if(!vis[v]) continue;
cnt++;
int pos = pos_find(ans,cur,v);
tmp = max(wide,cur-pos);
//printf("tmp:%d\n",tmp);
if(tmp > Min){
ok = false;
break;
}
}
if(!ok) continue;
cnt = child[u].size()-cnt;
if(cnt > Min) continue;
ans[cur] = u;
vis[u] = true;
dfs(ans,cur+,tmp);
vis[u] = false;
}
}
} int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
while(~scanf("%s",str) && str[]!='#'){
n = ;
Min = INF;
memset(res,-,sizeof(res));
memset(vis,false,sizeof(vis));
int len = strlen(str);
for(char ch = 'A';ch <= 'Z';ch++){
if(strchr(str,ch) != NULL){
id[ch-'\0'] = n++;
converse[n-] = ch;
}
}
for(int i = ;i < n;i++) child[i].clear();
for(int i = ;i < len;i++){
int u = id[str[i]-'\0'];
i += ;
while(str[i]!=';' && i<len){
int v = id[str[i++]-'\0'];
child[u].push_back(v);
child[v].push_back(u);
}
}
//for(int i = 0;i < n;i++) printf("%d\n",child[i].size());
int ans[kind];
memset(ans,,sizeof(ans));
dfs(ans,,);
for(int i = ;i < n;i++){
printf("%c ",converse[res[i]]);
}
printf("-> %d\n",Min);
}
return ;
}
UVA140-Bandwidth(搜索剪枝)的更多相关文章
- UVa140 Bandwidth 小剪枝+双射小技巧+枚举全排列+字符串的小处理
给出一个图,找出其中的最小带宽的排列.具体要求见传送门:UVa140 这题有些小技巧可以简化代码的编写. 本题的实现参考了刘汝佳老师的源码,的确给了我许多启发,感谢刘老师. 思路: 建立双射关系:从字 ...
- NOIP2015 斗地主(搜索+剪枝)
4325: NOIP2015 斗地主 Time Limit: 30 Sec Memory Limit: 1024 MBSubmit: 270 Solved: 192[Submit][Status] ...
- hdu 5469 Antonidas(树的分治+字符串hashOR搜索+剪枝)
题目链接:hdu 5469 Antonidas 题意: 给你一颗树,每个节点有一个字符,现在给你一个字符串S,问你是否能在树上找到两个节点u,v,使得u到v的最短路径构成的字符串恰好为S. 题解: 这 ...
- hdu 5887 搜索+剪枝
Herbs Gathering Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- hdu 5113(2014北京—搜索+剪枝)
题意:有N*M的棋盘,用K种颜色去染,要求相邻块不能同色.已知每种颜色要染的块数,问能不能染,如果能,输出任一种染法. 最开始dfs失败了- -,优先搜索一行,搜完后进入下一列,超时.本来以为搜索不行 ...
- luogu 1731 搜索剪枝好题
搜索剪枝这个东西真的是骗分利器,然鹅我这方面菜的不行,所以搜索数学dp三方面是真的应该好好训练一下 一本通的确该认真的刷嗯 #include<bits/stdc++.h> using na ...
- 搜索+剪枝——POJ 1011 Sticks
搜索+剪枝--POJ 1011 Sticks 博客分类: 算法 非常经典的搜索题目,第一次做还是暑假集训的时候,前天又把它翻了出来 本来是想找点手感的,不想在原先思路的基础上,竟把它做出来了而且还是0 ...
- [HNOI2002]彩票 (搜索+剪枝)
题目描述 某地发行一套彩票.彩票上写有1到M这M个自然数.彩民可以在这M个数中任意选取N个不同的数打圈.每个彩民只能买一张彩票,不同的彩民的彩票上的选择不同. 每次抽奖将抽出两个自然数X和Y.如果某人 ...
- luogu P3393 逃离僵尸岛-搜索剪枝+spfa
P3393 逃离僵尸岛 题目描述 小a住的国家被僵尸侵略了!小a打算逃离到该国唯一的国际空港逃出这个国家. 该国有N个城市,城市之间有道路相连.一共有M条双向道路.保证没有自环和重边. K个城市已经被 ...
- [luogu 1092] 虫食算 (暴力搜索剪枝)
传送门 Description Input 包含四行. 第一行有一个正整数 (N≤26). 后面的三行,每行有一个由大写字母组成的字符串,分别代表两个加数以及和.这3个字符串左右两端都没有空格,从高位 ...
随机推荐
- [leetcode]984. 不含 AAA 或 BBB 的字符串
给定两个整数 A 和 B,返回任意字符串 S,要求满足: S 的长度为 A + B,且正好包含 A 个 'a' 字母与 B 个 'b' 字母: 子串 'aaa' 没有出现在 S 中: 子串 'bbb' ...
- (7)Microsoft office Word 2013版本操作入门_常用技巧
1.自定义快速功能栏调整:常用功能按钮可以设置显示到此处.(如图所示的另存为和插入批注功能) 2.word中截图功能: 2.1 截图插入后的图片,可以进行设置 选中图片---点击[格式]可以设置图片 ...
- python文件夹copy器(多进程版)
本节的练习的要求如下: 输入要拷贝文件夹的文件名称 读取该文件夹下的所有文件 启动5个进程来拷贝文件夹,将拷贝成功的文件名称放入队列中 主进程中显示文件拷贝的进度 代码如下: import multi ...
- 【Linux】nginx常用命令
相关内容链接 Centos之安装Nginx及注意事项 [nginx]详细配置说明 nginx常用命令 [重新加载配置]sudo nginx -s reload [打开nginx配置]sudo vim ...
- How to Create a First Shell Script
How to Create a First Shell Script Shell scripts are short programs that are written in a shell pr ...
- xpath语法笔记
xpath是在xml文档中查找信息的语言, 其语法整理如下: 路径表达式(选取节点) div div下的所有子节点 /div 根节点下的div //div 匹配所有的div . 当前节点 .. 父节点 ...
- 四个O(n^2)级别的排序性能测试
测试环境为DEV-C++,并且选择排序,插入排序,冒泡排序,均为优化后的,若想了解具体优化过程,请参照:https://blog.csdn.net/qq_40164152 测试用例: #ifndef ...
- 将Y-m-d转换为Y年m月d日
自己编写的,不能直接套用,理解后可自行变化: $var=explode(' ',$res['act_starting']); $var1=$var[0]; $time=explode ...
- 微信小程序使用wxParse,解决图片显示路径问题
我们经常用到发布文章,用的是UEditor百度富文本编辑器,方便排版,存储的也是html代码,这样小程序解析出来的也是排版的样式,但是使用wxParse解析html的时候,因为存储的是图片的相对路径, ...
- ThreadPoolExecutor 线程池的源码解析
1.背景介绍 上一篇从整体上介绍了Executor接口,从上一篇我们知道了Executor框架的最顶层实现是ThreadPoolExecutor类,Executors工厂类中提供的newSchedul ...