UVa 140 Bandwidth(DFS 回溯 剪枝),bandwidthdfs

來源:互聯網
上載者:User

UVa 140 Bandwidth(DFS 回溯 剪枝),bandwidthdfs

題意  有一個無向圖  對於其所有頂點的一個排列 稱一頂點與所有與其有邊的其它頂點在排列中下標差的最大值為這個頂點的頻寬   而所有頂點頻寬的最大值為這個排列的頻寬   求這個圖頻寬最小的排列

枚舉排列  ans記錄當前找到的最小頻寬  枚舉過程中一旦出現頻寬大於ans的也就不用再擴充了  這樣枚舉完就得到了答案

#include<cstdio>#include<cstring>using namespace std;const int N = 50;int n, ans, h[N], vis[N], a[N], d[N][N], p[N];void dfs(int cur, int band){    if(cur >= n && band < ans)    {        ans = band;        for(int i = 0; i < cur; ++i)            p[i] = a[i];//p數組記錄最優解路徑    }    for(int i = 0; cur < n && i < 26; ++i)    {        if(vis[i] || !h[i]) continue;        a[cur] = i , vis[i] = 1;        for(int j = 0; j < cur; ++j)            if(d[i][a[j]] && band < cur - j) band = cur - j;        if(band > ans)        {            vis[i] = 0;            return;        }        dfs(cur + 1, band);        vis[i] = 0;    }}int main(){    char s[N];    while(scanf("%s", s), strcmp(s, "#"))    {        memset(d, 0, sizeof(d));        memset(h, 0, sizeof(h));        //h數組記錄字母'A'+i是否在圖中出現  n為頂點個數        int i = n = 0, u, v;        while(s[i])        {            u = s[i] - 'A';            if(!h[u]) h[u] = ++n;            i += 2;            while(s[i] && s[i] != ';')            {                v = s[i++] - 'A';                if(!h[v]) h[v] = ++n;                d[u][v] = d[v][u] =  1;            }            if(!s[i++]) break;        }        ans = N;        memset(vis, 0, sizeof(vis));        dfs(0, 0);        for(int i = 0; i < n; ++i)            printf("%c ", 'A' + p[i]);        printf("-> %d\n", ans);    }    return 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:

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


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.