ÉèΪÊ×Ò³ ¼ÓÈëÊÕ²Ø

TOP

uva 140 Bandwidth £¨È«ÅÅÁÐ+±©Á¦Ã¶¾Ù£©
2015-07-20 17:23:09 À´Ô´: ×÷Õß: ¡¾´ó ÖРС¡¿ ä¯ÀÀ:1´Î
Tags£º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 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


ÌâÄ¿´óÒ⣺¸ø³öһЩµã£¬ÒÔ¼°ËùÓбØÐëÏàÁ¬µÄÁ½µã£¬È»ºóÿ¸öÅÅÐòÖУ¬ÕÒ³ö±ØÐëÁ¬½ÓµÄ¾àÀë×î´óÖµ£¬È»ºóÔÚËùÓÐÐòÁÐÖÐÕÒ³öÁ¬½ÓËùÐè×î¶ÌµÄ¡£

½âÌâ˼·£ºÏȽ«½Úµã¹ØÏµ¹¹³ÉÒ»ÕÅ±í£¬È»ºó¸ù¾Ý±í½øÐÐÈ«ÅÅÁнøÐÐö¾Ù£¨ÀûÓÃnext_permutationº¯Êý£©£¬ÕÒ³ö×îС´ø¿í¡£


#include
  
   
#include
   
     #include
    
      #include
     
       using namespace std; char ch[10], ch2[10]; int A[30][30], Max, Min, a[26]; int find(char a) { for (int i = 0; i < 10; i++) { if (ch[i] == a) return i; } } void getMin() { //ÕÒ³ö¸øÅÅÁÐÇé¿öµÄ´ø¿í int temp1, temp2, num; for (int i = 0; i < 26; i++) { for (int j = 0; j < 26; j++) { if (A[i][j]) { temp1 = find(i + 'A'); temp2 = find(j + 'A'); num = abs(temp1 - temp2); if (Max < num) { Max = num; } } } } } int main() { char str[100]; while (scanf("%s", str) == 1 && strcmp(str, "#") != 0) { memset(A, 0, sizeof(A)); memset(a, 0, sizeof(a)); int len = strlen(str); int cnt1, cnt2 = 0, flag = 1; for (int i = 0; i < len; i++) { //¹¹³ÉÒ»ÕŹØÏµ±í if (str[i] >= 'A' && str[i] <= 'Z') { a[str[i] - 'A']++; if (flag) { cnt1 = str[i] - 'A'; } else { A[cnt1][str[i] - 'A'] = 1; } } else if (str[i] == ':') flag = 0; else if (str[i] == ';') flag = 1; } memset(ch, 0, sizeof(ch)); memset(ch2, 0, sizeof(ch2)); for (int i = 0; i < 26; i++) {//ÕÒ³ö³öÏÖ¹ýµÄ½Úµã×Öĸ±àºÅ if (a[i] != 0) ch[cnt2++] = i + 'A'; } Min = 10; sort(ch, ch + strlen(ch));//ÅÅÐò£¬ÎªÈ«ÅÅÁÐ×ö×¼±¸ do{ //È«ÅÅÁÐÕÒ³ö×îС´ø¿í Max = 0; getMin(); if (Min > Max) { strcpy(ch2, ch); Min = Max; } } while (next_permutation(ch, ch + strlen(ch))); for (int i = 0; i < strlen(ch2); i++) { printf("%c ", ch2[i]); } printf("-> %d\n", Min); } return 0; }
     
    
   
  





¡¾´ó ÖРС¡¿¡¾´òÓ¡¡¿ ¡¾·±Ìå¡¿¡¾Í¶¸å¡¿¡¾Êղء¿ ¡¾ÍƼö¡¿¡¾¾Ù±¨¡¿¡¾ÆÀÂÛ¡¿ ¡¾¹Ø±Õ¡¿ ¡¾·µ»Ø¶¥²¿¡¿
·ÖÏíµ½: 
ÉÏһƪ£ºc++ ¶·µØÖ÷·¢ÅƳÌÐò³õ¼¶£¨·ÖÅä·¢.. ÏÂһƪ£ºCF29D Ê÷µÄ±éÀú

ÆÀÂÛ

ÕÊ¡¡¡¡ºÅ: ÃÜÂë: (ÐÂÓû§×¢²á)
Ñé Ö¤ Âë:
±í¡¡¡¡Çé:
ÄÚ¡¡¡¡ÈÝ:

¡¤Spring Boot Java£º (2025-12-26 16:20:19)
¡¤Spring Boot¤ÇHello (2025-12-26 16:20:15)
¡¤Spring ¤Î»ù±¾¤«¤éŒ (2025-12-26 16:20:12)
¡¤C++Ä£°å (template) (2025-12-26 15:49:49)
¡¤C ÓïÑÔÖÐÄ£°åµÄ¼¸ÖÖ (2025-12-26 15:49:47)