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个字符串左右两端都没有空格,从高位 ...
随机推荐
- SpringBoot数据库集成-Mybatis
一.java web开发环境搭建 网上有很多教程,参考教程:http://www.cnblogs.com/Leo_wl/p/4752875.html 二.Spring boot搭建 1.Intelli ...
- 三问助你Fundebug
译者按: Debug也要三省吾身! 原文: Three Questions About Each Bug You Find 译者: Fundebug 为了保证可读性,本文采用意译而非直译.另外,本文版 ...
- hive函数应用之操作json
1.创建表 createtable.sql中存放的创建表语句如下 create external table adt.jsontest ( appKey string comment "AP ...
- SqL读取XML、解析XML、SqL将XML转换DataTable、SqL将XML转换表
DECLARE @ItemMessage XML )) SET @ItemMessage=N' <ReceivablesInfos> <ReceivablesList> < ...
- Web标准中xhtml规范的内容有哪些
1.所有的标记都必须要有一个相应的结束标记 以前在HTML中,你可以打开许多标签,例如<p>和<li>而不一定写对应的</p>和</li>来关闭它们.但 ...
- SAP MM ME81N PO Value Analysis报表中Net Value 为负数是怎么回事?
SAP MM ME81N PO Value Analysis报表中Net Value 为负数是怎么回事? ME81N 报表中,如下PO的net value为负数, 怎么回事? 经查这些PO都是退货采购 ...
- LockSupport的源码实现原理以及应用
一.为什么使用LockSupport类 如果只是LockSupport在使用起来比Object的wait/notify简单, 那还真没必要专门讲解下LockSupport.最主要的是灵活性. 上边的例 ...
- ionic 兼容title居中显示和tab栏在显示底部
在app.js里的 .config 里添加配置,同时在函数中引入 $ionicConfigProvider,具体格式如下所示: .config(function($stateProvider, $ur ...
- hdu-2018题(母牛问题)
HDU-2018题/*有一头母牛,它每年年初生一头小母牛.每头小母牛从第四个年头开始,每年年初也生一头小母牛.请编程实现在第n年的时候,共有多少头母牛?Input输入数据由多个测试实例组成,每个测试实 ...
- Android权限之动态权限
安卓系统的权限管理机制从API 23 (也就是Android 6.0 又叫做 Android M,)之后发生了比较大的改变,在一些比较危险的权限上要求必须申请动态权限,即使你在AndroidMainf ...