uva 140 Bandwidth (全排列+暴力枚举)
来源:互联网 发布:照片视频软件 编辑:程序博客网 时间:2024/05/30 05:25
uva 140 Bandwidth
Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and anordering 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
题目大意:给出一些点,以及所有必须相连的两点,然后每个排序中,找出必须连接的距离最大值,然后在所有序列中找出连接所需最短的。
解题思路:先将节点关系构成一张表,然后根据表进行全排列进行枚举(利用next_permutation函数),找出最小带宽。
#include<stdio.h>#include<string.h>#include<algorithm>#include<math.h>using namespace std;char ch[10], ch2[10];int A[30][30], Max, Min, a[26];int find(char a) {for (int i = 0; i < 10; i++) {if (ch[i] == a) return i;}}void getMin() { //找出给排列情况的带宽int temp1, temp2, num;for (int i = 0; i < 26; i++) {for (int j = 0; j < 26; j++) {if (A[i][j]) {temp1 = find(i + 'A');temp2 = find(j + 'A');num = abs(temp1 - temp2);if (Max < num) {Max = num;}}}}}int main() {char str[100];while (scanf("%s", str) == 1 && strcmp(str, "#") != 0) {memset(A, 0, sizeof(A));memset(a, 0, sizeof(a));int len = strlen(str);int cnt1, cnt2 = 0, flag = 1;for (int i = 0; i < len; i++) { //构成一张关系表if (str[i] >= 'A' && str[i] <= 'Z') {a[str[i] - 'A']++;if (flag) {cnt1 = str[i] - 'A';}else {A[cnt1][str[i] - 'A'] = 1;}}else if (str[i] == ':') flag = 0;else if (str[i] == ';') flag = 1;}memset(ch, 0, sizeof(ch));memset(ch2, 0, sizeof(ch2));for (int i = 0; i < 26; i++) {//找出出现过的节点字母编号if (a[i] != 0) ch[cnt2++] = i + 'A';}Min = 10;sort(ch, ch + strlen(ch));//排序,为全排列做准备do{ //全排列找出最小带宽Max = 0;getMin();if (Min > Max) {strcpy(ch2, ch);Min = Max;}}while (next_permutation(ch, ch + strlen(ch)));for (int i = 0; i < strlen(ch2); i++) {printf("%c ", ch2[i]);}printf("-> %d\n", Min);}return 0;}
- uva 140 Bandwidth (全排列+暴力枚举)
- UVA - 140 Bandwidth(全排列枚举)
- UVA 140 - Bandwidth 暴力 全排列 回溯
- UVa 140 Bandwidth (枚举全排列&剪枝搜索)
- 例题 7-6 UVA - 140 Bandwidth 带宽 (全排列暴力)
- UVA 140 带宽 Bandwidth (暴力枚举+剪枝)
- UVa 140 - BandWidth (暴力)
- UVa 140:Bandwidth(暴力)
- UVa 140 - Bandwidth(全排列+回溯剪枝)
- uva 140 Bandwidth(全排列+递归)
- UVA 140 全排列+暴力
- UVa 140 - BandWidth 解题报告(暴力)
- UVA 140——Bandwidth(暴力)
- UVA 140 Bandwidth 暴力模拟
- UVa 820 - Bandwidth 全排列+判断
- uv216 暴力枚举全排列
- UVA140 - Bandwidth (暴力dfs+排列+剪枝)
- uva - 140 - Bandwidth(模拟 + 下一个排列 + 图)
- 2015.1.27实验室日记
- PHP学习笔记之全局变量演示
- 中国剩余定理(笔记)
- 前端编码规范总结:HTML
- 逻辑推理与判断(谜语博士的难题(1))
- uva 140 Bandwidth (全排列+暴力枚举)
- mysql 5.6.14 win7 32位免安装版配置
- MySql存储过程
- DP之背包学习记录(一)
- 【 D3.js 选择集与数据详解 — 4 】 enter和exit的处理方法以及处理模板
- 南邮1004:线性表操作
- 百度敏捷之旅
- Java常用类库..【Pnoter】
- 通过阅读、分析和翻译二进制格式的Java Class文件学习Java Class的技术