博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ3967Ideal Path[反向bfs 层次图]
阅读量:6305 次
发布时间:2019-06-22

本文共 3966 字,大约阅读时间需要 13 分钟。

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 (
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 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: 
ai
bi, and 
ci — the numbers of rooms it connects and its color (1 <= 
ai
bi <= n, 1 <= 
ci <= 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

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 61 2 11 3 23 4 32 3 12 4 43 1 1

Sample Output

21 3

Source


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

从白书上看着的,用的白书做法
倒着bfs一遍得到层次图
然后按层次图bfs,每次选择当前层次向下一层次中颜色最小的边练的点加进下一层次集合中
 
速度还可以了
16 7320K 3360MS 2083B 2016-09-25 23:16:16
////  main.cpp//  poj3967////  Created by Candy on 9/25/16.//  Copyright © 2016 Candy. All rights reserved.//#include 
#include
#include
#include
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
1;i--) printf("%d ",ans[i]); // cout<<"\n\n"; // for(int i=1;i<=n;i++) printf("%d ",d[i]); return 0;}

 

 

转载地址:http://knixa.baihongyu.com/

你可能感兴趣的文章
JavaScript强化教程 —— Cocos2d-JS自动JSB绑定规则修改
查看>>
configure: error: in `/root/httpd-2.2.11/srclib/apr': c
查看>>
CentOS7搭建Kubernetes-dashboard管理服务
查看>>
buildroot下查找外部编译器通过ext-toolchain-wrapper调用的参数
查看>>
MySQL Replication 主主配置详细说明
查看>>
Linux的任务调度
查看>>
在Android studio中添加jar包方法如下
查看>>
iframe 在ie下面总是弹出新窗口解决方法
查看>>
分享10款漂亮实用的CSS3按钮
查看>>
安装nginx 常见错误及 解决方法
查看>>
Gorun8电子商城
查看>>
在之前链表的基础上改良的链表
查看>>
android编译系统makefile(Android.mk)写法
查看>>
MD5源代码C++
查看>>
Eclipse 添加 Ibator
查看>>
Linux中变量$#,$@,$0,$1,$2,$*,$$,$?的含义
查看>>
Python编程语言
查看>>
十四、转到 linux
查看>>
Got error 241 'Invalid schema
查看>>
ReferenceError: event is not defined
查看>>