uva 104 Bandwidth

题意:

  给一个图, 将其节点以任一序列排列。

  1)计算每个节点距离相邻节点的最大距离 dis[i]

  2)计算出当前序列中, 所有节点的dis[i], 并求出最大的dis[i] : max_dis

  求最小的max_dis, 并且输出此序列。

  节点数不超过8个

思路:

  节点数不超过八个, 那直接进行全排列, 求解最小值即可。

  复杂度为O(n!)

  坑爹的地方在读图, 模拟读图, 得处理好细节, 全排列的生成直接用dfs即可。

代码:

  

  1 #include <cmath>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cstdlib>
  5 #include <ctime>
  6 #include <set>
  7 #include <map>
  8 #include <list>
  9 #include <stack>
 10 #include <queue>
 11 #include <string>
 12 #include <vector>
 13 #include <fstream>
 14 #include <iterator>
 15 #include <iostream>
 16 #include <algorithm>
 17 using namespace std;
 18 #define LL long long
 19 #define INF 0x3f3f3f3f
 20 #define MOD 1000000007
 21 #define eps 1e-6
 22 #define MAXN 26
 23 #define MAXM 1000
 24 #define dd {cout<<"debug"<<endl;}
 25 #define pa {system("pause");}
 26 #define p(x) {printf("%d\n", x);}
 27 #define pd(x) {printf("%.7lf\n", x);}
 28 #define k(x) {printf("Case %d: ", ++x);}
 29 #define s(x) {scanf("%d", &x);}
 30 #define sd(x) {scanf("%lf", &x);}
 31 #define mes(x, d) {memset(x, d, sizeof(x));}
 32 #define do(i, x) for(i = 0; i < x; i ++)
 33 #define dod(i, x, l) for(i = x; i >= l; i --)
 34 #define doe(i, x) for(i = 1; i <= x; i ++)
 35 int len, max_ans, n;
 36 char str[MAXM];
 37 vector <int> V[MAXN];
 38 int ans[MAXN];
 39 int order[MAXN];
 40 bool vis[MAXN];
 41 void init()
 42 {
 43     memset(ans, 0, sizeof(ans));
 44     memset(vis, false, sizeof(vis));
 45     memset(order, 0, sizeof(order));
 46     for (int i = 0; i < MAXN; i ++)
 47         V[i].clear();
 48     max_ans = INF;
 49     n = 0;
 50 }
 51 void read()
 52 {
 53     len = strlen(str);
 54     bool flag = false;
 55     char ch;
 56     for (int pos = 0; pos < len; pos ++)
 57     {
 58         if (str[pos] >= ‘A‘ && str[pos] <= ‘Z‘)
 59         {
 60             if (!vis[str[pos] - ‘A‘])
 61             {
 62                 vis[str[pos] - ‘A‘] = true;
 63                 n ++;
 64             }
 65             if (!flag)
 66             {
 67                 ch = str[pos];
 68                 flag = true;
 69             }
 70             else
 71             {
 72                 if (find(V[ch - ‘A‘].begin(), V[ch - ‘A‘].end(), str[pos] - ‘A‘) == V[ch - ‘A‘].end())
 73                     V[ch - ‘A‘].push_back(str[pos] - ‘A‘);
 74                 if (find(V[str[pos] - ‘A‘].begin(), V[str[pos] - ‘A‘].end(), ch - ‘A‘) == V[str[pos] - ‘A‘].end())
 75                     V[str[pos] - ‘A‘].push_back(ch - ‘A‘);
 76             }
 77         }
 78         else if (str[pos] == ‘;‘)
 79         {
 80             flag = false;
 81         }
 82         else if (str[pos] == ‘:‘)
 83             continue;
 84     }
 85     memset(vis, false, sizeof(vis));
 86 }
 87 void show()
 88 {
 89     printf("%d\n", n);
 90     for (int i = 0; i < MAXN; i ++)
 91         if (!V[i].empty())
 92         {
 93             printf("%c:", i + ‘A‘);
 94             for (int j = 0; j < V[i].size(); j ++)
 95                 printf(" %c", V[i][j] + ‘A‘);
 96             printf("\n");
 97         }
 98 }
 99 int get_ans()
100 {
101     int max_num = 0;
102     int temp = 0;
103     for (int i = 0; i < MAXN; i ++)
104     {
105         temp = 0;
106         if (ans[i] == 0) continue;
107         for (int j = 0; j < V[i].size(); j ++)
108         {
109             int x  = abs(ans[V[i][j]] - ans[i]);
110             if (x > temp)
111                 temp = x;
112         }
113         max_num = max(max_num, temp);
114     }
115     return max_num;
116 }
117 void dfs(int root, int num)
118 {
119     ans[root] = num;
120     if (num == n)
121     {
122         int x = get_ans();
123         if (x < max_ans)
124         {
125             max_ans = x;
126             for (int i = 0; i < MAXN; i ++)
127                 if (ans[i])
128                     order[ans[i]] = i;
129         }
130         return ;
131     }
132
133     for (int u = 0; u < MAXN; u ++)
134         if (!vis[u] && !V[u].empty())
135         {
136             vis[u] = true;
137             dfs(u, num + 1);
138             vis[u] = false;
139         }
140 }
141 void solve()
142 {
143     init();
144     read();
145     for (int i = 0; i < MAXN; i ++)
146         if (!V[i].empty())
147         {
148             memset(vis, false, sizeof(vis));
149             memset(ans, 0, sizeof(ans));
150             vis[i] = true;
151             dfs(i, 1);
152         }
153     for (int i = 1; i <= n; i ++)
154         printf("%c ", order[i] + ‘A‘);
155     printf("-> %d\n", max_ans);
156 }
157
158 int main()
159 {
160     while (gets(str))
161     {
162         if (!strcmp(str, "#")) break;
163         solve();
164     }
165     return 0;
166 }

时间: 2024-12-19 19:59:03

uva 104 Bandwidth的相关文章

[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 104 Arbitrage (DP + floyd)

uva 104 Arbitrage Description Download as PDF Background The use of computers in the finance industry has been marked with controversy lately as programmed trading – designed to take advantage of extremely small fluctuations in prices – has been outl

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 (好题) ——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 con

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 104 Arbitrage

动态规划类似FLOYD dp[i][j][k] 表示第i个点经过K次到达j点能获得的最大利润 #include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <stack> #include <queue> #include <cctype> #incl

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];