逆向+两次bfs(UVA 1599)
为什么都说简单好想咧。坦白从宽看了人家的代码,涨了好多姿势,,
http://blog.csdn.net/u013382399/article/details/38227917
被一个细节坑了。。
2147483647是0x7fffffff啊啊啊,7个f!!!
- #include <iostream>
- #include <sstream>
- #include <cstdio>
- #include <cstring>
- #include <cmath>
- #include <string>
- #include <vector>
- #include <set>
- #include <cctype>
- #include <algorithm>
- #include <cmath>
- #include <deque>
- #include <queue>
- #include <map>
- #include <stack>
- #include <list>
- #include <iomanip>
- using namespace std;
- #define INF 0x7fffffff
- #define maxn 100000+10
- int n, m;
- vector<int> g[maxn];//用以存放互相连通的房间
- vector<int> col[maxn];//用以存放走廊颜色
- int step[maxn];//存放由n到i的最短步数
- int ans[maxn*];
- int vis[maxn];
- void init()
- {
- for(int i = ; i < maxn; i++)
- {
- g[i].clear();
- col[i].clear();
- }
- memset(step, -, sizeof(step));
- memset(ans, , sizeof(ans));
- memset(vis, , sizeof(vis));
- }
- //==============获得到n的最少步数(逆向由n到1)===========
- void bfs1()
- {
- queue<int> q;
- q.push(n);
- step[n] = ;//初始,由n到n的最短步数为0
- while(!q.empty())
- {
- int u = q.front(); q.pop();
- int sz = g[u].size();
- for(int i = ; i < sz; i++)
- {
- int v = g[u][i];
- if(v == )
- {
- step[v] = step[u]+;
- return ;
- }
- if(step[v] == -)
- {
- step[v] = step[u]+;
- q.push(v);
- }
- }
- }
- return ;
- }
- //==========获得最少步数时的最小走廊颜色===========
- void bfs2()
- {
- queue<int> q;
- q.push();
- while(!q.empty())
- {
- int u = q.front(); q.pop();
- ///
- if(!step[u]) return ;//到达n
- ///
- int mmin = INF;
- int sz = g[u].size();
- for(int i = ; i < sz; i++)
- {
- int v = g[u][i];
- if(step[v] == step[u]-)
- {
- mmin = min(mmin, col[u][i]);//注意理解c[u][i]与g[u][i]间的联系--c[u][i]是u连接g[u][i]的走廊颜色
- }
- }
- //==========以上获得了从1出发最短路中每步的最小色
- int tmp_step = step[] - step[u];//从1到u的步数,即出发第tmp_step步
- //ans[tmp_step] = (ans[tmp_step] == 0 ? mmin : min(mmin, ans[tmp_step]));
- if(ans[tmp_step] == ) ans[tmp_step] = mmin;
- else ans[tmp_step] = min(ans[tmp_step], mmin);
- for(int i = ; i < sz; i++)
- {
- int v = g[u][i];
- ///该处判断条件很重要,把走过的路做标记
- if(!vis[v] && step[v] == step[u]- && mmin == col[u][i])
- {
- vis[v] = ;
- q.push(v);
- }
- }
- }
- return ;
- }
- int main()
- {
- //===================input=====================
- while(~scanf("%d%d", &n, &m))
- {
- init();
- while(m--)
- {
- int a, b, c;
- scanf("%d%d%d", &a, &b, &c);
- if(a == b) continue;
- g[a].push_back(b);
- g[b].push_back(a);
- col[a].push_back(c);
- col[b].push_back(c);
- }
- //===============逆向bfs===============
- //============获得最短步数=============
- bfs1();
- //===============正向bfs===============
- //==========获得每步的走廊颜色=========
- bfs2();
- printf("%d\n", step[]);
- for(int i = ; i < step[]; i++)
- {
- if(i) printf(" ");
- printf("%d", ans[i]);
- }
- printf("\n");
- }
- return ;
- }
逆向+两次bfs(UVA 1599)的更多相关文章
- UVa 1599 Ideal Path (两次BFS)
题意:给出n个点,m条边的无向图,每条边有一种颜色,求从结点1到结点n颜色字典序最小的最短路径. 析:首先这是一个最短路径问题,应该是BFS,因为要保证是路径最短,还要考虑字典序,感觉挺麻烦的,并不好 ...
- UVa 11624,两次BFS
题目链接:http://vjudge.net/contest/132239#problem/A 题目链接:https://uva.onlinejudge.org/external/116/11624. ...
- UVA 11624 Fire!(两次BFS+记录最小着火时间)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVa 1599 理想路径(反向BFS 求最短路径 )
题意: 给定一个有重边有自环的无向图,n个点(2 <= n <= 100000), m条边(1 <= m <= 200000), 每条边有一个权值, 求从第一个点到n的最少步数 ...
- Fire! UVA - 11624 (两步bfs)
题目链接 题意 人要从迷宫走出去,火会向四个方向同时扩散 分析 两步bfs,先出火到达各地时的时间(设初始时间为0,人每走一步为1s,在着一步内火可以向四周可触及的方向同时扩散),然后在bfs人,人能 ...
- UVA 1599 Ideal Path (HDU 3760)
两次bfs: 第一次bfs逆向搜索,得到每个点到终点的最短距离,找出最短路:第二次bfs根据最短距离可以选择满足条件的最短路. 注意!碰到这种很大数据量的题目一定要记得用scanf,printf 输入 ...
- POJ 1475 Pushing Boxes 搜索- 两重BFS
题目地址: http://poj.org/problem?id=1475 两重BFS就行了,第一重是搜索箱子,第二重搜索人能不能到达推箱子的地方. AC代码: #include <iostrea ...
- HDU2612---(两次BFS)
Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Nin ...
- CSU - 2031 Barareh on Fire (两层bfs)
传送门: http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2031 Description The Barareh village is on f ...
随机推荐
- 5个Xcode开发调试技巧
转自Joywii的博客,原文:Four Tips for Debugging in XCode Like a Bro 1.Enable NSZombie Objects(开启僵尸对象) Enab ...
- HDU 4432 Sum of divisors (水题,进制转换)
题意:给定 n,m,把 n 的所有因数转 m 进制,再把各都平方,求和. 析:按它的要求做就好,注意的是,是因数,不可能有重复的...比如4的因数只有一个2,还有就是输出10进制以上的,要用AB.. ...
- ie6的兼容问题及解决方案
1.png24位的图片在ie6浏览器上会出现背景,解决方案是做成png8位: 2.浏览器默认的margin和padding不同,解决方法是用全局重置来统一,即是*{margin:0;padding:0 ...
- MVC4 EF6 MYSQL
在MVC的框架下连接mysql数据库 将EF框架升级到EF6 将NEW JSON升级到与之相匹配的版本 然后进行相应的配置就可以了
- 关于android 将对象写入文件以及从文件读取对象
由于项目需求,需要保存用户登录过的一些配置,当下次登录的时候读取登录过的配置,所以简单的SharePreferences没有办法满足,于是找到了Java中ObjectInputStream 与 Obj ...
- 设计模式六大原则——合成/聚合复用原则(CARP)
1.定义 简而言之,对于合成/聚合复用原则的定义就是:要尽量使用合成和聚合,尽量不要使用继承. 2.释义 为什么"要尽量使用合成和聚合.尽量不要使用继承"呢? 这是由于: 第一,继 ...
- fl,flash,mx包的区别
在ActionScript项目中还真是不能使用mx包中的UI组件. Adobe官方论坛上有一个帖子讲述了这个问题,大致意思是说:你要使用mx包中像Button这样的UI组件都是从U ...
- 第1章 游戏之乐——NIM(2)“拈”游戏分析
NIM(2)“拈”游戏分析 1. 问题 有N块石头和两个玩家A和B,玩家A先将石头分成若干堆,然后按照BABA……的顺序不断轮流取石头,能将剩下的石头一次取光的玩家获胜.每次取石头时,每个玩家只能从若 ...
- Web开发接口测试工具——Postman插件的使用(chrome浏览器)
Postman是chrome浏览器的一款插件.Postman 可以模拟 http 请求的发送,并自动解析 JSON 和 XML 的返回数据. 可以手动的去配置各类 parameter,还支持 Basi ...
- VBA Excel 常用 自定义函数
1. 将 互换 Excel 列号(数字/字母) Public Function excelColumn_numLetter_interchange(numOrLetter) As String Dim ...