第十八题 UVA1599 理想路径 Ideal Path

116 篇文章 1 订阅

PDF

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.
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

本题目的代码较之前长了很多,而且紫书上刘老师强烈建议写一写本题目,完全自己动手写出来的,还是有些成就感的…(不能膨胀,哈哈哈哈)
从n点跑一次BFS,求最短路,然后从1点正向BFS,找最小字典序,这时候vis数组判断是否已经访问过了
还有需要注意的就是 本题目 多组数据(我居然没看到,还在窃喜终于碰上个没有多组数据的题目,既然多组数据,注意自己的初始化)。 还有就是双向边 edge数组规模乘2,我因为这个RE了一次。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm> 
#include<queue>
#define Maxn 100005
#define Maxm 200005
using namespace std;
int n,m,head[Maxn],tot;
int dis[Maxn],col[Maxn];
bool exist[Maxn],vis[Maxn];
struct edge{ int u,v,next,col; }e[Maxm * 2];
queue<int> q;

void BFS() {
	q.push(n); dis[n] = 0; exist[n] = 1;
	while(!q.empty()) {
		int u = q.front(); q.pop(); exist[u] = 0;
		for(int i=head[u]; i; i=e[i].next) {
			int v = e[i].v;
			if(dis[v] > dis[u] + 1){
				dis[v] = dis[u] + 1;
				if(!exist[v]) { exist[v] = 1; q.push(v); }
			}
		}
	}
}

void BFS_2(){
	while(!q.empty()) q.pop();
	memset(exist,0,sizeof(exist));
	q.push(1); exist[1] = 1;
	while(!q.empty()) {
		int u = q.front(); q.pop(); exist[u] = 0; vis[u] = 1;
		int minc = dis[0];
		for(int i=head[u]; i; i=e[i].next) {
			int v = e[i].v;
			if(dis[v] == dis[u] - 1 && !vis[v]) minc = min(minc,e[i].col);
		}
		for(int i=head[u]; i; i=e[i].next) {
			int v = e[i].v;
			if(dis[v] == dis[u] - 1 && minc == e[i].col && !vis[v] && !exist[v]) {
				q.push(v); exist[v] = 1;
			}
		}
		int index = dis[1] - dis[u] + 1;
		col[index] = min(col[index],minc);
	}
}

int main(int argc,char *argv[]) {
	while(scanf("%d %d",&n,&m) == 2) {
		tot = 0;
		memset(head,0,sizeof(head));
		for(int u,v,d,i=1; i<=m; i++) {
			scanf("%d %d %d",&u,&v,&d);
			if(u == v) continue;
			e[++tot].u = u; e[tot].v = v; e[tot].col = d;
			e[tot].next = head[u]; head[u] = tot;
			e[++tot].u = v; e[tot].v = u; e[tot].col = d;
			e[tot].next = head[v]; head[v] = tot;// 我居然连双向边都不会了 
		}
		memset(dis,0x7f,sizeof(dis));
		memset(col,0x7f,sizeof(col));
		memset(exist,0,sizeof(exist));
		memset(vis,0,sizeof(vis));
		BFS();
		int lenth = dis[1];// ok
		printf("%d\n",lenth);
		BFS_2();
		for(int i=1; i<lenth; i++) printf("%d ",col[i]);
		printf("%d\n",col[lenth]);
	}
	return 0;
}

刘老师在书上写 可以只进行一次反向的BFS,但是咱不会 哈哈哈哈

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七情六欲·

学生党不容易~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值