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

A:FB;B:GC;D:GC;F:AGH;E:HD
#
 

 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(搜索剪枝)的更多相关文章

  1. UVa140 Bandwidth 小剪枝+双射小技巧+枚举全排列+字符串的小处理

    给出一个图,找出其中的最小带宽的排列.具体要求见传送门:UVa140 这题有些小技巧可以简化代码的编写. 本题的实现参考了刘汝佳老师的源码,的确给了我许多启发,感谢刘老师. 思路: 建立双射关系:从字 ...

  2. NOIP2015 斗地主(搜索+剪枝)

    4325: NOIP2015 斗地主 Time Limit: 30 Sec  Memory Limit: 1024 MBSubmit: 270  Solved: 192[Submit][Status] ...

  3. hdu 5469 Antonidas(树的分治+字符串hashOR搜索+剪枝)

    题目链接:hdu 5469 Antonidas 题意: 给你一颗树,每个节点有一个字符,现在给你一个字符串S,问你是否能在树上找到两个节点u,v,使得u到v的最短路径构成的字符串恰好为S. 题解: 这 ...

  4. hdu 5887 搜索+剪枝

    Herbs Gathering Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  5. hdu 5113(2014北京—搜索+剪枝)

    题意:有N*M的棋盘,用K种颜色去染,要求相邻块不能同色.已知每种颜色要染的块数,问能不能染,如果能,输出任一种染法. 最开始dfs失败了- -,优先搜索一行,搜完后进入下一列,超时.本来以为搜索不行 ...

  6. luogu 1731 搜索剪枝好题

    搜索剪枝这个东西真的是骗分利器,然鹅我这方面菜的不行,所以搜索数学dp三方面是真的应该好好训练一下 一本通的确该认真的刷嗯 #include<bits/stdc++.h> using na ...

  7. 搜索+剪枝——POJ 1011 Sticks

    搜索+剪枝--POJ 1011 Sticks 博客分类: 算法 非常经典的搜索题目,第一次做还是暑假集训的时候,前天又把它翻了出来 本来是想找点手感的,不想在原先思路的基础上,竟把它做出来了而且还是0 ...

  8. [HNOI2002]彩票 (搜索+剪枝)

    题目描述 某地发行一套彩票.彩票上写有1到M这M个自然数.彩民可以在这M个数中任意选取N个不同的数打圈.每个彩民只能买一张彩票,不同的彩民的彩票上的选择不同. 每次抽奖将抽出两个自然数X和Y.如果某人 ...

  9. luogu P3393 逃离僵尸岛-搜索剪枝+spfa

    P3393 逃离僵尸岛 题目描述 小a住的国家被僵尸侵略了!小a打算逃离到该国唯一的国际空港逃出这个国家. 该国有N个城市,城市之间有道路相连.一共有M条双向道路.保证没有自环和重边. K个城市已经被 ...

  10. [luogu 1092] 虫食算 (暴力搜索剪枝)

    传送门 Description Input 包含四行. 第一行有一个正整数 (N≤26). 后面的三行,每行有一个由大写字母组成的字符串,分别代表两个加数以及和.这3个字符串左右两端都没有空格,从高位 ...

随机推荐

  1. JSJ—编译器与虚拟机哪个重要?

    阅读本文约“2分钟” 熟悉Java的朋友都知道虚拟机还有编译器,那么它们各自主要的功能是什么?谁比较重要呢?让我们来了解一下这两位美女的故事. 虚拟机可以说就是Java,她能让程序运行起来. 但是编译 ...

  2. Hibernate入门(九)级联删除

    Hibernate级联删除 上一篇文章学习了级联保存和更新,这个级联删除应该很好理解的.一样的道理,删除一方,同时删除有关联的一方. https://www.cnblogs.com/deepSleep ...

  3. vue从入门到进阶:渲染函数 & JSX(八)

    Vue 推荐在绝大多数情况下使用 template 来创建你的 HTML.然而在一些场景中,你真的需要 JavaScript 的完全编程的能力,这就是 render 函数,它比 template 更接 ...

  4. IIS搭建Web服务器,外网可以访问,但无法加载视频

    错误提示如下: 可能原因: IIS的MIME中未注册MP4.ogg.webm相关类型,导致IIS无法识别 解决方法: 在IIS中注册MP4.ogg.webm类型,以下以MP4为例,ogg和webm以此 ...

  5. Android SharedPreferences增,删,查操作

    SharedPreferences是Android平台上一个轻量级的存储类,用来保存应用的一些常用配置,比如Activity状态,Activity暂停时,将此activity的状态保存到SharedP ...

  6. Tab 菜单切换

    <link rel="stylesheet" href="https://blog-static.cnblogs.com/files/hshen/layui.css ...

  7. 遇到一个很古怪的问题,C++类static const成员的初始化

    在我的文件里有这class NFDuration, NFDuration.h里是这样的: // A Duration represents the elapsed time between two i ...

  8. Ubuntu搭建NFS服务器,NFS协议详细分析

    目录 1. Ubuntu搭建NFS服务器 2. NFS协议分析 2.1 实验拓扑: 2.2 在kali抓包分析 1. Ubuntu搭建NFS服务器 ​ NFS(Network FileSystem,网 ...

  9. python os模块常用方法总结

    该模块提供一种便捷的方式来操作系统 os.environ:返回系统环境变量 os.getenv(env):返回环境变量env的值 os.getpid():当前程序的进程 os.uname():返回一个 ...

  10. ERROR 3009 (HY000): Column count of mysql.user is wrong…..

    在测试备份还原时,使用XtraBackup还原数据库后,创建一个测试账号时遇到了下面错误: mysql> grant all on house.* to test@'192.168.%' ide ...