zoukankan      html  css  js  c++  java
  • UVa 140 Bandwidth【枚举排列】

    题意:给出n个节点的图,和一个节点的排列,定义节点i的带宽b[i]为i和其相邻节点在排列中的最远的距离,所有的b[i]的最大值为这个图的带宽,给一个图,求出带宽最小的节点排列

    看的紫书,紫书上说得很详细--

    看标程的时候算每个图的带宽的时候看了好久,原来是这样的

    打印出u[i],v[i]的值就知道了

    样例:

    A:FB;B:GC;D:GC;F:AGH;E:HD
    #

    对于每一个u[i],v[i],abs(u[i]-v[i])就是相邻节点之间的距离, 再在这里面找出最大值

     1 #include<iostream>  
     2 #include<cstdio>  
     3 #include<cstring> 
     4 #include <cmath> 
     5 #include<stack>
     6 #include<vector>
     7 #include<map> 
     8 #include<set>
     9 #include<queue> 
    10 #include<algorithm>  
    11 using namespace std;
    12 
    13 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
    14 
    15 typedef long long LL;
    16 const int INF = (1<<30)-1;
    17 const int mod=1000000007;
    18 const int maxn=100005;
    19 
    20 char input[maxn];
    21 int id[maxn],letter[maxn];
    22 
    23 
    24 int main(){
    25 //    freopen("in.txt","r",stdin);
    26 //    freopen("out.txt","w",stdout);
    27     while(scanf("%s",input)!=EOF&&input[0]!='#'){
    28         int n=0;
    29         for(char ch='A';ch<='Z';ch++){//输入处理 
    30             if(strchr(input,ch)!=NULL){
    31                 id[ch]=n++;
    32                 letter[id[ch]]=ch;                
    33             }
    34             
    35         }
    36         
    37         int len=strlen(input),p=0,q=0;
    38         vector<int> u,v;
    39         
    40         for(;;){
    41             while(p<len&&input[p]!=':') p++;
    42             if(p==len) break;
    43             while(q<len&&input[q]!=';') q++;
    44             
    45             for(int i=p+1;i<q;i++){
    46                 u.push_back(id[input[p-1]]);
    47                 v.push_back(id[input[i]]);
    48             }
    49             
    50             p++;q++;//都向后挪一位,不挪的话,p位置一直是‘:’,q位置一直是';',就超时了 
    51         }
    52         
    53     
    54         int P[maxn],bestP[maxn],pos[maxn],ans=n;
    55         
    56         for(int i=0;i<n;i++) P[i]=i;
    57         do{
    58             for(int i=0;i<n;i++) pos[P[i]]=i;//记录下在这个排列中的位置 
    59             
    60             int bandwidth=0;
    61             
    62             for(int i=0;i<u.size();i++)
    63             bandwidth=max(bandwidth,abs(pos[u[i]]-pos[v[i]]));//求出当前的带宽 
    64             
    65             if(bandwidth<ans){//维护一个最小的带宽的值,并保存下此时的排列 
    66                 ans=bandwidth;
    67                 memcpy(bestP,P,sizeof(P));
    68             }
    69         } while(next_permutation(P,P+n));
    70         
    71         
    72         for(int i=0;i<n;i++) printf("%c ",letter[bestP[i]]);
    73         printf("-> %d
    ",ans);    
    74     }
    75     return 0;
    76     
    77 }
    View Code
  • 相关阅读:
    Python容器篇 4 -- 字典
    Python容器篇 3 -- 元组
    Python容器篇 2 -- 列表
    Python容器篇 1 -- 字符串
    Python中的关键字
    SQLI-LABS靶场环境搭建详细流程
    Qt QLineEdit 改变text内容的大小
    linux下QT连接mysql找不到驱动
    apt(rpm) Mysql安装
    const 成员函数
  • 原文地址:https://www.cnblogs.com/wuyuewoniu/p/4467008.html
Copyright © 2011-2022 走看看