看到next_permutation好像也能过╮(╯▽╰)╭

这题学习点:

1.建图做映射

2.通过定序枚举保证字典序最小

3.strtok,sscanf,strchr等函数又复习了一遍,尽管程序中没有实际用上

4.剪枝,或者回溯

#include<bits/stdc++.h>
using namespace std; int G[][],deg[];
bool vG[][];//判读连通
int pos[];
bool vis[];
int k;
int best[];
int cnt;
int ID[];
char rev_ID[]; void dfs(int d,int width)
{
if(d == cnt){
if(width < k){
k = width;
memcpy(best,pos,sizeof(pos));
}
return;
}
for(int i = ;i < cnt; i++) if(!vis[i]){//把i放在d位置
pos[d] = i; //prune 计算i和之前确定位置的结点的最大距离
int m = ;
for(int j = d-; j >=; j--) if(vG[i][pos[j]]) {
m = max(m,d-j);
if(m >= k) continue;//这题数据水了,这里写成return还是能过
} //prune2 计算u和未确定位置的相邻点的个数,适用于简单图
int ct = ;
for(int j = ; j < deg[i]; j++) if(!vis[G[i][j]]) ct++;
if(ct >= k) continue; vis[i] = ;
dfs(d+,max(width,m));
vis[i] = ;
}
} int main()
{
// freopen("in.txt","r",stdin);
char buf[];
const int INF = 0x3fffffff;
while(fgets(buf,,stdin) && *buf!='#'){
cnt = ;
memset(vG,,sizeof(vG));
memset(deg,,sizeof(deg));
memset(ID,-,sizeof(ID));
memset(vis,,sizeof(vis));
bool ap[] ;
memset(ap,,sizeof(ap));
for(char *cur = buf; *cur ; cur++) {
int u = *cur - 'A';
if(<= u && u < ) ap[u] = ;
} for(int i = ; i < ; i++){
if(ap[i]){
rev_ID[cnt] = i+'A';
ID[i] = cnt++;
}
} for(char *cur = buf; *cur ; cur++) {
int u = ID[*cur - 'A'];
for(cur+= ; *cur != '\n' && *cur != ';' ; cur++) {
int v = ID[*cur - 'A'];
vG[u][v] = vG[v][u] = ;
}
} for(int i = ; i < cnt; i++)
for(int j = ; j < cnt; j++){
if(vG[i][j]) G[i][deg[i]++] = j;
}
k = INF;
dfs(,);
for(int i = ; i < cnt; i++){
printf("%c ",rev_ID[best[i]]);
}
printf("-> %d\n",k);
}
return ;
}

UVA 140 Brandwidth 带宽 (dfs回溯)的更多相关文章

  1. UVA 140 Bandwidth (dfs 剪枝 映射)

    题意: 给定一个n个结点的图G和一个结点的排列, 定义结点i的带宽b(i)为i和相邻结点在排列中的最远距离, 所有b(i)的最大值就是这个图的带宽, 给定G, 求让带宽最小的结点排列. 给定的图 n ...

  2. UVa 129 Krypton Factor (DFS && 回溯)

    题意 : 如果一个字符串包含两个相邻的重复子串,则称它是“容易的串”,其他串称为“困难的 串”.例如,BB.ABCDACABCAB.ABCDABCD都是容易的串,而D.DC.ABDAB. CBABCB ...

  3. uva 193 Graph Coloring(图染色 dfs回溯)

    Description You are to write a program that tries to find an optimal coloring for a given graph. Col ...

  4. poj1270Following Orders(拓扑排序+dfs回溯)

    题目链接: 啊哈哈.点我点我 题意是: 第一列给出全部的字母数,第二列给出一些先后顺序. 然后按字典序最小的方式输出全部的可能性.. . 思路: 整体来说是拓扑排序.可是又非常多细节要考虑.首先要按字 ...

  5. 素数环(dfs+回溯)

    题目描述: 输入正整数n,把整数1,2...n组成一个环,使得相邻两个数和为素数.输出时从整数1开始逆时针排列并且不能重复: 例样输入: 6 例样输出: 1 4 3 2 5 6 1 6 5 2 3 4 ...

  6. NOJ 1074 Hey Judge(DFS回溯)

    Problem 1074: Hey Judge Time Limits:  1000 MS   Memory Limits:  65536 KB 64-bit interger IO format: ...

  7. HDU 1016 Prime Ring Problem(经典DFS+回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. HDU 2181 哈密顿绕行世界问题(经典DFS+回溯)

    哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  9. HDU1016 Prime Ring Problem(DFS回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

随机推荐

  1. 使用MeshLab的技巧总结(自己原创总结)

    1.放大点的显示,Alt+wheel,即按住Alt键后使用鼠标滑轮改变点的显示大小.

  2. POJ - 3268 Silver Cow Party SPFA+SLF优化 单源起点终点最短路

    Silver Cow Party One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to ...

  3. Animation Blueprint, Set Custom Variables Via C++

    https://wiki.unrealengine.com/Animation_Blueprint,_Set_Custom_Variables_Via_C%2B%2B Animation Bluepr ...

  4. github最火的springboot开源学习资料

    https://github.com/JeffLi1993/springboot-learning-example https://github.com/ityouknow/spring-boot-e ...

  5. 利用sizeof,得到二维数组的维度

    #include <iostream> #include <stdlib.h> using namespace std; int main() { ][]; cout < ...

  6. IOS 打包提示错误(ERROR ITMS-90125: ERROR ITMS-90087: ERROR ITMS-90209:)

    提示这种错误是集成环信造成的,解决方法看环信的官方文档: 集成动态库上传AppStore 由于 iOS 编译的特殊性,为了方便开发者使用,我们将 i386 x86_64 armv7 arm64 几个平 ...

  7. css绘制各种形状

    代码:http://runjs.cn/code/9lyjtbxl: 效果:http://sandbox.runjs.cn/show/9lyjtbxl <!DOCTYPE html> < ...

  8. Educational Codeforces Round 65 (Rated for Div. 2) A. Telephone Number

    链接:https://codeforces.com/contest/1167/problem/A 题意: A telephone number is a sequence of exactly 11  ...

  9. Codeforces Round #431 (Div. 2) B

    Connect the countless points with lines, till we reach the faraway yonder. There are n points on a c ...

  10. Net Core免费开源分布式异常日志收集框架Exceptionless

    asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 https://www.cnblogs.com/yilezhu/p/9193723.htm ...