[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 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.

Sample input

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

Sample output

A B C F G D H E -> 3


  • 时间:2016-02-20 16:44:54 星期六
  • 题目编号:UVA 140
  • 题目大意:
    • 给定一个图G(V,E),输出一个节点的序列,使得这个序列是所有序列中,带宽最小的序列(带宽相同时,字典序最小).
    • 带宽:每个节点在该序列中,到相邻节点的距离(从序列上来看)中,最大的那个
  • 分析:枚举所有情况,取最优的解
    • 剪枝优化:枚举过程中,遇到带宽大于等于当前最小带宽的,停止当前的枚举,进入下一个
  • 解题过程遇到问题:
    • 给出来的节点不一定是连续的!!!需要重新定义节点的编号,方便后面枚举
    • 这里的处理方法是,先把图按原字母处理出来,在重新矫正对应的编号  

#include <vector>

#include <list>

#include <map>

#include <set>

#include <deque>

#include <queue>

#include <stack>

#include <bitset>

#include <algorithm>

#include <functional>

#include <numeric>

#include <utility>

#include <sstream>

#include <iostream>

#include <iomanip>

#include <cstdio>

#include <cmath>

#include <cstdlib>

#include <cctype>

#include <string>

#include <cstring>

#include <cstdio>

#include <cmath>

#include <cstdlib>

#include <ctime>

using namespace std;

typedef long long LL;

#define CLR(x,y) memset((x),(y),sizeof((x)))

#define FOR(x,y,z) for(int (x)=(y);(x)<(z);(x)++)

#define FORD(x,y,z) for(int (x)=(y);(x)>=(z);(x)--)

#define FOR2(x,y,z) for((x)=(y);(x)<(z);(x)++)

#define FORD2(x,y,z) for((x)=(y);(x)>=(z);(x)--)

char str[8*10],ch[10];

int G[29][29],vis[28],n,minres,res[10],a[10],g[10][10];

//G保存原图,g保存更正后的图,ch保存出现的节点的字母,a临时保存dfs过程中的顺序,

//res当前最优的顺序,minres当前最优的答案,

void read_in(){

//清理

CLR(g,0);CLR(G,0);CLR(vis,0);n = 0;

char *p = str;

int u,v,len = strlen(str);

//后面不上一个 ";‘,方便后面书写

str[len] = ‘;‘;

str[len + 1] = ‘\0‘;

//保存原图,并记录出现的字母

while(*p){

u = *p - ‘A‘;

if(!vis[u]){

vis[u] = 1;

ch[n++] = *p;

}

++p;++p;

while(*p != ‘;‘){

int v = *p - ‘A‘;

if(!vis[v]){

vis[v] = 1;

ch[n++] = *p;

}

G[u][v] = G[v][u] = 1;

p++;

}

p++;

}

sort(ch,ch + n);

//矫正编号

FOR(i,0,27){

FOR(j,0,27){

if(G[i][j]){

FOR(k,0,n){

if(i == ch[k] - ‘A‘)    u = k;

if(j == ch[k] - ‘A‘)    v = k;

}

g[u][v] = 1;

}

}

}

}

int count(int cur){

int u = a[cur],v;

FOR(i,0,cur){

v = a[i];

if(g[u][v])     return cur - i;

}

return 0;

}

void dfs(int cur,int curmax){

if(cur == n){

minres = curmax;

memcpy(res,a,sizeof(a));

return ;

}

FOR(i,0,n){

if(!vis[i]){

a[cur] = i;

vis[i] = 1;

int tmp = max(curmax,count(cur));

if(tmp < minres)        dfs(cur + 1,tmp);

vis[i] = 0;

}

}

}

int main(){

while(gets(str) && str[0] != ‘#‘){

read_in();

minres = n + 1;

CLR(vis,0);

dfs(0,0);

FOR(i,0,n)      printf("%c ",ch[res[i]]);

printf("-> %d\n",minres);

}

return 0;

}  

来自为知笔记(Wiz)

时间: 2025-01-02 16:43:37

[2016-02-20][UVA][140][Bandwidth]的相关文章

2016/02/20 codes

<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title>2016/02/20</title></head><body><div id="mainDiv"> <div id = "content"> <div id = &qu

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,

2016.02.20 学习笔记 数据在Activity之间的传递的情况

情况一:一个Activity跳转到另一个Activity时,将第一个Activity的数据传递到第二个Activity里面. 分析:当一个界面跳转到另一个界面的同时还要讲数据传递过去,这种情况需要用Intent类putExtra()方法实现. 具体在Onclick方法中的样例代码如下: Intent i1=new Intent(this,SecondActivity.class); String Message=Edit1.getText().toString(); i1.putExtra("M

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

翻译:Gregory Larsen,2016/02/19(第一版:2014年12月17日)高级T-SQL阶梯1级:使用CROSS JOIN介绍高级T-SQL

原文链接:http://www.sqlservercentral.com/articles/Stairway+Series/119933/ 原文作者:Gregory Larsen,2016/02/19(第一版:2014年12月17日) 系列 本文是"Stairway Series:Stairway to Advanced T-SQL"的一部分 这个阶梯将包含一系列文章,这些文章将在前面两个T-SQL阶梯,T-SQL DML和T-SQL超越基础知识的T-SQL基础上进行扩展. 这个楼梯应

2016/02/21 codes

var Class = { create:function(){ var parent = null,properties = $A(arguments); if(Object.isFunction(properties[0])) parent = properties.shift(); function kclass(){ this.initialize.apply(this.arguments); } Object.extend(kclass,Class.Methods); kclass.s