UVa--140 BandWidth (DFS)
2016-02-09 20:26
393 查看
传送门
题意:
给出一个有n个顶点的图,结点编号 A ~ Z。给定顶点的一个排列,那么定义结点v的带宽B(v)为v和相邻结点在排列中的最远距离,而所有B(v)的最大值定义为图的带宽。本题所求为给定图G,求出让带宽最小的结点的排列。n <= 8.
题解:
考虑到n <= 8,枚举每个排列再分别计算带宽,然后选取带宽最小的一种排列。需要注意的是,结点编号在A~Z范围内取值,是跳跃性的,所以需要映射到一个连续的整数序列上,方便全排列。
此题建立图略微麻烦。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 30;
int graph[maxn][maxn];
int a[maxn], b[maxn];
int n;
map<char, int> mp;
void solve()
{
//结点映射到一个连续整数序列
int i = 0;
for(auto &it : mp){
a[i++] = it.first - 'A';
}
int ans = 1 << 30;
do{
int cur = 0;
//计算结点当前排列下的bandwidth
for(int i = 0; i < n; ++i)
{
for(int j = 0; j < n; ++j)
{
if(graph[a[i]][a[j]]){
cur = max(cur, j - i);
}
}
}
//更新最小bandwidth
if(cur < ans)
{
ans = cur;
for(int i = 0; i < n; ++i)
b[i] = a[i];
}
}while(next_permutation(a, a + n));
for(int i = 0; i < n; ++i)
printf("%c ", 'A' + b[i]);
printf("-> %d\n", ans);
}
int main()
{
string str;
while(cin >> str && str != "#")
{
memset(graph, 0, sizeof(graph));
mp.clear();
//建图
int s = 0, e = 0;
for(int i = 0; i < (int)str.length(); ++i)
{
if(str[i] != ':' && str[i] != ';')
mp[str[i]]++;
e++;
if(str[i] == ';')
{
for(int j = s + 2; j < s + e - 1; ++j)
{
graph[str[s] - 'A'][str[j] - 'A'] = 1;
graph[str[j] - 'A'][str[s] - 'A'] = 1;
}
s += e;
e = 0;
}
}
for(int j = s + 2; j < s + e; ++j)
{
graph[str[s] - 'A'][str[j] - 'A'] = 1;
graph[str[j] - 'A'][str[s] - 'A'] = 1;
}
//图的结点个数
n = mp.size();
solve();
}
}
题意:
给出一个有n个顶点的图,结点编号 A ~ Z。给定顶点的一个排列,那么定义结点v的带宽B(v)为v和相邻结点在排列中的最远距离,而所有B(v)的最大值定义为图的带宽。本题所求为给定图G,求出让带宽最小的结点的排列。n <= 8.
题解:
考虑到n <= 8,枚举每个排列再分别计算带宽,然后选取带宽最小的一种排列。需要注意的是,结点编号在A~Z范围内取值,是跳跃性的,所以需要映射到一个连续的整数序列上,方便全排列。
此题建立图略微麻烦。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 30;
int graph[maxn][maxn];
int a[maxn], b[maxn];
int n;
map<char, int> mp;
void solve()
{
//结点映射到一个连续整数序列
int i = 0;
for(auto &it : mp){
a[i++] = it.first - 'A';
}
int ans = 1 << 30;
do{
int cur = 0;
//计算结点当前排列下的bandwidth
for(int i = 0; i < n; ++i)
{
for(int j = 0; j < n; ++j)
{
if(graph[a[i]][a[j]]){
cur = max(cur, j - i);
}
}
}
//更新最小bandwidth
if(cur < ans)
{
ans = cur;
for(int i = 0; i < n; ++i)
b[i] = a[i];
}
}while(next_permutation(a, a + n));
for(int i = 0; i < n; ++i)
printf("%c ", 'A' + b[i]);
printf("-> %d\n", ans);
}
int main()
{
string str;
while(cin >> str && str != "#")
{
memset(graph, 0, sizeof(graph));
mp.clear();
//建图
int s = 0, e = 0;
for(int i = 0; i < (int)str.length(); ++i)
{
if(str[i] != ':' && str[i] != ';')
mp[str[i]]++;
e++;
if(str[i] == ';')
{
for(int j = s + 2; j < s + e - 1; ++j)
{
graph[str[s] - 'A'][str[j] - 'A'] = 1;
graph[str[j] - 'A'][str[s] - 'A'] = 1;
}
s += e;
e = 0;
}
}
for(int j = s + 2; j < s + e; ++j)
{
graph[str[s] - 'A'][str[j] - 'A'] = 1;
graph[str[j] - 'A'][str[s] - 'A'] = 1;
}
//图的结点个数
n = mp.size();
solve();
}
}
相关文章推荐
- 搜狗百度360市值齐跌:搜索引擎们陷入集体焦虑?
- 本人即将筹备败家日志,敬请期待!
- IE:使用搜索助手
- C++深度优先搜索的实现方法
- 基于文本的搜索
- php实现搜索一维数组元素并删除二维数组对应元素的方法
- 使用Sphinx对索引进行搜索
- asp 多关键词搜索的简单实现方法
- C#使用foreach语句搜索数组元素的方法
- WordPress中用于获取搜索表单的PHP函数使用解析
- JavaScript中数组的排序、乱序和搜索实现代码
- C#编程实现Excel文档中搜索文本内容的方法及思路
- Win2003利用dfs(分布式文件系统)在负载均衡下的文件同步配置方案
- sqlserver中在指定数据库的所有表的所有列中搜索给定的值
- 可以用来搜索当前页面内容的js代码
- 全文搜索和替换
- javascript搜索自动提示功能的实现第1/3页
- mysql 模糊搜索的方法介绍
- C#搜索文字在文件及文件夹中出现位置的方法
- 基于ajax的简单搜索实现方法