Ideal Path (UVA - 1599 )

Ideal Path

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 ≤ ai
, bi ≤ 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
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.


bfs+思维;

一般bfs都是求边权为 1 的图的最短路,这道题初看上去就是bfs求下最短路,但是还有个要求就是经过的路径要求字典序最小,我们可以把每条边带的数字当做边权,又容易联想到求边权不为 1 的最短路题目。
但是最终却是求边权为 1 的最短路加上每条边带的数字字典序最小,先用反向bfs求每个点到终点 n 的路径长度,然后得到d[1],也就是1到n有几条边,我们枚举每条边,求出每条边的数值最小就行;

代码:

#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define lson k<<1
#define rson k<<1|1
#define inf 0x3f3f3f3f
//ios::sync_with_stdio(false);
using namespace std;
const int N=200100;
const int M=401000;
const LL mod=998244353;
int n,m;
vector<int>step;
bool vis[N];
struct Node{
	int to,nex,w;
}edge[M];
int cnt;
int head[N];
void add(int p,int q,int w){
	edge[cnt].w=w;
	edge[cnt].to=q;
	edge[cnt].nex=head[p];
	head[p]=cnt++;
}
int d[N];
void Init(){
	memset(head,-1,sizeof(head));
	memset(vis,false,sizeof(vis));
	memset(d,0,sizeof(d));
	step.clear();
	cnt=0;
}
struct Nod{
	int fi,s;
};
void bfs1(){
	queue<Nod>qu;
	Nod n1;
	n1.fi=n,n1.s=0,vis[n]=true;
	qu.push(n1);
	while(!qu.empty()){
		Nod n2=qu.front();
		qu.pop();
		int u=n2.fi;
		for(int i=head[u];~i;i=edge[i].nex){
			int v=edge[i].to;
			if(!vis[v]){
				vis[v]=true;
				Nod n3;
				n3.fi=v,n3.s=n2.s+1;
				d[v]=n3.s;
				qu.push(n3);
			}
		}
	}
}
void bfs2(){
	memset(vis,false,sizeof(vis));
	queue<int>q;
	q.push(1);
	vis[1]=true;
	vector<int>v1;
	for(int i=1;i<=d[1];i++){
		int mmin=2e9;
		while(!q.empty()){
			int p=q.front();
			v1.push_back(p);
			q.pop();
			for(int j=head[p];~j;j=edge[j].nex){
				int u=edge[j].to;
				if(d[p]==d[u]+1) mmin=min(mmin,edge[j].w);
			}
		}
		step.push_back(mmin);//总路径
		for(int j=0;j<v1.size();j++){
			for(int k=head[v1[j]];~k;k=edge[k].nex){
				int u=edge[k].to;
				if(d[v1[j]]==d[u]+1&&!vis[u]&&edge[k].w==mmin){
					vis[u]=true;
					q.push(u);
				}
			}
		}
		v1.clear();
	} 
	cout<<d[1]<<endl;
	for(int i=0;i<step.size()-1;i++) cout<<step[i]<<" ";
	cout<<step[step.size()-1]<<endl;
}
int main(){
	ios::sync_with_stdio(false);
	while(cin>>n>>m){
		Init();
		int p,q,w;
		for(int i=1;i<=m;i++){
			cin>>p>>q>>w;
			add(p,q,w);
			add(q,p,w);
		}
		bfs1();
		bfs2();
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值