UVa 140 带宽
题意:给出一个n个结点的图G和一个结点的排列,定义结点的带宽为i和相邻结点在排列中的最远距离,求出让带宽最小的结点排列。
思路:用STL的next_permutation来做确实是很方便,适当剪枝一下就可以了,不过我不明白的是为什么我用string字符串会超时...
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std; int a[], c[];
int ans[];
int map[][];
char str[]; int main()
{
//freopen("D:\\txt.txt", "r", stdin);
int l, k, maxn,n;
while (gets(str) )
{
if (str[] == '#') break;
memset(map, , sizeof(map));
memset(a, , sizeof(a));
memset(ans, , sizeof(ans));
memset(c, , sizeof(c));
l = strlen(str); for (int i = ; i < l; i++)
{
if (str[i] == ':')
{
int t = str[i - ] - 'A';
a[t] = ;
for (k = i + ; str[k] != ';' && k < l; k++)
{
int p = str[k] - 'A';
a[p] = ;
map[t][p] = map[p][t] = ;
}
i = k;
}
} n = ;
for (int i = ; i < ; i++)
{
if (a[i])
c[n++] = i;
}
maxn = ;
do
{
int sum = ;
int ok = ;
for (int i = ; i < n; i++)
{
for (int j = i + ; j < n; j++)
{
if (map[c[i]][c[j]])
if (j - i>sum)
sum = j - i;
if (sum>=maxn) { ok = ; break; }
}
if (!ok) break;
} if (sum < maxn)
{
maxn = sum;
memcpy(ans, c, sizeof(c));
}
} while (next_permutation(c, c + n));
for (int i = ; i < n; i++)
{
char s = 'A' + ans[i];
cout << s << " ";
}
cout << "-> " << maxn << endl;
}
return ;
}
UVa 140 带宽的更多相关文章
- UVA - 140 Bandwidth(带宽)(全排列)
题意:给定图,求是带宽最小的结点排列. 分析:结点数最多为8,全排列即可.顶点范围是A~Z. #pragma comment(linker, "/STACK:102400000, 10240 ...
- UVA 140 Brandwidth 带宽 (dfs回溯)
看到next_permutation好像也能过╮(╯▽╰)╭ 这题学习点: 1.建图做映射 2.通过定序枚举保证字典序最小 3.strtok,sscanf,strchr等函数又复习了一遍,尽管程序中没 ...
- 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 ...
- UVa 140 (枚举排列) Bandwidth
题意较复杂,请参见原题=_=|| 没什么好说的,直接枚举每个排列就好了,然后记录最小带宽,以及对应的最佳排列. STL里的next_permutation函数真是好用. 比较蛋疼的就是题目的输入了.. ...
- UVA 140 Bandwidth
题意: 给出一个n个节点的图G,和一个节点的排列,定义节点i的带宽为i和相邻节点在排列中的最远距离,而所有带宽的最大值就是图的带宽,求让图的带宽最小的排列. 分析: 列出所有可能的排列,记录当前找到的 ...
- UVA 140 Bandwidth (dfs 剪枝 映射)
题意: 给定一个n个结点的图G和一个结点的排列, 定义结点i的带宽b(i)为i和相邻结点在排列中的最远距离, 所有b(i)的最大值就是这个图的带宽, 给定G, 求让带宽最小的结点排列. 给定的图 n ...
- UVa 140 Bandwidth【枚举排列】
题意:给出n个节点的图,和一个节点的排列,定义节点i的带宽b[i]为i和其相邻节点在排列中的最远的距离,所有的b[i]的最大值为这个图的带宽,给一个图,求出带宽最小的节点排列 看的紫书,紫书上说得很详 ...
- UVA 140 (13.07.29)
Bandwidth Given a graph (V,E) where V is a set of nodes and E is a set of arcsin VxV, and anorderi ...
- uva 140
思路:暴力+剪枝 uva140 wa了好多次……数组开小了……!!! #include <iostream> #include <cstdio> #include <cm ...
随机推荐
- Python3学习之路~2.4 字典操作
字典一种key - value 的数据类型,使用就像我们上学用的字典,通过笔划.字母来查对应页的详细内容. 定义字典(dictionary) info = { 'stu1101': "Amy ...
- 最简单的css实现页面宽度自适应
<div class="rxs"> <div class="rxleft"> 第一段内容,可以是任何html标签 </div> ...
- (转)Mysql 多表查询详解
MySQL 多表查询详解 一.前言 二.示例 三.注意事项 一.前言 上篇讲到mysql中关键字执行的顺序,只涉及了一张表:实际应用大部分情况下,查询语句都会涉及到多张表格 : 1.1 多表连接有 ...
- PAT 1097 Deduplication on a Linked List[比较]
1097 Deduplication on a Linked List(25 分) Given a singly linked list L with integer keys, you are su ...
- testNG入门详解
TestNG 的注释: @DataProvider @ExpectedExceptions @Factory @Test @Parameters <suite name="Parame ...
- iOS UI基础-2.0按钮操作与形变
按钮状态 1.normal:默认状态 Default 对应的枚举常量:UIControlStateNormal 2.highlighted(高亮状态) 按钮被按下去的时候(未松开) 对应的枚举常量 ...
- Leetcode: Repeated DNA Sequence
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- liferay中如何实现自己定义的方法
大家看到这篇文章是不是很开心啊,我感觉是很开心,我们终于可以按照自己的意愿来写一次代码,在liferay中一些基本的增删改查的代码是自动生成的,然而我们想要实现自己的方法的话,恐怕要费一点劲,你要知道 ...
- c# 获取某个进程的CPU使用百分百(类似任务管理器中显示CPU)
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...
- bootstrap 带有确定取消按钮的modal
</div><div class="modal fade" id="confirmModal" tabindex="-1" ...