首页 > 其他分享 >HDU 3760 Ideal Path

HDU 3760 Ideal Path

时间:2022-11-09 20:38:17浏览次数:32  
标签:3760 sz HDU ft room int pp number Path


Problem 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 input begins with an integer T. The next T blocks each represents a case. The first line of each case 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 ≤ 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 case, the first line of the output 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


1
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

 


最短路+字典序最小,bfs更新的时候+优先队列并且要去重


#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define rep(i,j,k) for (int i = j; i <= k; i++)
const int N = 4e5;
int n, m, T, x, y, z;
int ft[N], nt[N], u[N], v[N], sz;
int d[N][2], t[N];

void bfs(int x, int t)
{
queue<int> p; p.push(x); d[x][t] = 0;
while (!p.empty())
{
int q = p.front(); p.pop();
for (int i = ft[q]; i != -1; i = nt[i])
{
if (d[u[i]][t] != -1) continue;
d[u[i]][t] = d[q][t] + 1;
p.push(u[i]);
}
}
}

struct point
{
int x, y;
point(int x = 0, int y = 0) :x(x), y(y) {};
bool operator<(const point &a)const { return y > a.y; }
};

void Bfs()
{
printf("%d\n", d[n][0]);
priority_queue<point> pp;
queue<int> p; p.push(1);
int flag = 0;
for (int j = 1; j <= d[n][0]; j++)
{
while (!p.empty())
{
int q = p.front(); p.pop();
for (int i = ft[q]; i != -1; i = nt[i])
{
if (d[u[i]][0] + d[u[i]][1] != d[n][0]) continue;
if (d[u[i]][0] != d[q][0] + 1) continue;
pp.push(point(u[i], v[i]));
}
}
int q = pp.top().y, qq;
while (!pp.empty() && pp.top().y == q)
{
qq = pp.top().x;
if (t[qq] < j) p.push(pp.top().x), t[qq] = j;
pp.pop();
}
while (!pp.empty()) pp.pop();
if (flag) printf(" "); else flag = 1;
printf("%d", q);
}
putchar(10);
}

int main()
{
scanf("%d", &T);
while (T--)
{
scanf("%d%d", &n, &m);
memset(ft, -1, sizeof(ft));
memset(d, -1, sizeof(d));
memset(t, 0, sizeof(t));
for (sz = 0; m; m--)
{
scanf("%d%d%d", &x, &y, &z);
u[sz] = y; v[sz] = z; nt[sz] = ft[x]; ft[x] = sz++;
u[sz] = x; v[sz] = z; nt[sz] = ft[y]; ft[y] = sz++;
}
bfs(1, 0); bfs(n, 1); Bfs();
}
return 0;
}



标签:3760,sz,HDU,ft,room,int,pp,number,Path
From: https://blog.51cto.com/u_15870896/5838725

相关文章

  • HDU 4101 Ali and Baba
    ProblemDescriptionThereisarectanglearea(withNrowsandMcolumns)infrontofAliandBaba,eachgridmightbeoneofthefollowing:1.Empty......
  • HDU 4198 Quick out of the Harbour
    ProblemDescriptionCaptainClearbearddecidedtogototheharbourforafewdayssohiscrewcouldinspectandrepairtheship.Now,afewdayslater,......
  • HDU 2589 正方形划分
    ProblemDescription一个边长为L的正方形可以分成L*L个小正方形.有N个石子放在N个小正方形里,能否将它分成N个正方形,使得每个正方形里恰有一个石子且这N个正方......
  • HDU 3085 Nightmare Ⅱ
    ProblemDescriptionLastnight,littleerriyuehadahorriblenightmare.Hedreamedthatheandhisgirlfriendweretrappedinabigmazeseparately.Mo......
  • HDU 3313 Key Vertex
    ProblemDescriptionYouneedwalkingfromvertexStovertexTinagraph.IfyouremoveonevertexwhichstopsyoufromwalkingfromStoT,thatverte......
  • HDU 3316 Mine sweeping
    ProblemDescriptionIthinkmostofyouareusingsystemnamedofxporvistaorwin7.Andthesesystemisconsistofafamousgamewhatisminesweeping......
  • HDU 3355 Hop — Don’t Walk!
    ProblemDescriptionKERMITTHEFROGisaclassicvideogamewithasimplecontrolandobjectivebutrequiresagooddealofthinking.Youcontrolanani......
  • HDU 3427 Clickomania
    ProblemDescriptionClickomaniaisapuzzleinwhichonestartswitharectangulargridofcellsofdifferentcolours.Ineachstep,aplayerselects("......
  • HDU 2209 翻纸牌游戏
    ProblemDescription有一种纸牌游戏,很有意思,给你N张纸牌,一字排开,纸牌有正反两面,开始的纸牌可能是一种乱的状态(有些朝正,有些朝反),现在你需要整理这些纸牌。但是麻烦的是,......
  • HDU 2216 Game III
    ProblemDescriptionZjtandSarawilltakepartinagame,namedGameIII.ZjtandSarawillbeinamaze,andZjtmustfindSara.Therearesomestrang......