为什么都说简单好想咧。坦白从宽看了人家的代码,涨了好多姿势,,

http://blog.csdn.net/u013382399/article/details/38227917

被一个细节坑了。。

2147483647是0x7fffffff啊啊啊,7个f!!!

  1. #include <iostream>
  2. #include <sstream>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <cmath>
  6. #include <string>
  7. #include <vector>
  8. #include <set>
  9. #include <cctype>
  10. #include <algorithm>
  11. #include <cmath>
  12. #include <deque>
  13. #include <queue>
  14. #include <map>
  15. #include <stack>
  16. #include <list>
  17. #include <iomanip>
  18. using namespace std;
  19.  
  20. #define INF 0x7fffffff
  21. #define maxn 100000+10
  22.  
  23. int n, m;
  24. vector<int> g[maxn];//用以存放互相连通的房间
  25. vector<int> col[maxn];//用以存放走廊颜色
  26. int step[maxn];//存放由n到i的最短步数
  27. int ans[maxn*];
  28. int vis[maxn];
  29. void init()
  30. {
  31. for(int i = ; i < maxn; i++)
  32. {
  33. g[i].clear();
  34. col[i].clear();
  35. }
  36. memset(step, -, sizeof(step));
  37. memset(ans, , sizeof(ans));
  38. memset(vis, , sizeof(vis));
  39. }
  40. //==============获得到n的最少步数(逆向由n到1)===========
  41. void bfs1()
  42. {
  43. queue<int> q;
  44. q.push(n);
  45. step[n] = ;//初始,由n到n的最短步数为0
  46. while(!q.empty())
  47. {
  48. int u = q.front(); q.pop();
  49. int sz = g[u].size();
  50. for(int i = ; i < sz; i++)
  51. {
  52. int v = g[u][i];
  53.  
  54. if(v == )
  55. {
  56. step[v] = step[u]+;
  57. return ;
  58. }
  59.  
  60. if(step[v] == -)
  61. {
  62. step[v] = step[u]+;
  63. q.push(v);
  64. }
  65. }
  66. }
  67. return ;
  68. }
  69.  
  70. //==========获得最少步数时的最小走廊颜色===========
  71. void bfs2()
  72. {
  73. queue<int> q;
  74. q.push();
  75. while(!q.empty())
  76. {
  77. int u = q.front(); q.pop();
  78. ///
  79. if(!step[u]) return ;//到达n
  80. ///
  81. int mmin = INF;
  82. int sz = g[u].size();
  83. for(int i = ; i < sz; i++)
  84. {
  85. int v = g[u][i];
  86. if(step[v] == step[u]-)
  87. {
  88. mmin = min(mmin, col[u][i]);//注意理解c[u][i]与g[u][i]间的联系--c[u][i]是u连接g[u][i]的走廊颜色
  89. }
  90. }
  91. //==========以上获得了从1出发最短路中每步的最小色
  92.  
  93. int tmp_step = step[] - step[u];//从1到u的步数,即出发第tmp_step步
  94. //ans[tmp_step] = (ans[tmp_step] == 0 ? mmin : min(mmin, ans[tmp_step]));
  95. if(ans[tmp_step] == ) ans[tmp_step] = mmin;
  96. else ans[tmp_step] = min(ans[tmp_step], mmin);
  97.  
  98. for(int i = ; i < sz; i++)
  99. {
  100. int v = g[u][i];
  101. ///该处判断条件很重要,把走过的路做标记
  102. if(!vis[v] && step[v] == step[u]- && mmin == col[u][i])
  103. {
  104. vis[v] = ;
  105. q.push(v);
  106. }
  107. }
  108. }
  109. return ;
  110. }
  111. int main()
  112. {
  113. //===================input=====================
  114. while(~scanf("%d%d", &n, &m))
  115. {
  116. init();
  117. while(m--)
  118. {
  119. int a, b, c;
  120. scanf("%d%d%d", &a, &b, &c);
  121. if(a == b) continue;
  122. g[a].push_back(b);
  123. g[b].push_back(a);
  124. col[a].push_back(c);
  125. col[b].push_back(c);
  126. }
  127. //===============逆向bfs===============
  128. //============获得最短步数=============
  129. bfs1();
  130. //===============正向bfs===============
  131. //==========获得每步的走廊颜色=========
  132. bfs2();
  133.  
  134. printf("%d\n", step[]);
  135. for(int i = ; i < step[]; i++)
  136. {
  137. if(i) printf(" ");
  138. printf("%d", ans[i]);
  139. }
  140. printf("\n");
  141. }
  142. return ;
  143. }

逆向+两次bfs(UVA 1599)的更多相关文章

  1. UVa 1599 Ideal Path (两次BFS)

    题意:给出n个点,m条边的无向图,每条边有一种颜色,求从结点1到结点n颜色字典序最小的最短路径. 析:首先这是一个最短路径问题,应该是BFS,因为要保证是路径最短,还要考虑字典序,感觉挺麻烦的,并不好 ...

  2. UVa 11624,两次BFS

    题目链接:http://vjudge.net/contest/132239#problem/A 题目链接:https://uva.onlinejudge.org/external/116/11624. ...

  3. UVA 11624 Fire!(两次BFS+记录最小着火时间)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  4. UVa 1599 理想路径(反向BFS 求最短路径 )

    题意: 给定一个有重边有自环的无向图,n个点(2 <= n <= 100000), m条边(1 <= m <= 200000), 每条边有一个权值, 求从第一个点到n的最少步数 ...

  5. Fire! UVA - 11624 (两步bfs)

    题目链接 题意 人要从迷宫走出去,火会向四个方向同时扩散 分析 两步bfs,先出火到达各地时的时间(设初始时间为0,人每走一步为1s,在着一步内火可以向四周可触及的方向同时扩散),然后在bfs人,人能 ...

  6. UVA 1599 Ideal Path (HDU 3760)

    两次bfs: 第一次bfs逆向搜索,得到每个点到终点的最短距离,找出最短路:第二次bfs根据最短距离可以选择满足条件的最短路. 注意!碰到这种很大数据量的题目一定要记得用scanf,printf 输入 ...

  7. POJ 1475 Pushing Boxes 搜索- 两重BFS

    题目地址: http://poj.org/problem?id=1475 两重BFS就行了,第一重是搜索箱子,第二重搜索人能不能到达推箱子的地方. AC代码: #include <iostrea ...

  8. HDU2612---(两次BFS)

    Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Nin ...

  9. CSU - 2031 Barareh on Fire (两层bfs)

    传送门: http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2031 Description The Barareh village is on f ...

随机推荐

  1. 5个Xcode开发调试技巧

    转自Joywii的博客,原文:Four Tips for Debugging in XCode Like a Bro    1.Enable NSZombie Objects(开启僵尸对象) Enab ...

  2. HDU 4432 Sum of divisors (水题,进制转换)

    题意:给定 n,m,把 n 的所有因数转 m 进制,再把各都平方,求和. 析:按它的要求做就好,注意的是,是因数,不可能有重复的...比如4的因数只有一个2,还有就是输出10进制以上的,要用AB.. ...

  3. ie6的兼容问题及解决方案

    1.png24位的图片在ie6浏览器上会出现背景,解决方案是做成png8位: 2.浏览器默认的margin和padding不同,解决方法是用全局重置来统一,即是*{margin:0;padding:0 ...

  4. MVC4 EF6 MYSQL

    在MVC的框架下连接mysql数据库 将EF框架升级到EF6 将NEW JSON升级到与之相匹配的版本 然后进行相应的配置就可以了

  5. 关于android 将对象写入文件以及从文件读取对象

    由于项目需求,需要保存用户登录过的一些配置,当下次登录的时候读取登录过的配置,所以简单的SharePreferences没有办法满足,于是找到了Java中ObjectInputStream 与 Obj ...

  6. 设计模式六大原则——合成/聚合复用原则(CARP)

    1.定义 简而言之,对于合成/聚合复用原则的定义就是:要尽量使用合成和聚合,尽量不要使用继承. 2.释义 为什么"要尽量使用合成和聚合.尽量不要使用继承"呢? 这是由于: 第一,继 ...

  7. fl,flash,mx包的区别

    在ActionScript项目中还真是不能使用mx包中的UI组件.           Adobe官方论坛上有一个帖子讲述了这个问题,大致意思是说:你要使用mx包中像Button这样的UI组件都是从U ...

  8. 第1章 游戏之乐——NIM(2)“拈”游戏分析

    NIM(2)“拈”游戏分析 1. 问题 有N块石头和两个玩家A和B,玩家A先将石头分成若干堆,然后按照BABA……的顺序不断轮流取石头,能将剩下的石头一次取光的玩家获胜.每次取石头时,每个玩家只能从若 ...

  9. Web开发接口测试工具——Postman插件的使用(chrome浏览器)

    Postman是chrome浏览器的一款插件.Postman 可以模拟 http 请求的发送,并自动解析 JSON 和 XML 的返回数据. 可以手动的去配置各类 parameter,还支持 Basi ...

  10. VBA Excel 常用 自定义函数

    1. 将 互换 Excel 列号(数字/字母) Public Function excelColumn_numLetter_interchange(numOrLetter) As String Dim ...