UVA - 140 Bandwidth

Description

Download as PDF

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


分析:对输入的处理:映射节点为编号,把点和点的连接关系放在两个vector中;

第一种方法是枚举所有序列,找到最小带宽


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;

int id[256];
int letter[10];

int main()
{
	char buf[300];
	while(scanf("%s", buf)&&buf[0]!='#') {
        int n = 0;
        for(int i='A'; i<='Z'; i++) {
			if(strchr(buf, i)) {
                id[i] = n; letter[n] = i; n++;
			}
        }
        char* p = strtok(buf, ";");
        vector<int> u,v;
        while(p) {
            char t = *p++;
            while(*(++p)) {
                u.push_back(id[t]);
                v.push_back(id[*p]);
            }
            p = strtok(NULL, ";");
        }

        int bestP[10], P[10], pos[10], 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 bw = 0;
            for(int i=0; i<v.size(); i++) {
                bw = max(bw, abs(pos[u[i]]-pos[v[i]]));
            }
            if(bw < ans) {
                ans = bw;
                memcpy(bestP, P, sizeof(P));
            }
        }while(next_permutation(P, P+n));

        for(int i=0; i<n; i++) {
			printf("%c ", letter[bestP[i]]);
        }
        printf("-> %d\n", ans);
	}
    return 0;
}

第二种方法是回溯生成序列,类似八皇后,但是注意到所有的序列都是合法,我们要找到那些满足条件的,或者说去掉那些明显不满足当前状态的,比如现在的最小带宽是w,如果我们算出某两点带宽大于w,则可以肯定这个序列不满足条件;这种做法一般称作剪枝。


#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int id[256], letter[10]; //id:节点到编号  letter:编号到节点
int bestP[10], P[10];   //bestP:最优序列  P:当前序列
int edge[10][10], n, ans;   //edge:邻接矩阵 n:节点个数 ans:最小带宽
int visit[10];  //visit:标识节点是否访问

void gen(int cur, int bw) //cur:当前放置节点位置  bw:当前最小带宽
{
    if(cur == n) {  //如果节点已经放完则找到最优解
        ans = bw;
        memcpy(bestP, P, sizeof(P));
        return;
    }
    for(int i=0; i<n; i++) { //i表示节点编号
        if(!visit[i]) {
            visit[i] = 1;
            P[cur] = i; //在cur处放置i节点
            int cur_w = 0; //i节点和前面节点的最大带宽
            for(int j=0; j<cur; j++) {
                if(edge[i][P[j]]) {
                    cur_w = cur - j; //如果找到某个节点和i节点相连,则两点位置差即带宽
                    break;
                }
            }
            cur_w = max(bw, cur_w); //当前最大带宽
            if(cur_w < ans) { //如果放i后的带宽最小带宽小则继续放
                gen(cur+1, cur_w);
            }
            visit[i] = 0; //回溯记得还原标志位
        }
    }
}

int main()
{
    char str[256];
    while(~scanf("%s",str)&&str[0]!='#') {
        n = 0;
        for(int i='A'; i<='Z'; i++) {
            if(strchr(str, i)) { //分别对节点进行编号
                id[i] = n; letter[n] = i; n++;
            }
        }
        char* p = strtok(str, ";");
        memset(edge, 0, sizeof(edge));
        while(p) {
            char t = *p++;
            while(*(++p)) { //做出邻接矩阵
                edge[id[t]][id[*p]] = 1;
                edge[id[*p]][id[t]] = 1;
            }
            p = strtok(NULL, ";");
        }

        ans = n;
        memset(visit, 0, sizeof(visit));
        gen(0, 0);

        for(int i=0; i<n; i++) {
            printf("%c ", letter[bestP[i]]);
        }
        printf("-> %d\n", ans);
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值