UVa - 140 - Bandwidth
2015-06-25 18:37
351 查看
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.
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.
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.
直接枚举全排列,计算带宽,记录最小的情况,如果有一样的,取字典序小的就可以了。生成排列的时候用了STL函数next_permutation。
AC代码:
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 seriesof 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
直接枚举全排列,计算带宽,记录最小的情况,如果有一样的,取字典序小的就可以了。生成排列的时候用了STL函数next_permutation。
AC代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cctype> #include <cstring> #include <string> #include <sstream> #include <vector> #include <set> #include <map> #include <algorithm> #include <stack> #include <queue> #include <bitset> #include <cassert> #include <cmath> #include <functional> using namespace std; const int maxn = 10; int id[256], letter[maxn]; char input[1000]; int n; void init() // 计算结点的个数并给字母编号 { n = 0; for (char ch = 'A'; ch <= 'Z'; ch++) { if (strchr(input, ch) != NULL) { id[ch] = n++; letter[id[ch]] = ch; } } } // 处理输入 vector <int> u, v; void readInput() { int len = strlen(input); int p = 0, q = 0; u.clear(); v.clear(); while (1) { while (p < len && input[p] != ':') { p++; } if (p == len) { break; } while (q < len && input[q] != ';') { q++; } for (int i = p + 1; i < q; i++) { u.push_back(id[input[p - 1]]); v.push_back(id[input[i]]); } p++; q++; } } // 枚举全排列 void solve() { int P[maxn], bestP[maxn], pos[maxn], ans = n; 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++) { cout << (char)letter[bestP[i]] << ' '; } cout << "-> " << ans << endl; } int main() { while (cin >> input && input[0] != '#') { init(); readInput(); solve(); } return 0; }
相关文章推荐
- UVa - 140 - Bandwidth
- SharedPreferences详解
- 微店产品分析
- Dart基础-函数
- error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
- 卸载oracle 11g grid软件
- [工作积累] android 中添加libssl和libcurl
- Linux平台下Java调用C函数
- c++ std::string 用法
- OpenSSH服务及其相关应用
- form 单选按钮
- C++_利用栈进行运算符表达式的运算
- Android 内存管理 &Memory Leak & OOM 分析
- volley 应用 GET POST请求 图片异步加载
- Linux查看Dell服务器型号
- Linux OS Service 'ntpd' (文档 ID 551704.1)
- Android事件分发机制完全解析,带你从源码的角度彻底理解(下)
- Java 强引用、软引用、弱引用、虚引用
- Linux使用快捷键,who命令,rm命令,ps命令,cd,命令kill命令,find命令,grep命令,tar命令(gz、tar、bz2),用户管理,vim配置的一部分,相关命令
- 创建数据透视表数据包含合并单元格