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. 教你搞定ElasticSearch(head)

    简介: ElasticSearch(以下简称ES)是一个基于Lucene构建的开源(open-source),分布式(distributed),RESTful,实时(real-time)的搜索与分析( ...

  2. javascript模块化编程-立即执行函数(IIFE)

    IIFE 全拼Imdiately Invoked Function Expression,立即执行的函数表达式. 语法 var module1 = (function(){ var _count = ...

  3. O(n*logn)级别的算法之一(归并排序及其优化)

    原理: 设两个有序的子序列(相当于输入序列)放在同一序列中相邻的位置上:array[low..m],array[m + 1..high],先将它们合并到一个局部的暂存序列 temp (相当于输出序列) ...

  4. blfs(systemd版本)学习笔记-编译安装配置dhcpcd

    我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! dhcpcd项目地址:http://www.linuxfromscratch.org/blfs/view/stable-syst ...

  5. K8S flannel

    kubernetes网络通信方式有: 容器间的通信 : pod内的容器通信 通过(lo)设备 Pod之间的通信 :pod IP <-----> pod IP ,K8S 要求所有的 pod ...

  6. [总结]高效的jQuery代码编写技巧

    缓存变量 DOM遍历是昂贵的,所以尽量将会重用的元素缓存. // 糟糕 h = $('#element').height(); $('#element').css('height',h-20); // ...

  7. java对程序的简单加密

    File file = new File("oppo.in"); File file1 = new File("main.in"); GregorianCale ...

  8. HTML5 & CSS3初学者指南(4) – Canvas使用

    介绍 传统的HTML主要用于文本的创建,可以通过<img>标签插入图像,动画的实现则需要第三方插件.在这方面,传统的HTML极其缺乏满足现代网页多媒体需求的能力.HTML5的到来,带来了新 ...

  9. mybatis的三种批量插入以及次效率比较

    1.表结构 CREATE TABLE `t_user` ( `id` varchar(32) CHARACTER SET utf8 NOT NULL COMMENT '主键', `name` varc ...

  10. leetcode-9.回文数(水仙花数)

    leetcode-9.回文数(水仙花数) 题意:给定整数,判断是否是水仙花数(回文数),返回判断结果 算法: 1.判断负数, 如果是负数直接返回false 2.将整数逐位拆解,用数组存储 3.遍历数组 ...