题意:给出一个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 带宽的更多相关文章

  1. UVA - 140 Bandwidth(带宽)(全排列)

    题意:给定图,求是带宽最小的结点排列. 分析:结点数最多为8,全排列即可.顶点范围是A~Z. #pragma comment(linker, "/STACK:102400000, 10240 ...

  2. UVA 140 Brandwidth 带宽 (dfs回溯)

    看到next_permutation好像也能过╮(╯▽╰)╭ 这题学习点: 1.建图做映射 2.通过定序枚举保证字典序最小 3.strtok,sscanf,strchr等函数又复习了一遍,尽管程序中没 ...

  3. 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 ...

  4. UVa 140 (枚举排列) Bandwidth

    题意较复杂,请参见原题=_=|| 没什么好说的,直接枚举每个排列就好了,然后记录最小带宽,以及对应的最佳排列. STL里的next_permutation函数真是好用. 比较蛋疼的就是题目的输入了.. ...

  5. UVA 140 Bandwidth

    题意: 给出一个n个节点的图G,和一个节点的排列,定义节点i的带宽为i和相邻节点在排列中的最远距离,而所有带宽的最大值就是图的带宽,求让图的带宽最小的排列. 分析: 列出所有可能的排列,记录当前找到的 ...

  6. UVA 140 Bandwidth (dfs 剪枝 映射)

    题意: 给定一个n个结点的图G和一个结点的排列, 定义结点i的带宽b(i)为i和相邻结点在排列中的最远距离, 所有b(i)的最大值就是这个图的带宽, 给定G, 求让带宽最小的结点排列. 给定的图 n ...

  7. UVa 140 Bandwidth【枚举排列】

    题意:给出n个节点的图,和一个节点的排列,定义节点i的带宽b[i]为i和其相邻节点在排列中的最远的距离,所有的b[i]的最大值为这个图的带宽,给一个图,求出带宽最小的节点排列 看的紫书,紫书上说得很详 ...

  8. 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 ...

  9. uva 140

    思路:暴力+剪枝 uva140 wa了好多次……数组开小了……!!! #include <iostream> #include <cstdio> #include <cm ...

随机推荐

  1. C和C++不容易发现的区别

    1.char指针指向字符串常量 当下面的代码写到.c文件中时,可以正常运行;而写到.cpp文件中就会报错:无法从“const char [6]”转换为“char *”. char * c = &quo ...

  2. Hadoop DistributedCache分布式缓存的使用

    做项目的时候遇到一个问题,在Mapper和Reducer方法中处理目标数据时,先要去检索和匹配一个已存在的标签库,再对所处理的字段打标签.因为标签库不是很大,没必要用HBase.我的实现方法是把标签库 ...

  3. [py]处理文件的3个方法

    file处理的3个方法: f和f.readlines效果一样 # f.read() 所有行 -> 字符串 # f.readline 读取一行 -> 字符串 # f.readlines 所有 ...

  4. git 生成patch 和打入patch

    转载:https://blog.csdn.net/liuhaomatou/article/details/54410361 平时我们在使用git 管理项目的时候,会遇到这样一种情况,那就是客户使用gi ...

  5. Flask中'endpoint'(端点)的理解

    翻译整理自Stack Overflow:http://stackoverflow.com/questions/19261833/what-is-an-endpoint-in-flask 原文中用到了m ...

  6. Centos 设置zookeeper开机自启动

    把zookeeper做成服务 1.进入到/etc/rc.d/init.d目录下,新建一个zookeeper脚本 [root@zookeeper ~]# cd /etc/rc.d/init.d/ [ro ...

  7. Hardwood Species(stl map)

    http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=203#problem/B 属于暴力 #include <stdio.h&g ...

  8. PAT 1022 Digital Library[map使用]

    1022 Digital Library (30)(30 分) A Digital Library contains millions of books, stored according to th ...

  9. [ps] 灰度和通道基础知识

    灰度.灰度值.灰度图像 灰度:灰度使用黑色调来表示物体,即用黑色为基准色,不同饱和度的黑色来显示图像.每个灰度对象都具有从0%(白色)到100%(黑色)的亮度值.使用黑白或灰度扫描仪生成的图像通常以灰 ...

  10. Python基础socket编程

    Python 提供了两个基本的 socket 模块. 第一个是 Socket,它提供了标准的 BSD Sockets API. 第二个是 SocketServer, 它提供了服务器中心类,可以简化网络 ...