uva 140 bandwidth (好题) ——yhx

 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.

 1 #include<cstdio>
 2 #include<cstring>
 3 bool use[10],app[30];
 4 int map[30][30],n,a[10],ans,ans_arr[10],alfa[30];
 5 void dfs(int fin,int cur)
 6 {
 7     int i,j,k,p,q,x,y,z,cnt,temp;
 8     char c1,c2;
 9     if (cur>=ans) return;
10     cnt=0;
11     for (i=1;i<=n;i++)
12       if (map[alfa[a[fin]]][alfa[i]]&&!use[i]) cnt++;
13     if (cnt>=ans) return;
14     if (fin==n)
15     {
16         ans=cur;
17         for (i=1;i<=n;i++)
18           ans_arr[i]=a[i];
19         return;
20     }
21     fin++;
22     for (i=1;i<=n;i++)
23       if (!use[i])
24       {
25           a[fin]=i;
26           temp=cur;
27           use[i]=1;
28           for (j=1;j<fin-cur;j++)
29             if (map[alfa[i]][alfa[a[j]]])
30             {
31                 temp=fin-j;
32                 break;
33           }
34         dfs(fin,temp);
35         use[i]=0;
36       }
37 }
38 int main()
39 {
40     char s[1000],c;
41     int i,x,y;
42     while (scanf("%s",s)&&s[0]!='#')
43     {
44         memset(map,0,sizeof(map));
45         memset(use,0,sizeof(use));
46         memset(a,0,sizeof(a));
47         memset(alfa,0,sizeof(alfa));
48         memset(app,0,sizeof(app));
49         n=0;
50         for (i=0;i<strlen(s);)
51         {
52             c=s[i++];
53             x=c-'A'+1;
54             app[x]=1;
55             i++;
56             while (s[i]!=';'&&i<strlen(s))
57             {
58                 y=s[i]-'A'+1;
59                 app[y]=1;
60                 map[x][y]=map[y][x]=1;
61                 i++;
62             }
63             i++;
64         }
65         for (i=1;i<=26;i++)
66           if (app[i])
67             alfa[++n]=i;
68         ans=0x3f3f3f3f;
69         dfs(0,0);
70         for (i=1;i<=n;i++)
71           printf("%c ",alfa[ans_arr[i]]+'A'-1);
72         printf("-> %d\n",ans);
73     }
74 }

搜索+剪枝。

1.目前带宽大于等于已知答案,剪枝。

2.在搜索到某一节点时,与该节点连接的还没有加入排列的点的个数大于等于已知答案,剪枝。(若这些点全都紧跟在此点之后,带宽也为其个数。否则更大。)

读入数据处理的时候注意,要让字母序小的排在前头。

注意各种下标、字母、数字、位置的变量引用。

转载于:https://www.cnblogs.com/AwesomeOrion/p/5331143.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值