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

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)的更多相关文章

  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. Spring入门(7)-自动检测Bean

    Spring入门(7)-自动检测Bean 本文介绍如何自动检测Bean. 0. 目录 使用component-scan自动扫描 为自动检测标注Bean 1. 使用component-scan自动扫描 ...

  2. ManagementFactory (简介)

    Java Management Extensions (JMX) is a Java technology that supplies tools for managing and monitorin ...

  3. Usage of readonly and const

    Many new learners can not make sure the usage scenarios of readonly and const keywords. In my opinio ...

  4. Bootstrap栅格系统(布局)

    栅格系统(布局) Bootstrap内置了一套响应式.移动设备优先的流式栅格系统,随着屏幕设备或视口(viewport)尺寸的增加,系统会自动分为最多12列. 我在这里是把Bootstrap中的栅格系 ...

  5. maven使用.01.Hello World

    要说Java世界有什么东西是我最为留恋的:在写其他语言程序的时候,我最为想要的东西,那非maven莫属. 什么是Maven? Maven能做什么? Maven是一个针对Java的自动构建工具.所谓自动 ...

  6. PPTP + FreeRADIUS + MySQL 安装与配置

    原文地址:http://www.zhukun.net/archives/5375 PPTP + FreeRADIUS + MySQL 安装与配置 2012/03/29Linux运维centos.Fre ...

  7. 【转】GCC使用简介

    Linux系统下的gcc(GNU C Compiler)是GNU推出的功能强大.性能优越的多平台编译器,是GNU的代表作品之一.gcc是可以在多种硬体平台上编译出可执行程序的超级编译器,其执行效率与一 ...

  8. MFC Windows程序设计源代码免费下载

    本人近期在网上找到了<MFC Windows程序设计>第二版的书内程序的源代码,特意上传CSDN上面,供学习MFC的程序猿们免费下载. 源代码下载: http://download.csd ...

  9. Proteus仿真_01、 8086 IO译码仿真

    最近在学习一些微机原理与接口技术方面的知识. 参考书籍<微机原理与接口技术---基于8086Proteus仿真> 顾晖 梁惺彦 编著 实验一.利用8086 芯片来实现对I/O设备的读取和控 ...

  10. springMVC中的Controller里面定义全局变量

    转自:http://notebookdong.iteye.com/blog/1869852 使用SpringMVC的时候,如果想要在Controller中定义一个全局变量,并且实现在不同用户访问程序的 ...