POJ3967Ideal Path[反向bfs 层次图]

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

Northeastern Europe 2010


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


从白书上看着的,用的白书做法

倒着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+5,M=2e5+5,INF=1e9+5;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();}
    while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();}
    return x;
}
int n,m,u,v,w;
struct edge{
    int v,w,ne;
}e[M<<1];
int h[N],cnt=0;
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=1,tail=0;
int d[N];
void bfs1(){
    q[++tail]=n;vis[n]=1;
    d[n]=1;
    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]=1;
            d[v]=d[u]+1;
            q[++tail]=v;
        }
    }
}
int ans[N],lst[N],num=0;
void bfs2(){
    memset(ans,127,sizeof(ans));
    head=1;tail=0;
    memset(q,0,sizeof(q));
    memset(vis,0,sizeof(vis));
    q[++tail]=1;
    while(head<=tail||num>=1){
        int mn=INF,dis=0;num=0;
        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]-1) continue;
                if(c>mn) continue;
                if(c<mn){
                    num=0; mn=c;
                    lst[++num]=v;
                }else lst[++num]=v;
            }
        }
        ans[dis]=mn;
        for(int i=1;i<=num;i++)
            if(!vis[lst[i]]){vis[lst[i]]=1;q[++tail]=lst[i];}
    }
}
int main(int argc, const char * argv[]) {
    n=read();m=read();
    for(int i=1;i<=m;i++){
        u=read();v=read();w=read();
        if(u!=v) ins(u,v,w);
    }
    bfs1();
    bfs2();
    printf("%d\n",d[1]-1);
    for(int i=d[1];i>1;i--) printf("%d ",ans[i]);
  //  cout<<"\n\n";
  //  for(int i=1;i<=n;i++) printf("%d ",d[i]);
    return 0;
}
时间: 2024-10-23 12:30:33

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

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

Alien's Necklace Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1526    Accepted Submission(s): 415 Problem Description JYY is taking a trip to Mars. To get accepted by the Martians, he decid

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

Eight Descriptions: 简单介绍一下八数码问题:在一个3×3的九宫格上,填有1~8八个数字,空余一个位置,例如下图: 1 2 3 4 5 6 7 8   在上图中,由于右下角位置是空的,你可以移动数字,比如可以将数字6下移一位: 1 2 3   1 2 3 4 5 6 → 4 5   7 8     7 8 6 或者将数字8右移一位: 1 2 3   1 2 3 4 5 6 → 4 5 6 7 8     7   8 1~8按顺序排列的情况称为"初始状态"(如最上方图)

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 题目大意:传统八数码问题 解题思路:就是从“12345678x”这个终点状态开始反向BFS,将各个状态记录下来,因为数字太大所以用康托展开将数字离散化. 代码: 1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstring> 5 #include<st

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

题意: 给定一个有重边有自环的无向图,n个点(2 <= n <= 100000), m条边(1 <= m <= 200000), 每条边有一个权值, 求从第一个点到n的最少步数, 如果最少步数相同有多条路径, 那么输出权值字典序最小的一条. 分析: 用BFS解决最短路问题, 可以先从终点BFS, 求出每个点到终点的最短距离. 那么最少步数就是起点的最短距离, 最短路径就是从起点每次向最短距离比自己少1的顶点移动(如果有多个则可以随便走), 这样就可以保证走的是最短路径, 如果一开始

《西游记》《封神榜》各路神仙基本层次图,不要再傻傻分不清楚了

<西游记><封神榜>各路神仙基本层次图 看<西游记>及<封神榜>常常被里面的神仙名称眼花缭乱,现总结如下: *第一级:创始元灵 宇宙诞生之初,有一先天混元之元灵,灵窍初开,"创始元灵"成为宇宙间唯一无上灵力,创始元灵分别传授给四个小生灵一门修行的法门:老大"鸿钧老祖",修玄清气:老二"混鲲祖师",修玄灵气:老三"女娲娘娘",修玄空气:老四"陆压道君",修玄明气

Spring层次图

Spring层次图 说明: 1.Strus2充当web层,接管jsp/action表单,主要体现出MVC的数据输入.数据处理.数据分离显示. 2.平时所讲的model是一个很大的概念,会包括业务层.dao层和持久层.但在一个项目中不一定三个层都有,可以根据实际情况选择. 3.hibernate  OOP主要解决关系模型和对象模型间的阻抗. 4.Spring框架,它可以管理web层.dao层.业务层.持久层,spring可以配置各个层的bean组件(bean)并且维护各个bean之间的关系. Sp

浅谈IM软件业务知识—会话session的概念,附一张IM软件的层次图

session一般出现在计算机领域,IM软件中的session,老的IM有两层:首先是逻辑层的session来管理会话的参与者,消息列表,会话类型等等:还有协议层的session,主要是代表客户端跟服务器的一个事物通道. 老的IM软件 客户端跟Server交互的每一类操作都是基于会话.比如客户端登录,需要建立一个登录的会话:客户端发消息,需要建立一个会话.下面举例: 客户端向Server发了一条消息,这条消息的发送就建立在会话之上.客户端需要下面几个步骤. 1. 创建一个session ID=1

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

使用Architecture Explorer分析应用程序 Architecture Explorer和依赖图可以帮助我们了解所有的项目,包括小项目和大项目.Architecture Explorer可用于研究现有代码,还可以用依赖图选择要研究的代码并对其进行可视化. Architecture Explorer将结构表示为节点,关系表示为连接,下图为WatiN框架项目下的图示: 最左侧的第一个选项的功能是从当前在Architecture Explorer中选中的所有节点生成新的依赖图文件.如果仅

AOJ 0121 Seven Puzzle(反向BFS+map)

卡了很久的一道题,之前用正向BFS硬缸,结果凉凉,下面是之前错误的代码,仅供自己记录,他人浏览请直接跳过: 1 #include<cstring> 2 #include<iostream> 3 #include<queue> 4 #include<algorithm> 5 #include<cstdio> 6 7 using namespace std; 8 9 int tmaze[2][4]; 10 struct node{ 11 int ma