7-6 Bandwidth UVA140
没有清空向量导致debug了好久
这题难以下手 不知道怎么dfs
原来是用排序函数。
letter[n]=i;
id[i]=n++;
用来储存与设置标记十分巧妙
for(;;)
{
while(s[p]!=':'&&p<n1)p++;
if(p==n1)break;
while(s[e]!=';'&&e<n1)e++; for(int i=p+;i<e;i++)
{
dad.push_back(id[ s[p-] ]);
son.push_back(id[ s[i] ]);
}
p++;e++;
}
输入方式是一个难点 lrj采用一个父节点对应一个子结点 方便后续的判断,p和e的使用要注意!
然后 设置pai数组用来排序 数组里先是第一个字典序0,1,2,3。。。n-1(对应的是之前被标记的字母,因为字母是从A按顺序排序的 所以这就是第一个字典序) 然后用 next_permutation函数来自动改变其字典序
同时 设置一个posi函数来储存各个位置 相当于位置下标 差即为距离
这两个数组的设置很值得学习
memcpy 快速保存答案。想起在之前有一道求旅行路径的深搜题可以用
这题很有价值 多打几遍
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std; vector<int>dad,son;
int main()
{
char s[];
int letter[];
int id[];
while(scanf("%s",s)==&&s[]!='#')
{ int n=;
for(char i='A';i<='Z';i++)
{
if(strchr(s ,i)!=NULL)
{
letter[n]=i;
id[i]=n++;
}
}
int p=;
int e=;
int n1=strlen(s);
dad.clear();son.clear();
for(;;)
{
while(s[p]!=':'&&p<n1)p++;
if(p==n1)break;
while(s[e]!=';'&&e<n1)e++; for(int i=p+;i<e;i++)
{
dad.push_back(id[ s[p-] ]);
son.push_back(id[ s[i] ]);
}
p++;e++;
}
int pai[];
int posi[];
int bestposi[];
for(int i=;i<n;i++)pai[i]=i;
int ans=n;
do
{ for(int i=;i<n;i++)posi[ pai[i] ]=i;
int d=;
for(int i=;i<dad.size();i++)
d=max(d,abs( posi[dad[i]]-posi[ son[i] ] ) );
if(d<ans)
{
ans=d;
memcpy(bestposi,pai,sizeof(pai));
} }
while(next_permutation(pai,pai+n)); for(int i=;i<n;i++)printf("%c ",letter[bestposi[i]]);
printf("-> %d\n",ans);
}
return ;
}
7-6 Bandwidth UVA140的更多相关文章
- uva140 - Bandwidth
Bandwidth Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an orderi ...
- UVa140 Bandwidth 小剪枝+双射小技巧+枚举全排列+字符串的小处理
给出一个图,找出其中的最小带宽的排列.具体要求见传送门:UVa140 这题有些小技巧可以简化代码的编写. 本题的实现参考了刘汝佳老师的源码,的确给了我许多启发,感谢刘老师. 思路: 建立双射关系:从字 ...
- Uva140 Bandwidth 全排列+生成测试法+剪枝
参考过仰望高端玩家的小清新的代码... 思路:1.按字典序对输入的字符串抽取字符,id[字母]=编号,id[编号]=字母,形成双射 2.邻接表用两个vector存储,存储相邻关系 ...
- UVA140 ——bandwidth(搜索)
Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an ordering on the ...
- UVa140 Bandwidth 【最优性剪枝】
题目链接:https://vjudge.net/contest/210334#problem/F 转载于:https://www.cnblogs.com/luruiyuan/p/5847706.ht ...
- 递归回溯 UVa140 Bandwidth宽带
本题题意:寻找一个排列,在此排序中,带宽的长度最小(带宽是指:任意一点v与其距离最远的且与v有边相连的顶点与v的距离的最大值),若有多个,按照字典序输出最小的哪一个. 解题思路: 方法一:由于题目说结 ...
- UVA-140 Bandwidth (回溯+剪枝)
题目大意:求一个使带宽最小的排列和最小带宽.带宽是指一个字母到其相邻字母的距离最大值. 题目分析:在递归生成全排列的过程中剪枝,剪枝方案还是两个.一.当前解不如最优解优时,减去:二.预测的理想解不必最 ...
- uva 140 bandwidth (好题) ——yhx
Bandwidth Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an orde ...
- Propagation of Visual Entity Properties Under Bandwidth Constraints
1. Introduction The Saga of Ryzom is a persistent massively-multiplayer online game (MMORPG) release ...
随机推荐
- js 碰撞 + 重力 运动
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- js 将很长的内容进行页面分页显示
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- yolov3实践(二)
这次给大家带来一个有趣的项目,项目主要是Python写的,基于Keras,backend是tf. 首先,当我们回顾视觉目标检测这个任务时,我们可能纠结于如何使这个项目变得更加work,我理解的更加wo ...
- luogu P4036 [JSOI2008]火星人
传送门 很久以前xzz大佬就喊我做这题,结果现在才做qwq 因为要在序列中插入,所以直接用\(Splay\)维护这个串的哈希值,插入就直接把那个点插♂进去,修改就把点旋到根,然后修改和pushup,询 ...
- CMake 示例
1.需求 [1].使用第三方动/静太库 [2].本身代码部分编译为动/静态库 [3]多项目管理 原文转自:http://blog.csdn.net/shuyong1999/article/detail ...
- ftruncate(改变文件大小)
ftruncate(改变文件大小) 定义函数 int ftruncate(int fd,off_t length); 函数说明 ftruncate()会将参数fd指定的文件大小改为参数length指定 ...
- 有pom.xml文件但是无法用maven构建问题
java项目转maven项目,要注意pom.xml文件中是否定义了JDK的版本,要与环境保持一致.项目,右键,configure,选择转换为maven项目即可.转换后,有三个位置需要注意: 1.Jav ...
- 同时装了Python3和Python2,怎么用pip
作者:匿名用户链接:https://www.zhihu.com/question/21653286/answer/95532074来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...
- 使用console进行 性能测试 和 计算代码运行时间
原文:http://www.tuicool.com/articles/JrARVjv 对于前端开发人员,在开发过程中经常需要监控某些表达式或变量的值,如果使用用 debugger 会显得过于笨重,最常 ...
- windows下升级node&npm
一.升级npm npm install -g npm 二.升级node 1.查询node的安装目录 where node 2.在官网下载最新的安装包,直接覆盖安装即可. https://nodejs. ...