码迷,mamicode.com
首页 > 其他好文 > 详细

UVA - 140

时间:2014-11-26 20:54:01      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   ar   color   os   sp   

 Bandwidth 

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 bandwidthof 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:

 

bubuko.com,布布扣

 

This can be ordered in many ways, two of which are illustrated below:

 

bubuko.com,布布扣

 

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

bubuko.com,布布扣
 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <set>
 5 #include <vector>
 6 
 7 using namespace std;
 8 
 9 typedef vector <int> VINT;
10 typedef set <char> SINT;
11 int G[30][30];
12 SINT s;
13 int mini;
14 
15 void Read(const char* str) {
16     int len = strlen(str);
17     int p = strchr(str,:) - str;
18     int j = 0;
19     for (int i = 0;i < p;i++) {
20         s.insert(str[i]);
21         for (j = p + 1;j < len && str[j] != ;;j++) {
22             G[str[i] - A][str[j] - A] = 1;
23             G[str[j] - A][str[i] - A] = 1;
24             s.insert(str[j]);
25         }
26     }
27     if (j != len) Read(str + j + 1);
28 }
29 
30 VINT v;
31 int len;
32 
33 void dfs(int* a,int cur,int degree) {
34     if (degree >= mini) return;
35     if (cur == len) {
36         mini = degree;
37         v.clear();
38         for (int i = 0;i < len;i++) {
39             v.push_back(a[i]);
40         }
41         return;
42     }
43     else {
44         for (SINT::iterator Iter = s.begin();Iter != s.end();Iter++) {
45             int ok = 1;
46             a[cur] = *Iter;
47             for (int i = 0;i < cur;i++) {
48                 if (a[i] == *Iter) {
49                     ok = 0;
50                     break;
51                 }
52             }
53             if (ok) {
54                 for (int i = 0;i <= cur;i++) {
55                     if (G[a[i] - A][*Iter - A]) {
56                         degree = max(degree,cur - i);
57                     }
58                 }
59                 dfs(a,cur + 1,degree);
60             }
61         }
62     }
63 }
64 
65 int main () {
66     // freopen("1.in","r",stdin);
67     string str;
68     while (cin >> str && str != "#") {
69         memset(G,0,sizeof(G));
70         s.clear();
71         v.clear();
72         Read(str.c_str());
73         mini = 30;
74         int a[30];
75         memset(a,0,sizeof(a));
76         len = s.size();
77         dfs(a,0,0);
78         for (int i = 0;i < len;i++) {
79             cout << char(v[i]) << " ";
80         }
81         cout << "-> " << mini << endl;
82     }
83 }
View Code

 

UVA - 140

标签:des   style   blog   http   io   ar   color   os   sp   

原文地址:http://www.cnblogs.com/xiaoshanshan/p/4124733.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!