# Uva 1599 / POJ 3967 Ideal Path：不一样的 BFS

/ 1评 / 0

POJ 上居然不能用 vector ！！！强烈不满！

（本博客以 UVa 上的题目为准）

## Problem

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 (a1, a2, . . . , ak) is lexicographically smaller than a sequence (b1, b2, . . . , bk) if there exists i such that ai < bi , and aj = bj for all j < i.

## Input

The input file contains several test cases, each of them as described below.
The first line of the input file contains integers n and m — the number of rooms and passages, respectively (2 ≤ n ≤ 100000, 1 ≤ m ≤ 200000). The following m lines describe passages, each passage is described with three integer numbers: ai, bi, and ci — the numbers of rooms it connects and its color (1 ≤ a_i, b_i ≤ n, 1 ≤ c_i ≤ 10^9). 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

For each test case, the output must follow the description below.
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


## Analysis

emm，咨询 dalao 听说，据说 POJ 是不能用 vector 的……害怕……

## Code

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn=100005,maxe=400005;
int T,n,e,dst[maxn],INF,dist=0;
bool vis[maxn];
struct VertexInfo{
int x,c;
};
vector<int> ans,edge[maxn],color[maxn];
vector<VertexInfo> now,nxt;
queue<int> que;
int ret=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar();
return ret*f;
}
inline void init(){
memset(dst,0x3f,sizeof(dst));INF=dst[0];
for (int i=1;i<=n;i++) edge[i].clear(),color[i].clear();
}
inline void BFS(int s){
memset(vis,0,sizeof(vis));
que.push(s);vis[s]=1;dst[s]=0;
while (!que.empty()){
int x=que.front();que.pop();
for (int i=0;i<edge[x].size();i++) if (!vis[edge[x][i]]){
vis[edge[x][i]]=true;
dst[edge[x][i]]=dst[x]+1;
que.push(edge[x][i]);
}
}
}
inline bool CompareColor(VertexInfo aa,VertexInfo bb){
return aa.c<bb.c||(aa.c==bb.c&&aa.x<bb.x);
}
inline void GetAns(int s){
memset(vis,0,sizeof(vis));ans.clear();
//Init Vector "now"
vis[s]=1;now.clear();
now.push_back((VertexInfo){s,-1});

for (int t=1;t<=dist;t++){ // Do BT BFS
nxt.clear();
for (int i=0;i<now.size();i++){
}
}

now.clear();
sort(nxt.begin(),nxt.end(),CompareColor);
for (int i=0;i<nxt.size()&&nxt[i].c==nxt[0].c;i++){
if (i!=0&&nxt[i].x==nxt[i-1].x) continue;
now.push_back(nxt[i]);
vis[nxt[i].x]=true;
}
ans.push_back(now[0].c);
}
}
int main(){
while (scanf("%d%d",&n,&e)!=-1){
init();
for (int i=1;i<=e;i++){
edge[x].push_back(y);color[x].push_back(z);
edge[y].push_back(x);color[y].push_back(z);
}
BFS(n);dist=dst[1];
GetAns(1);
printf("%d\n",(int)ans.size());
printf("%d",ans[0]);
for (int i=1;i<ans.size();i++) printf(" %d",ans[i]);
printf("\n");
}
return 0;
}


### 一条回应：“Uva 1599 / POJ 3967 Ideal Path：不一样的 BFS”

1. CaptainSlow说道：

小盖哥牛逼啊！不过我好像没怎么看懂…

• 默认
• 护眼
• 夜晚
• Serif
• Sans