uva140-Bandwidth

刷刘汝佳的时候半天没看懂题,看了大神题解:http://blog.csdn.net/keshuai19940722/article/details/9752217

题意大致明白了,为了加深理解,特意给代码加上注释(虽然大神已经写的浅显易懂orz)。

Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

Status

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

#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <cstdio>
#include <stack>
#include <cmath>
#include <queue>
#include <map>
#include <set>
using namespace std;
#define N 1005
#define M 30
#define INF 0x3f3f3f3f;

int Min,g[M][M],bir[M],son[N],n,num[N],rec[N];
char str[N];

bool judge(int u,int v){//判断v是否与u相连
    for (int i=0; i<son[u]; i++) {
        if (v==g[u][i]) {
            return false;
        }
    }
    return true;
}

int find(int k){
    for (int i=0; i<n; i++) {//找到该点在num数组中的位置,num数组用于存储字母。
        if (num[i]==k) {
            return i;
        }
    }
    return 0;
}

void handle(){//此函数是将输入转化进数组中
    char f=str[0],s;
    bir[f-'A']=1;
    int len=strlen(str);
    for (int i=1; i<len; i++) {
        if (str[i]==';') {
            f=str[++i]; bir[f-'A']=1;
        }
        else if (str[i]==':') continue;
        else {
            s=str[i];
            int a=f-'A',b=s-'A';
            if (judge(a, b)) {
                g[a][son[a]++] = b;
                g[b][son[b]++] = a;
            }
            bir[b]=1;
        }
    }
}

void count(){
    int Max=0;
    for (int i=0; i<n; i++) {//对每个点分别计算
        for (int j=0; j<son[num[i]]; j++) {
            int t=abs(find(g[num[i]][j])-i);//对于相连接的点都计算到该点的距离,找出最大值。
            if (Max<t)
                Max=t;
        }
    }
    if (Max<Min) {//每次找出最大值都与之前所找的相比较,若更小,则作为相应地新样本。
        memcpy(rec, num, sizeof(num));
        Min=Max;
    }
}

int main(){
    while (cin>>str) {
        if (str[0]=='#') {
            break;
        }
        memset(g, 0, sizeof(g));
        memset(bir, 0, sizeof(bir));
        memset(num, 0, sizeof(num));
        memset(rec, 0, sizeof(rec));
        memset(son, 0, sizeof(son));
        Min=INF;
        n=0;
        
        handle();
        for (int i=0; i<M; i++)
            if (bir[i])
                num[n++]=i;
        do {
            count();
        } while (next_permutation(num, num+n));//对每个排列都进行计算,找出最小值。
        for (int i=0; i<n; i++) {
            printf("%c ",'A'+rec[i]);
        }
        printf("-> %d\n",Min);
        
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值