思路:暴力+剪枝

uva140

wa了好多次……数组开小了……!!!

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <algorithm>
  6. #include <cstdlib>
  7. #include <stack>
  8. #include <cctype>
  9. #include <string>
  10. #include <malloc.h>
  11. #include <queue>
  12. #include <map>
  13.  
  14. using namespace std;
  15. const int INF = 0xffffff;
  16. const double esp = 10e-;
  17. const double Pi = * atan(1.0);
  18. const int Maxn = ;
  19. const int mod = ;
  20. const int dr[] = {,,-,,-,,-,};
  21. const int dc[] = {,,,-,,-,-,};
  22. //int dir2[8][2] = {{-1,0},{0,-1},{-1,1},{1,-1},{-1,-1},{1,0},{0,1},{1,1}};
  23.  
  24. bool graph[Maxn][Maxn];
  25. int arr[Maxn];
  26. int n;
  27. int Min;
  28. int tot;
  29. bool visit[Maxn];
  30. int ans[Maxn];
  31. int init[Maxn];
  32.  
  33. void dfs(int cur){
  34. if(tot >= Min)
  35. return;
  36. if(cur == n){
  37. Min = min(Min,tot);
  38. for(int i = ;i < n;i++){
  39. ans[i] = arr[i];
  40. }
  41. return;
  42. }
  43. for(int i = ;i < n;i++){
  44. if(!visit[ init[i] ]){
  45. visit[init[i]] = ;
  46. arr[cur] = init[i];
  47. int tmp = ;
  48. for(int j = ;j < cur;j++){
  49. if(graph[ arr[j] ][ init[i] ]){
  50. int tt = abs(cur - j);
  51. tmp = max(tmp,tt);
  52. if(tmp > Min)
  53. break;
  54. }
  55. }
  56. int tt = tot;
  57. tot = max(tmp,tot);
  58. dfs(cur+);
  59. visit[init[i]] = ;
  60. tot = tt;
  61. }
  62. }
  63. }
  64. char str[];
  65. int main()
  66. {
  67. #ifndef ONLINE_JUDGE
  68. freopen("inpt.txt","r",stdin);
  69. #endif
  70. while(scanf("%s",str) != EOF){
  71. if(str[] == '#')
  72. break;
  73. int len = strlen(str);
  74. n = ;
  75. memset(graph,,sizeof(graph));
  76. memset(arr,,sizeof(arr));
  77. memset(visit,,sizeof(visit));
  78. for(int i = ;i < len;i++){
  79. if(str[i] == ' ')
  80. continue;
  81. int a = str[i] - 'A';
  82. if(!arr[a]){
  83. init[n++] = str[i] - 'A';
  84. arr[a] = ;
  85. }
  86. for(i = i+;str[i] != ';' && i < len;i++){
  87. if(!isalpha(str[i]))
  88. continue;
  89. int b = str[i] - 'A';
  90. graph[a][b] = ;
  91. graph[b][a] = ;
  92. if(!arr[b]){
  93. init[n++] = str[i] - 'A';
  94. arr[b] = ;
  95. }
  96. }
  97. }
  98. Min = INF;
  99. tot = ;
  100. sort(init,init+n);
  101. dfs();
  102. for(int i = ;i < n;i++){
  103. printf("%c ",ans[i] + 'A');
  104. }
  105. printf("-> %d\n",Min);
  106. }
  107. return ;
  108. }

uva 140的更多相关文章

  1. 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 orde ...

  2. UVa 140 (枚举排列) Bandwidth

    题意较复杂,请参见原题=_=|| 没什么好说的,直接枚举每个排列就好了,然后记录最小带宽,以及对应的最佳排列. STL里的next_permutation函数真是好用. 比较蛋疼的就是题目的输入了.. ...

  3. UVA 140 (13.07.29)

     Bandwidth  Given a graph (V,E) where V is a set of nodes and E is a set of arcsin VxV, and anorderi ...

  4. UVA 140 Bandwidth

    题意: 给出一个n个节点的图G,和一个节点的排列,定义节点i的带宽为i和相邻节点在排列中的最远距离,而所有带宽的最大值就是图的带宽,求让图的带宽最小的排列. 分析: 列出所有可能的排列,记录当前找到的 ...

  5. UVA - 140 Bandwidth(带宽)(全排列)

    题意:给定图,求是带宽最小的结点排列. 分析:结点数最多为8,全排列即可.顶点范围是A~Z. #pragma comment(linker, "/STACK:102400000, 10240 ...

  6. UVa 140 带宽

    题意:给出一个n个结点的图G和一个结点的排列,定义结点的带宽为i和相邻结点在排列中的最远距离,求出让带宽最小的结点排列. 思路:用STL的next_permutation来做确实是很方便,适当剪枝一下 ...

  7. UVA 140 Brandwidth 带宽 (dfs回溯)

    看到next_permutation好像也能过╮(╯▽╰)╭ 这题学习点: 1.建图做映射 2.通过定序枚举保证字典序最小 3.strtok,sscanf,strchr等函数又复习了一遍,尽管程序中没 ...

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

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

  9. UVa 140 Bandwidth【枚举排列】

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

随机推荐

  1. 在Windows下github展示代码

    最近大爱Web编程,于是寻找各种代码中,然后就发现了GitHub这个网站,如果你知道Google Code,那么你就知道这个GitHub是做什么的了.不过GitHub主要是用作基于Git的分布式版本管 ...

  2. 笔记之Cyclone IV第一卷第四章Cyclone IV器件中的嵌入式乘法器

    嵌入式乘法器可以配置成一个 18 × 18 乘法器,或者配置成两个 9 × 9 乘法器.对于那些大于18 × 18 的乘法运算 ,Quartus II 软件会将多个嵌入式乘法器模块级联在一起.虽然没有 ...

  3. ASP.NET Core (一):简介

    下一篇:ASP.NET Core(二):入门 英文原版:Introduction to ASP.NET Core 关于ASP.NET Core ASP.NET Core 是一个全新的开源.跨平台框架, ...

  4. 17.1.1.3 Creating a User for Replication

    17.1.1.3 Creating a User for Replication 创建一个用户用于复制: 每个slave 连接到master 使用一个MySQL 用户名和密码, 因此必须有一个user ...

  5. H面试程序(28):字符串处理转换

    //2 字符串处理转换 //问题描述: //在给定字符串中找出单词( “单词”由大写字母和小写字母字符构成, //其他非字母字符视为单词的间隔,如空格.问号.数字等等:另外单个字母不算单词): //找 ...

  6. oracle scn浅析

    1. 系统SCN号 查询系统SCN号的方法: select dbms_flashback.get_system_change_number from dual commit后系统SCN号会增长,但是即 ...

  7. ACM比赛(第二次A)

    ime Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Description There is ...

  8. java--异常处理总结

    [在程序中抛出异常] 在程序中抛出异常,一定要使用关键字throw. throw+异常实例对象. public class Demo2 { public static void main(String ...

  9. ZOJ 3603字符串操作

    解题思路:找到公共子串然后升序输出 坑的地方就在于输入是存在相同字母的 #include <stdio.h> #include <algorithm> #include < ...

  10. PHP - 接口 - 单一接口

    /* * 接口的使用 */ //定义接口 interface IPerosn{ public function eat(); public function water(); } //定义继承自接口的 ...