uva140 - Bandwidth

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:

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

 

题意:给一个最多8个结点的无向图,把结点重排后对于图中每条边(u,v),u和v在排列中的最大距离称为该排列的带宽。求带宽最小的排列
算法:枚举全排列。需要注意的是本题的输入格式相对麻烦一点,需要仔细应对

学习点:

1. id和letter的映射关系处理

2. strtok函数使用方法

3.for(int i=0;i<n;i++) pos[P[i]]=i; 确认排列中元素位置

 

复制代码
#include<cstdio>
#include<cstring>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=10;
int id[256], letter[maxn];
int n;

void map_id_letter(char *p) 
{
    n=0;
    for(int c='A'; c<='Z'; c++) if(strchr(p, c)) 
    {
        id[c]=n;
        letter[n]=c;
        n++;
    }
}

int main()
{
    char buff[256];
    while(gets(buff) && buff[0]!='#')
    {
        map_id_letter(buff);
        char*p=strtok(buff, ";");
        vector<int> u,v;
        while(p)
        {
            //printf("%s\n", p);
            int t=p[0];
            ++p;//跳过:
            while(*(++p))
            {
                u.push_back(id[t]);
                v.push_back(id[*p]);
            }
            p=strtok(0, ";");
        }

        int ans=n;
        int P[maxn], bestP[maxn], pos[maxn];
        for(int i=0;i<n;i++) P[i]=i;
        do
        {
            for(int i=0;i<n;i++) pos[P[i]]=i;
            int bandwidth=0;
            for(int i=0;i<u.size();i++)
            {
                bandwidth=max(bandwidth, abs(pos[u[i]] - pos[v[i]]));
            }
            if(bandwidth<ans)
            {
                ans=bandwidth;
                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;
}
复制代码

 

回溯加剪枝方法,更快:

复制代码
#include<cstdio>
#include<cstring>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=10;
int id[256], letter[maxn];
int n;

void map_id_letter(char *p) 
{
    n=0;
    for(int c='A'; c<='Z'; c++) if(strchr(p, c)) 
    {
        id[c]=n;
        letter[n]=c;
        n++;
    }
}

int vis[maxn];
int ans;
int P[maxn], bestP[maxn], pos[maxn];
int edge[27][27];

//生成排列
void gen(int d, int bw)
{
    if(d==n)
    {
        ans=bw;
        memcpy(bestP, P, sizeof(P));
        return;
    }

    for(int i=0;i<n;i++)
    {
        if(!vis[i])
        {
            vis[i]=1;
            P[d]=i;
            //如果新加的节点带宽大于现有带宽,剪枝
            int w=0;//新加节点带宽
            for(int j=0;j<d;j++)
            {
                if(edge[i][P[j]])
                {
                    w=d-j;            
                    break;
                }
            }
            int cur_bw=max(bw, w);
            if(cur_bw<ans)
                gen(d+1, cur_bw);
            vis[i]=0;
        }
    }
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("./uva140.in", "r", stdin);
#endif
    char buff[256];
    while(gets(buff) && buff[0]!='#')
    {
        map_id_letter(buff);
        char*p=strtok(buff, ";");
        memset(edge, 0, sizeof(edge));
        while(p)
        {
            int t=p[0];
            ++p;//跳过:
            while(*(++p))
            {
                edge[id[t]][id[*p]]=1;
                edge[id[*p]][id[t]]=1;
            }
            p=strtok(0, ";");
        }

        ans=n;
        gen(0, 0);

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

posted on   katago  阅读(1959)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
历史上的今天:
2011-06-24 essential linux device drivers ch10
2011-06-24 linux的ioremap
2011-06-24 qemu-launcher:图形化的QEMU启动器
2011-06-24 双网卡服务器2个IP不能在同一网段
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示