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:

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.

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

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

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

时间: 2024-12-17 15:03:20

uva 140 bandwidth (好题) ——yhx的相关文章

[2016-02-20][UVA][140][Bandwidth]

UVA - 140 Bandwidth Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description 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 

uva 140 Bandwidth (全排列+暴力枚举)

uva 140 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

UVa 140 Bandwidth(DFS 回溯 剪枝)

题意  有一个无向图  对于其所有顶点的一个排列 称一顶点与所有与其有边的其它顶点在排列中下标差的最大值为这个顶点的带宽   而所有顶点带宽的最大值为这个排列的带宽   求这个图带宽最小的排列 枚举排列  ans记录当前找到的最小带宽  枚举过程中一旦出现带宽大于ans的也就不用再扩展了  这样枚举完就得到了答案 #include<cstdio> #include<cstring> using namespace std; const int N = 50; int n, ans,

UVA - 140 Bandwidth(带宽)(全排列)

题意:给定图,求是带宽最小的结点排列. 分析:结点数最多为8,全排列即可.顶点范围是A~Z. #pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #i

uva 140 Bandwidth(全排列+递归)

快睡觉的时候1A的把序列全排列,递归暴力判断就ok啦,我改成对应的整数存了,a数组存的是所有的字符的排列, b数组存的是所有开始节点的排列,map[i][j]数组存的是i为起点,与j相邻 贴代码: #include<stdio.h> #include<stdlib.h> #include<string.h> #include<limits.h> #include<math.h> #include<algorithm> using na

UVa 489 HangmanJudge --- 水题

UVa 489 题目大意:计算机给定一个单词让你猜,你猜一个字母,若单词中存在你猜测的字母,则会显示出来,否则算出错, 你最多只能出错7次(第6次错还能继续猜,第7次错就算你失败),另注意猜一个已经猜过的单词也算出错, 给定计算机的单词以及猜测序列,判断玩家赢了(You win).输了(You lose).放弃了(You chickened out) /* UVa 489 HangmanJudge --- 水题 */ #include <cstdio> #include <cstring

uva 10006 数论入门题

这是一个入门的数论题目 , 只需要简单的找素数和快速幂取模 题意:输入一个数 n , 如果这个数是非素数 , 问是不是 这个2~n-1区间的所有数都满足 ? 解法:由于数据量不大 , 可以直接暴力求解 解法1: 暴力求解 #include <iostream> #include <string.h> #include <stdio.h> using namespace std; long long prime[65010]; long long n; void init

UVa 1585 Score --- 水题

题目大意:给出一个由O和X组成的串(长度为1-80),统计得分. 每个O的分数为目前连续出现的O的个数,例如,OOXXOXXOOO的得分为1+2+0+0+1+0+0+1+2+3 解题思路:用一个变量term记录当前O的分数,若出现O,则term+1,若出现X,则term=0: 再用一个sum记录总和,没次加上term即可 /* UVa 1585 Score --- 水题 */ #include <cstdio> #include <cstring> const int maxn =

UVa 140 (枚举排列) Bandwidth

题意较复杂,请参见原题=_=|| 没什么好说的,直接枚举每个排列就好了,然后记录最小带宽,以及对应的最佳排列. STL里的next_permutation函数真是好用. 比较蛋疼的就是题目的输入了.. 还有就是不明白,为什么19.20行注释哪错了?? 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int maxn = 10; 5 6 int id[256], letter[maxn]; 7 char in[1000];