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.
#include<cstdio>
#include<cstring>
bool use[],app[];
int map[][],n,a[],ans,ans_arr[],alfa[];
void dfs(int fin,int cur)
{
int i,j,k,p,q,x,y,z,cnt,temp;
char c1,c2;
if (cur>=ans) return;
cnt=;
for (i=;i<=n;i++)
if (map[alfa[a[fin]]][alfa[i]]&&!use[i]) cnt++;
if (cnt>=ans) return;
if (fin==n)
{
ans=cur;
for (i=;i<=n;i++)
ans_arr[i]=a[i];
return;
}
fin++;
for (i=;i<=n;i++)
if (!use[i])
{
a[fin]=i;
temp=cur;
use[i]=;
for (j=;j<fin-cur;j++)
if (map[alfa[i]][alfa[a[j]]])
{
temp=fin-j;
break;
}
dfs(fin,temp);
use[i]=;
}
}
int main()
{
char s[],c;
int i,x,y;
while (scanf("%s",s)&&s[]!='#')
{
memset(map,,sizeof(map));
memset(use,,sizeof(use));
memset(a,,sizeof(a));
memset(alfa,,sizeof(alfa));
memset(app,,sizeof(app));
n=;
for (i=;i<strlen(s);)
{
c=s[i++];
x=c-'A'+;
app[x]=;
i++;
while (s[i]!=';'&&i<strlen(s))
{
y=s[i]-'A'+;
app[y]=;
map[x][y]=map[y][x]=;
i++;
}
i++;
}
for (i=;i<=;i++)
if (app[i])
alfa[++n]=i;
ans=0x3f3f3f3f;
dfs(,);
for (i=;i<=n;i++)
printf("%c ",alfa[ans_arr[i]]+'A'-);
printf("-> %d\n",ans);
}
}
搜索+剪枝。
1.目前带宽大于等于已知答案,剪枝。
2.在搜索到某一节点时,与该节点连接的还没有加入排列的点的个数大于等于已知答案,剪枝。(若这些点全都紧跟在此点之后,带宽也为其个数。否则更大。)
读入数据处理的时候注意,要让字母序小的排在前头。
注意各种下标、字母、数字、位置的变量引用。
uva 140 bandwidth (好题) ——yhx的更多相关文章
- UVa 140 Bandwidth【枚举排列】
题意:给出n个节点的图,和一个节点的排列,定义节点i的带宽b[i]为i和其相邻节点在排列中的最远的距离,所有的b[i]的最大值为这个图的带宽,给一个图,求出带宽最小的节点排列 看的紫书,紫书上说得很详 ...
- UVA 140 Bandwidth
题意: 给出一个n个节点的图G,和一个节点的排列,定义节点i的带宽为i和相邻节点在排列中的最远距离,而所有带宽的最大值就是图的带宽,求让图的带宽最小的排列. 分析: 列出所有可能的排列,记录当前找到的 ...
- UVA - 140 Bandwidth(带宽)(全排列)
题意:给定图,求是带宽最小的结点排列. 分析:结点数最多为8,全排列即可.顶点范围是A~Z. #pragma comment(linker, "/STACK:102400000, 10240 ...
- UVA 140 Bandwidth (dfs 剪枝 映射)
题意: 给定一个n个结点的图G和一个结点的排列, 定义结点i的带宽b(i)为i和相邻结点在排列中的最远距离, 所有b(i)的最大值就是这个图的带宽, 给定G, 求让带宽最小的结点排列. 给定的图 n ...
- UVa 489 HangmanJudge --- 水题
UVa 489 题目大意:计算机给定一个单词让你猜,你猜一个字母,若单词中存在你猜测的字母,则会显示出来,否则算出错, 你最多只能出错7次(第6次错还能继续猜,第7次错就算你失败),另注意猜一个已经猜 ...
- UVa 1585 Score --- 水题
题目大意:给出一个由O和X组成的串(长度为1-80),统计得分. 每个O的分数为目前连续出现的O的个数,例如,OOXXOXXOOO的得分为1+2+0+0+1+0+0+1+2+3 解题思路:用一个变量t ...
- UVa OJ 140 - Bandwidth (带宽)
Time limit: 3.000 seconds限时3.000秒 Problem问题 Given a graph (V,E) where V is a set of nodes and E is a ...
- UVa 140 (枚举排列) Bandwidth
题意较复杂,请参见原题=_=|| 没什么好说的,直接枚举每个排列就好了,然后记录最小带宽,以及对应的最佳排列. STL里的next_permutation函数真是好用. 比较蛋疼的就是题目的输入了.. ...
- 【例题 7-6 UVA - 140】Bandwidth
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 暴力做就好. O(8!*26^2) [代码] /* 1.Shoud it use long long ? 2.Have you ev ...
随机推荐
- 【Tip】如何让引用的dll随附的xml注释文档、pdb调试库等文件不出现在项目输出目录中
项目输出目录(bin/debug|release)中经常是这个样子: main.exemain.pdb a.dll a.xml b.dll b.pdb b.xml ... 其中xml是同名dll的注释 ...
- EF架构~EF异步改造之路~仓储接口的改造
回到目录 返回异步与并行目录 C#5.0带来了并行编程 {C#1.0托管代码→C#2.0泛型→C#3.0LINQ→C#4.0动态语言→C#5.0异步编程} 随着C#5.0在.net4.5出来之后,它们 ...
- C#基础-邮件发送
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- <%@ page contentType="text/html; charset=utf-8" language="java"%>每一个字符的含义
contentType="text/html:网页类型htmlcharset=utf-8"网页编码类型language="java"网页编程语言<% @ ...
- 复杂的xml转化为java实体
一.样例一: 以根据订单号向支付宝查询支付是否成功为例(成功信息) 失败信息: <?xml version="1.0" encoding="utf-8"? ...
- 十一个行为模式之模板方法模式(Template Method Pattern)
定义: 定义一个操作中算法的框架,并且将一部分操作延迟到子类当中.使得子类可以不改变一个算法的结构即可重新定义算法步骤. 结构图: AbstractClass:抽象方法类,定义了一些基本操作,这些操作 ...
- WindowsAPI调用和OCR图片识别
傻了吧唧的装双系统.成功的干崩了原本的系统.现在重装VS.闲的没事胡扯几句. WindowsAPI在每一台Windows系统上开放标准API供开发人员调用.功能齐全.在这里只介绍三个部分. 1.利用A ...
- ajax分页与组合查询配合使用
使用纯HTML页与js.ajax.Linq实现分页与组合查询的配合使用 <body> <div id="top"><input type=" ...
- js事件委托
什么是事件委托:通俗的讲,onclick,onmouseover,onmouseout,等就是事件,委托呢,就是让别人来做,这个事件本来是加在某些元素上的,然而你却加到别人身上来做,完成这个事件. 也 ...
- 【UI插件】简单的日历插件(下)—— 学习MVC思想
前言 我们上次写了一个简单的日历插件,但是只是一个半成品,而且做完后发现一些问题,于是我们今天尝试来解决这些问题 PS:距离上次貌似很久了 上次,我们大概遇到哪些问题呢: ① 既然想做一套UI库,那么 ...