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 ...
随机推荐
- ABySS非root权限安装
本文转自 http://yangl.net/2015/11/12/abyss_install/ ABySS: ABySS is a de novo, parallel, paired-end seq ...
- python之消息队列
引言 你是否遇到过两个(多个)系统间需要通过定时任务来同步某些数据?你是否在为异构系统的不同进程间相互调用.通讯的问题而苦恼.挣扎?如果是,那么恭喜你,消息服务让你可以很轻松地解决这些问题.消息服务擅 ...
- Nginx主配置参数详解,Nginx配置网站
1.Niginx主配置文件参数详解 a.上面博客说了在Linux中安装nginx.博文地址为:http://www.cnblogs.com/hanyinglong/p/5102141.html b.当 ...
- 把普通对象转换成json格式的对象
1.什么叫做JSON?JSON只是一种数据格式(它不是一种新的数据类型) var obj = {name: "中国", age: 5000};//->普通格式的对象 var ...
- 鼠标滑过图片变暗文字链接滑出jQuery特效
效果体验:http://hovertree.com/texiao/jquery/7.htm HTML文件代码: <!DOCTYPE html> <html xmlns="h ...
- if语句,case语句
1.句式:if...then.判断赋值 例: if RadioButton1.Checked then sex:='男' else if RadioButton2.Checked then sex:= ...
- .NET 各种框架
基于.NET平台常用的框架整理 分布式缓存框架: Microsoft Velocity:微软自家分布式缓存服务框架. Memcahed:一套分布式的高速缓存系统,目前被许多网站使用以提升网站的访问速度 ...
- (转)SQL 优化原则
一.问题的提出 在应用系统开发初期,由于开发数据库数据比较少,对于查询SQL语句,复杂视图的的编写等体会不出SQL语句各种写法的性能优劣,但是如果将应用 系统提交实际应用后,随着数据库中数据的增加,系 ...
- [deviceone开发]-do_Http组件示例
一.简介 详细展示do_Http组件的使用,包括get,post,upload,form,download的http请求.另外还附加了一个Java实现的后台程序作为参考.初学者强烈推荐. 二.效果图 ...
- Android Studio添加aar
1.把aar复制到项目中的 libs 里面 2.在module 里面的build.gradle 的根目录添加 repositories{ flatDir { dirs 'libs' } } 3.在mo ...