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 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 following graph:
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 output
A B C F G D H E -> 3
题意:给一个最多8个结点的无向图,把结点重排后对于图中每条边(u,v),u和v在排列中的最大距离称为该排列的带宽。求带宽最小的排列
算法:枚举全排列。需要注意的是本题的输入格式相对麻烦一点,需要仔细应对
学习点:
1. id和letter的映射关系处理
2. strtok函数使用方法
3.for(int i=0;i<n;i++) pos[P[i]]=i; 确认排列中元素位置
#include<cstdio>
#include<cstring>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=10;
int id[256], letter[maxn];
int n; void map_id_letter(char *p)
{
n=0;
for(int c='A'; c<='Z'; c++) if(strchr(p, c))
{
id[c]=n;
letter[n]=c;
n++;
}
} int main()
{
char buff[256];
while(gets(buff) && buff[0]!='#')
{
map_id_letter(buff);
char*p=strtok(buff, ";");
vector<int> u,v;
while(p)
{
//printf("%s\n", p);
int t=p[0];
++p;//跳过:
while(*(++p))
{
u.push_back(id[t]);
v.push_back(id[*p]);
}
p=strtok(0, ";");
} int ans=n;
int P[maxn], bestP[maxn], pos[maxn];
for(int i=0;i<n;i++) P[i]=i;
do
{
for(int i=0;i<n;i++) pos[P[i]]=i;
int bandwidth=0;
for(int i=0;i<u.size();i++)
{
bandwidth=max(bandwidth, abs(pos[u[i]] - pos[v[i]]));
}
if(bandwidth<ans)
{
ans=bandwidth;
memcpy(bestP, P, sizeof(P));
}
}while(next_permutation(P, P+n)); for(int i=0;i<n;i++)
{
printf("%c ", letter[bestP[i]]);
}
printf("-> %d\n", ans);
} return 0;
}
回溯加剪枝方法,更快:
#include<cstdio>
#include<cstring>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=10;
int id[256], letter[maxn];
int n; void map_id_letter(char *p)
{
n=0;
for(int c='A'; c<='Z'; c++) if(strchr(p, c))
{
id[c]=n;
letter[n]=c;
n++;
}
} int vis[maxn];
int ans;
int P[maxn], bestP[maxn], pos[maxn];
int edge[27][27]; //生成排列
void gen(int d, int bw)
{
if(d==n)
{
ans=bw;
memcpy(bestP, P, sizeof(P));
return;
} for(int i=0;i<n;i++)
{
if(!vis[i])
{
vis[i]=1;
P[d]=i;
//如果新加的节点带宽大于现有带宽,剪枝
int w=0;//新加节点带宽
for(int j=0;j<d;j++)
{
if(edge[i][P[j]])
{
w=d-j;
break;
}
}
int cur_bw=max(bw, w);
if(cur_bw<ans)
gen(d+1, cur_bw);
vis[i]=0;
}
}
} int main()
{
#ifndef ONLINE_JUDGE
freopen("./uva140.in", "r", stdin);
#endif
char buff[256];
while(gets(buff) && buff[0]!='#')
{
map_id_letter(buff);
char*p=strtok(buff, ";");
memset(edge, 0, sizeof(edge));
while(p)
{
int t=p[0];
++p;//跳过:
while(*(++p))
{
edge[id[t]][id[*p]]=1;
edge[id[*p]][id[t]]=1;
}
p=strtok(0, ";");
} ans=n;
gen(0, 0); for(int i=0;i<n;i++)
{
printf("%c ", letter[bestP[i]]);
}
printf("-> %d\n", ans);
} return 0;
}
uva140 - Bandwidth的更多相关文章
- 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 (回溯+剪枝)
题目大意:求一个使带宽最小的排列和最小带宽.带宽是指一个字母到其相邻字母的距离最大值. 题目分析:在递归生成全排列的过程中剪枝,剪枝方案还是两个.一.当前解不如最优解优时,减去:二.预测的理想解不必最 ...
- 7-6 Bandwidth UVA140
没有清空向量导致debug了好久 这题难以下手 不知道怎么dfs 原来是用排序函数. letter[n]=i; id[i]=n++; 用来储存与设置标记十分巧妙 for(;;) { while(s[ ...
- 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 ...
随机推荐
- hdu 5469 Antonidas (dfs+剪枝)2015 ACM/ICPC Asia Regional Shanghai Online
题意: 给出一棵树,再给出每个节点上的值(一个char字符)这些值以一个字符串s1表示,然后给出一个s2字符串,问在这棵树上是否存在两个点,从一个点走到另一个点所经过的路径上的char字符组成的字符串 ...
- Dispatcher
Dispatcher是guava EventBus的事件分发器. Dispatcher是抽象类, 抽象方法: abstract void dispatch(Object event, Iterator ...
- VC++中内存对齐
我们经常看到求 sizeof(A) 的值的问题,其中A是一个结构体,类,或者联合体. 为了优化CPU访问和优化内存,减少内存碎片,编译器对内存对齐制定了一些规则.但是,不同的编译器可能有不同的实现,本 ...
- Spring配置数据库固定代码
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" > &l ...
- ChromePHP - Chrome浏览器下的PHP debug工具
一款 Chrome 下用来配合调试 PHP 的工具,看官方介绍应该和 FirePHP 有异曲同工的.喜欢用Chrome 的PHPer 可以尝试一下. 官方网站:http://www.chromephp ...
- 【Unity入门】场景、游戏物体和组件的概念
版权声明:本文为博主原创文章,转载请注明出处. 游戏和电影一样,是通过每一个镜头的串联来实现的,而这样的镜头我们称之为“场景”.一个游戏一般包含一个到多个场景,这些场景里面实现了不同的功能,把它们组合 ...
- 新手须知设计的法则 Mark
经常看到一些讲如何学习设计的文章,坦白讲感觉有些千篇一律.且不痛不痒,都说要看点书.学点画.练软件.多观察……唉,练软件这事还要说么,难道你还需要告诉一个人学开发是需要学习编程语言的? 学习是基于过往 ...
- CDH4.1基于Quorum-based Journaling的NameNode HA
几个星期前, Cloudera发布了CDH 4.1最新的更新版本,这是第一个真正意义上的独立高可用性HDFS NameNode的hadoop版本,不依赖于特殊的硬件或外部软件.这篇文章从开发者的角度来 ...
- 正整数N是否是素数
来自:[数据结构与算法分析——C语言描述]练习2.13 问题描述: a. 编写一个程序来确定正整数N是否是素数. b. 你的程序在最坏的情形下的运行时间是多少(用N表示)? c. 令B等于N的二进制表 ...
- 如果Apache Spark集群中没有分布式系统,则会?
若当连接到Spark的master之后,若集群中没有分布式文件系统,Spark会在集群中每一台机器上加载数据,所以要确保Spark集群中每个节点上都有完整数据. 通常可以选择把数据放到HDFS.S3或 ...