Ideal Path
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 1754   Accepted: 240

Description

New labyrinth attraction is open in New Lostland amusement park. The labyrinth consists of n rooms connected by m passages. Each passage is colored into some color ci. Visitors of the labyrinth are dropped from the helicopter to the room number 1 and their goal is to get to the labyrinth exit located in the room number n.

Labyrinth owners are planning to run a contest tomorrow. Several runners will be dropped to the room number 1. They will run to the room number n writing down colors of passages as they run through them. The contestant with the shortest sequence of colors is the winner of the contest. If there are several contestants with the same sequence length, the one with the ideal path is the winner. The path is the ideal path if its color sequence is the lexicographically smallest among shortest paths.

Andrew is preparing for the contest. He took a helicopter tour above New Lostland and made a picture of the labyrinth. Your task is to help him find the ideal path from the room number 1 to the room number n that would allow him to win the contest.

Note

A sequence (a1a2, . . . , ak) is lexicographically smaller than a sequence (b1b2, . . . , bk) if there exists i such that ai < bi, and aj = bj for all j < i.

Input

The first line of the input file contains integers n and m —the number of rooms and passages, respectively (2 <= n <= 100 000, 1 <= m <= 200 000). The following m lines describe passages, each passage is described with three integer numbers: aibi, and ci — the numbers of rooms it connects and its color (1 <= aibi <= n, 1 <= ci <= 109). Each passage can be passed in either direction. Two rooms can be connected with more than one passage, there can be a passage from a room to itself. It is guaranteed that it is possible to reach the room number n from the room number 1.

Output

The first line of the output file must contain k — the length of the shortest path from the room number 1 to the room number n. The second line must contain k numbers — the colors of passages in the order they must be passed in the ideal path.

Sample Input

4 6
1 2 1
1 3 2
3 4 3
2 3 1
2 4 4
3 1 1

Sample Output

2
1 3

Source


题意:路径最短,颜色字典序最小

从白书上看着的,用的白书做法
倒着bfs一遍得到层次图
然后按层次图bfs,每次选择当前层次向下一层次中颜色最小的边练的点加进下一层次集合中
 
速度还可以了

16 16123402(2) thwfhk 7320K 3360MS G++ 2083B 2016-09-25 23:16:16
//
// main.cpp
// poj3967
//
// Created by Candy on 9/25/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=1e5+,M=2e5+,INF=1e9+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x;
}
int n,m,u,v,w;
struct edge{
int v,w,ne;
}e[M<<];
int h[N],cnt=;
inline void ins(int u,int v,int w){
cnt++;
e[cnt].v=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=cnt;
cnt++;
e[cnt].v=u;e[cnt].w=w;e[cnt].ne=h[v];h[v]=cnt;
}
int vis[N],q[N],head=,tail=;
int d[N];
void bfs1(){
q[++tail]=n;vis[n]=;
d[n]=;
while(head<=tail){
int u=q[head++];
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
if(vis[v]) continue;
vis[v]=;
d[v]=d[u]+;
q[++tail]=v;
}
}
}
int ans[N],lst[N],num=;
void bfs2(){
memset(ans,,sizeof(ans));
head=;tail=;
memset(q,,sizeof(q));
memset(vis,,sizeof(vis));
q[++tail]=;
while(head<=tail||num>=){
int mn=INF,dis=;num=;
while(head<=tail){
int u=q[head++];dis=d[u]; //printf("u %d\n",u);
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v,c=e[i].w;
if(d[v]!=d[u]-) continue;
if(c>mn) continue;
if(c<mn){
num=; mn=c;
lst[++num]=v;
}else lst[++num]=v;
}
}
ans[dis]=mn;
for(int i=;i<=num;i++)
if(!vis[lst[i]]){vis[lst[i]]=;q[++tail]=lst[i];}
}
}
int main(int argc, const char * argv[]) {
n=read();m=read();
for(int i=;i<=m;i++){
u=read();v=read();w=read();
if(u!=v) ins(u,v,w);
}
bfs1();
bfs2();
printf("%d\n",d[]-);
for(int i=d[];i>;i--) printf("%d ",ans[i]);
// cout<<"\n\n";
// for(int i=1;i<=n;i++) printf("%d ",d[i]);
return ;
}
 

POJ3967Ideal Path[反向bfs 层次图]的更多相关文章

  1. hdu 1689 Alien’s Necklace (bfs层次图剪枝)

    Alien's Necklace Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  2. UVA 1599, POJ 3092 Ideal Path 理想路径 (逆向BFS跑层次图)

    大体思路是从终点反向做一次BFS得到一个层次图,然后从起点开始依次向更小的层跑,跑的时候选则字典序最小的,由于可能有多个满足条件的点,所以要把这层满足条件的点保存起来,在跑下一层.跑完一层就会得到这层 ...

  3. 【HDU - 1043】Eight(反向bfs+康托展开)

    Eight Descriptions: 简单介绍一下八数码问题:在一个3×3的九宫格上,填有1~8八个数字,空余一个位置,例如下图: 1 2 3 4 5 6 7 8   在上图中,由于右下角位置是空的 ...

  4. UVA1599-Ideal Path(BFS进阶)

    Problem UVA1599-Ideal Path Time Limit: 3000 mSec Problem Description New labyrinth attraction is ope ...

  5. HDU 1043 Eight(反向BFS+打表+康托展开)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 题目大意:传统八数码问题 解题思路:就是从“12345678x”这个终点状态开始反向BFS,将各 ...

  6. POJ1077 Eight —— 反向BFS

    主页面:http://www.cnblogs.com/DOLFAMINGO/p/7538588.html 代码一:以数组充当队列,利用结构体中的pre追溯上一个状态在数组(队列)中的下标: #incl ...

  7. BZOJ_1195_[HNOI2006]最短母串_AC自动机+BFS+分层图

    BZOJ_1195_[HNOI2006]最短母串_AC自动机+BFS+分层图 Description 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2, ...

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

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

  9. 使用Architecture Explorer分析应用程序及使用层次图

    使用Architecture Explorer分析应用程序 Architecture Explorer和依赖图可以帮助我们了解所有的项目,包括小项目和大项目.Architecture Explorer ...

随机推荐

  1. (转)轻松学习JavaScript三:JavaScript与HTML的结合

    摘自:http://blog.csdn.net/erlian1992 HTML中的JavaScript脚本必须位于<script>与</script>标签之间,JavaScri ...

  2. 在 Xamarin.Android 中使用 Notification.Builder 构建通知

    0 背景 在 Android 4.0 以后,系统支持一种更先进的 Notification.Builder 类来发送通知.但 Xamarin 文档含糊其辞,多方搜索无果,遂决定自己摸索. 之前的代码: ...

  3. 关于WPF中文件夹浏览对话框的方式

    文件夹浏览时dialogresult要写全引用路径 string path=null; FolderBrowserDialog fbd = new FolderBrowserDialog(); fbd ...

  4. 一个UILabel不同部分显示不同颜色

    我们直接来看效果图吧: 需求:就是表格cell里面的状态Label,前面的"状态:"是黑色,后面的状态值是红色,他们在同一个Label上,怎么做呢? 解答:真的是会者不难,难者不会 ...

  5. iOS开发200个tips总结(一)

    tip 1 :  给UIImage添加毛玻璃效果 func blurImage(value:NSNumber) -> UIImage { let context = CIContext(opti ...

  6. vs2012中使用Icenium开发应用

    Developing a Cross Platform Mobile App in Visual Studio using JavaScript & HTML5 http://www.dotn ...

  7. 转载:检测到有潜在危险的 Request.Form 值

    转载:检测到有潜在危险的 Request.Form 值 金刚 ASP.NET Request.Form 这是一篇转载的文章,文章原始出处.点我 这种问题是因为你提交的Form中有HTML字符串,例如你 ...

  8. JSON字符串与JSON对象

    JSON对象是直接可以使用JQuery操作的格式,和js中的对象一样,可以用对象(类名)点出属性(方法). JSON字符串仅仅只是一个字符串,一个整体,不截取的话没办法取出其中存储的数据,不能直接使用 ...

  9. vs.net git版本仓库使用 之解决冲突方法 原创

    vs.net git 之解决冲突方法 如果别人已经修改推送到服务器,但自已本地未进行同部更新,那么就会出现要解决冲突的提示! 具体解决方法为: ... ... 下载word离线版:vs.net_git ...

  10. ror 在windows下开发的坑

    虽然知道ror在windows下的坑很多很多,但是目前没有先将就入门学习,也不折腾了.后面等待新机器来了,用linux来搭平台,先记录一下遇到的坑. 1.views/layouts/applicati ...