## 舒哥的blog

www.6aiq.com AIQ-机器学习大数据技术社区 全国最专业的机器学习大数据技术社区。全国IT精英群号:467123855...

# UVa 1599 Ideal Path

。这里绝对是个技巧。因为队列中出来的元素必然是兄弟结点全部出来完之后， 才会弹出这些兄弟结点的第一个作为接下来子树的根节点，所以用层数数组可以记录一层边

3 2

1 3 2

2 3 3

1

2

/*=============================================================================
#
#      Author: liangshu - cbam
#
#      QQ : 756029571
#
#      School : 哈尔滨理工大学
#
#
#     Filename: H.cpp
#
#     Description:
#        The people who are crazy enough to think they can change the world, are the ones who do !
=============================================================================*/

#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<cctype>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;
const int INF=400004;
int inq[INF];
int cnt[INF];
int low[INF];
int n,m,c;
struct Edge
{
int next_edge,v,t,val;
} edge[INF];

void bfs1(int p){
memset(low, -1, sizeof(low));
low[p] = 0;
queue<int>q;
q.push(p);
while(!q.empty()){
int u = q.front();
if(u == 1){
return ;
}
q.pop();
for(int e = head[u]; e != -1; e = edge[e].next_edge){
if(low[edge[e].v] == -1 && edge[e].v != n){
low[edge[e].v] = low[u] + 1;
q.push(edge[e].v);

}
}
}
}
struct Node
{
int x;
Node(int x):x(x){
}
};
int ans[INF];
int vis[INF];
void bfs(int p){
memset(ans, 0, sizeof(ans));
memset(vis, 0, sizeof(vis));
queue<Node>q;
q.push(Node(p));
while(!q.empty()){
int e = q.front().x;
int tmp = -1;
q.pop();
for(int i = head[e]; i != -1; i = edge[i].next_edge){
if(low[edge[i].v] == low[e] - 1){
if(tmp == -1){
tmp = edge[i].val;
}
else{
tmp = min(edge[i].val, tmp);
}
}
}
int t = low[1] - low[e];
if(ans[t] == 0){
ans[t] = tmp;
}
else{
ans[t] = min(tmp, ans[t]);
}
for(int i = head[e]; i != -1; i = edge[i].next_edge){
if(!vis[edge[i].v] && edge[i].val == tmp  && low[edge[i].v] == low[e] - 1){
q.push(edge[i].v);
vis[edge[i].v] = 1;
}
}
}
}
int main()
{
int x,y,z;
while(scanf("%d%d", &n, &m) != EOF)
{
for(int i=0; i<m; i++)
{
scanf("%d%d%d", &x, &y, &z);
if(x == y){
continue;
}
edge[i].v=y;
edge[i].t=1;
edge[i].val = z;

edge[i+m].v=x;
edge[i+m].t=1;
edge[i+ m].val = z;
}
bfs1(n);
bfs(1);
printf("%d\n%d", low[1], ans[0]);
for(int i = 1; i < low[1]; i++)
printf(" %d", ans[i]);
printf("\n");
}
return 0;
}
/*
4 5
1 2 3
2 3 345
3 4 2
4 2 2
1 3 1
*/


02-10 2571

04-11 78

03-24 225

05-05 63

08-06 162

07-29 930

06-18 553

03-17 226

02-08 1780

02-26 161