码迷,mamicode.com
首页 > 其他好文 > 详细

POJ3967Ideal Path[反向bfs 层次图]

时间:2016-09-25 23:33:23      阅读:560      评论:0      收藏:0      [点我收藏+]

标签:

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+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;
}

 

 

POJ3967Ideal Path[反向bfs 层次图]

标签:

原文地址:http://www.cnblogs.com/candy99/p/5907540.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!