uva 140 Bandwidth

来源:互联网 发布:淘宝刷单是什么 编辑:程序博客网 时间:2024/05/16 08:51

原题:
Giv en a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an or dering on
the elemen ts 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:
这里写图片描述
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 b y 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 b y ‘;’. Each record will consist of a node name (a single upper case character in the the range ‘A’ to ‘Z’), follow ed 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 follow ed 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:G C; F:A GH; E: HD
#
Sample output
A B C F G D H E -> 3
中文:
首先给你一个图,每个节点上是一个大写字母。现在让你找一条路径,这条路径按照图的连通性连接,在你找到的这条路径当中,每个节点在和它连通的节点在路径中的最长距离定义为这个节点的带宽。而所有节点带宽当中最大的那个则定义为这条路径的带宽。现在让你找一条路径,使得这个路径的带宽最小。

#include <bits/stdc++.h>using namespace std;string s;set<char> sc;int G[27][27];vector<int> seq;int cnt,ans,tmp[27];bool vis[27],used[27];void dfs(int x,int band){    if(x==cnt&&band<ans)    {        ans=band;        seq.clear();        for(int i=0;i<cnt;i++)            seq.push_back(tmp[i]);    }    for(int i=0;i<26;i++)    {        if(!vis[i]&&used[i])        {            tmp[x]=i;            vis[i]=true;            int mark=0;            for(int j=0;j<x;j++)                if(G[i][tmp[j]])                {                    if(band<x-j)                    band=x-j;                    mark++;                }            if(band>ans||mark>ans)//剪枝            {                vis[i]=false;                return;            }            dfs(x+1,band);            vis[i]=0;        }    }}int main(){    ios::sync_with_stdio(false);    while(cin>>s)    {        if(s=="#")            break;        memset(G,0,sizeof(G));        sc.clear();        memset(vis,false,sizeof(vis));        memset(used,false,sizeof(used));        seq.clear();        for(int i=0;i<s.size();)        {            int j;            if(s[i]!=':'&&s[i]!=';')            {                sc.insert(s[i]);                for(j=i+2;s[j]!=';'&&j<s.size();j++)                {                    G[s[i]-'A'][s[j]-'A']=1;                    G[s[j]-'A'][s[i]-'A']=1;                    sc.insert(s[j]);                }            }            i=j+1;        }        cnt=sc.size();        for(auto it=sc.begin();it!=sc.end();it++)            used[(*it)-'A']=true;/*      for(int i=0;i<cnt;i++)        {            cout<<(char)(i+'A')<<": ";            for(int j=0;j<cnt;j++)                if(G[i][j])                cout<<(char)(j+'A')<<" ";            cout<<endl;        }*/        ans=INT_MAX;        dfs(0,0);        for(int i=0;i<cnt;i++)            cout<<(char)(seq[i]+'A')<<" ";        cout<<"-> "<<ans<<endl;    }    return 0;}

解答:
紫书上的例题,节点很少,可以直接枚举排列,也可以暴力搜索。但是,这样做的就没什么意思了。可以想到用剪枝的方法。
这里在搜索最大值,最小值的过程中,保存上次搜索到的最佳结果。再下次搜索过程中,如果发现搜索出来的结果不可能比上次的结果更有,就不必搜索
本题剪枝的策略也是如此,保存已经搜索出来的一条路径,带宽是ans。下次再从头搜索的时候,如果发现搜索过程中出现了大于ans的带宽,就剪掉。
第二种剪枝方式,如果搜索到当前节点u,u有m个相邻的节点,如果那么此时对于u节点的带宽最好情况就是u后面连着m个节点,也就是u的带宽是m。如果连m都大于之前找到的路径带宽ans,则剪掉。

0 0
原创粉丝点击