24h購物| | PChome| 登入
2012-07-15 07:46:46| 人氣1,666| 回應0 | 上一篇 | 下一篇

[UVA][dfs, 窮舉] 140 - Bandwidth

推薦 0 收藏 0 轉貼0 訂閱站台


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

picture25

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

picture47

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


把全排列列出來, 去找最大寬就行了

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int map[26][26] = {};
int app[26] = {}, n;
int set[26] = {}, tmp[26];
int way[26] = {}, used[26] = {};
int ans;
int dfs(int idx) {
    if(idx == n) {
        int i, j;
        int bandwidth = 0;
        for(i = 0; i < n; i++) {
            for(j = 0; j < 26; j++) {
                if(map[app[i]][j]) {
                    if(abs(set[app[i]] - set[j]) > bandwidth)
                        bandwidth = abs(set[app[i]] - set[j]);
                }
            }
        }
        if(bandwidth < ans) {
            ans = bandwidth;
            memcpy(way, tmp, sizeof(way));
        }
        return 0;
    }
    int i;
    for(i = 0; i < n; i++) {
        if(used[i] == 0) {
            used[i] = 1;
            tmp[idx] = app[i];
            set[app[i]] = idx;
            dfs(idx+1);
            used[i] = 0;
        }
    }
}
int main() {
    char s[100];
    int i, j, st, ed;
    while(scanf("%s", s) == 1) {
        if(!strcmp(s, "#"))
            break;
        memset(map, 0, sizeof(map));
        int len = strlen(s), used[26] = {};
        n = 0;
        for(i = 0; i < len; i++) {
            st = s[i]-'A';
            if(used[st] == 0) {
                used[st] = 1;
                app[n++] = st;
            }
            j = i+2;
            while(s[j] != ';' && s[j] != '\0') {
                ed = s[j]-'A';
                map[st][ed] = 1;
                if(used[ed] == 0) {
                    used[ed] = 1;
                    app[n++] = ed;
                }
                j++;
            }
            i = j;
        }
        std::sort(app, app+n);
        ans = 0xffff;
        dfs(0);
        for(i = 0; i < n; i++)
            printf("%c ", way[i]+'A');
        printf("-> %d\n", ans);
    }
    return 0;
}

台長: Morris
人氣(1,666) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 213 - Message Decoding
此分類上一篇:[UVA] 10693 - Traffic Volume

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文