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个字符串左右两端都没有空格,从高位 ...
随机推荐
- HbuilderX 常用快捷键
最新2019.3.30版本已经添加idea的快捷键,就不需要再额外记住HbuilderX的快捷键啦~ 链接 输入a,按下tab 加粗 ctrl+B 换行 需要换行的那一行后面加2个空格(或者按下tab ...
- Golang 正则表达式Regex相关资料整理
Golang 支持的正在表达式是 https://github.com/google/re2/wiki/Syntax 注意这里提示 NOT SUPPORTED的。 工具 一些测试正则表达式的工具 推荐 ...
- Docker命令行与守护进程如何交互?
译者按: Docker是典型的C/S架构,其守护进程(daemon)与命令行(CLI)是通过REST API进行交互的. 原文: Understanding how the Docker Daemon ...
- 百度前端学院-基础学院-第七天到第八天之BFC
1.BFC简介 BFC全称"block formatting context",中文为“块级格式化上下文”.特征总之记住一句话: BFC元素特征表现原则就是:内部元素无论怎么翻江倒 ...
- laravel表单验证
效果展示: 代码 实现: 后台: use Validator; public function login() { if($input = Input::all()){ //验证提交的 ...
- 1,JavaScript前世今生
JavaScript历史大概在1992年,一家称作Nombas的公司开始开发一种叫做C–(C-minus-minus,简称Cmm)的嵌入式脚本语言. Cmm背后的理念很简单:一个足够强大可以替代宏操作 ...
- FE 命令随笔
FE_CMD ————— * >>>>>>>> Vue ________________________________________________ ...
- css实现div内一段文本的两端对齐
在一个固定宽度的div内,使得P标签内的文本两端对齐: text-align: justify;text-justify:inter-ideograph; <!DOCTYPE html> ...
- markdown 语法指南
说明:左边是markdown的语法 右边是预览.(我这里用了黑色的背景,一般白色较多) 1. 标题 2.列表 3.引用 (1)一层引用 (2)多层引用 4.图片(如果是本地:按照语法写图片路径:如果是 ...
- K8s helm 创建自定义Chart
# 删除之前创建的 chart helm list helm delete --purge redis1 # 创建自定义 chart myapp cd ~/helm helm create myapp ...