UVA-140 Bandwidth (枚举排列 next_permutation)

UVA-140 Bandwidth (枚举排列: next_permutation)

Given a graph (V, E) where V is a set of nodes and E is a set of arcs in
V ×V , 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 graph on the right:
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

寻找带宽最小的排列,带宽:max(头(i)和尾的最大距离),例如上面的例子,A这个头有两个尾:F B,在 A B C F G D H E排列中,A离B距离为1,离F距离为3

思路:用 next_permutation 枚举排列,检查带宽,更新答案,检查的时候可以剪枝,当前带宽已经>= ans, 直接return,不用往下检查了。

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

int ans,t;
vector<int> g[30];
int turn[10], ans_arr[10], cnt;
char s[1000];

bool find(int tail){
    for(int i = 0; i < cnt; i++)
        if(turn[i] == tail)
            return true;
    return false;
}
void init(){
    cnt = 0;//cnt 为头的个数
    int flag = 1, head;//flag == 1,找头, flag == 0,续尾。
    for(int i = 0; i < 30; i++) g[i].clear();
    for(int i = 0; i < strlen(s); i++){
        if(s[i] == ' ') continue;
        else if(s[i] == ';') flag = 1;
        else if(s[i] == ':') flag = 0;
        else//字母
            if(flag) {
                head = s[i]-'A';
//            printf("head:%c\n",s[i]);
                if(!find(head)) turn[cnt++] = head;//把出现的头都加入turn
            }else{
                int tail = s[i]-'A';
                g[head].push_back(tail);
                if(!find(tail)) turn[cnt++] = tail;
            }
    }
}

void update(){
    for(int i = 0; i < cnt; i++){
        int head = turn[i];
        for(int j = 0; j < g[head].size(); j++){
            int target = g[head][j];
            for(int k = 0; k < cnt; k++){
                if(turn[k] == target)
                    t = max(t, abs(k-i));
                    if(t >= ans) return;//剪枝
            }
        }
    }
}
int main(){
    while(scanf("%[^'\n']",s) && s[0] != '#'){
        getchar();
        init();
        sort(turn, turn+cnt);
        ans = 99999, t = 0;
//    for(int i = 0; i < cnt; i++) printf("%c ",turn[i]+'A');
        do{
            t = 0;
            update();
//        for(int i = 0; i < cnt; i++) printf("%c ",turn[i]+'A');
            if(t < ans){
                ans = t;
                for(int i = 0; i < cnt; i++)
                    ans_arr[i] = turn[i];
            }
        }while(next_permutation(turn,turn+cnt));
        for(int i = 0; i < cnt; i++)
            printf("%c ",ans_arr[i]+'A');
        printf("-> %d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值