UVa 140 Bandwidth(DFS 回溯 剪枝)

题意  有一个无向图  对于其所有顶点的一个排列 称一顶点与所有与其有边的其它顶点在排列中下标差的最大值为这个顶点的带宽   而所有顶点带宽的最大值为这个排列的带宽   求这个图带宽最小的排列

枚举排列  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

时间: 2024-10-22 22:31:03

UVa 140 Bandwidth(DFS 回溯 剪枝)的相关文章

[2016-02-20][UVA][140][Bandwidth]

UVA - 140 Bandwidth Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description 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 

uva 140 Bandwidth (全排列+暴力枚举)

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

uva 307 Sticks(回溯剪枝)

uva 307 Sticks George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the original state, but he forgot how many sticks he had originally and how long they were origi

uva 140 bandwidth (好题) ——yhx

 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 con

hdu 5113 Black And White (dfs回溯+剪枝)

Black And White Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 854    Accepted Submission(s): 218 Special Judge Problem Description In mathematics, the four color theorem, or the four color

UVA-140 Bandwidth (回溯+剪枝)

题目大意:求一个使带宽最小的排列和最小带宽.带宽是指一个字母到其相邻字母的距离最大值. 题目分析:在递归生成全排列的过程中剪枝,剪枝方案还是两个.一.当前解不如最优解优时,减去:二.预测的理想解不必最优解优时,减去.将与当前最后一个位置上的字母相邻的字母全部接过来,便得理想解. 代码如下: # include<iostream> # include<cstdio> # include<string> # include<vector> # include&l

uva 140 Bandwidth(全排列+递归)

快睡觉的时候1A的把序列全排列,递归暴力判断就ok啦,我改成对应的整数存了,a数组存的是所有的字符的排列, b数组存的是所有开始节点的排列,map[i][j]数组存的是i为起点,与j相邻 贴代码: #include<stdio.h> #include<stdlib.h> #include<string.h> #include<limits.h> #include<math.h> #include<algorithm> using na

UVA - 140 Bandwidth(带宽)(全排列)

题意:给定图,求是带宽最小的结点排列. 分析:结点数最多为8,全排列即可.顶点范围是A~Z. #pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #i

uva 11218 KTV(DFS+回溯)

uva 11218 KTV One song is extremely popular recently, so you and your friends decided to sing it in KTV. The song has 3 characters, so exactly 3 people should sing together each time (yes, there are 3 microphones in the room). There are exactly 9 peo