2021-10-16

本文介绍了在New Lostland游乐园的新迷宫中,参赛者需从房间1到达房间n,通过记录经过的通道颜色来竞争最短路径。如果多条路径长度相同,则颜色序列最小的为优胜。题目提供了一种基于BFS的解决方案,寻找从1到n的理想路径。算法首先找到最短路径长度,然后逆向构造颜色序列。该文适合对图论和算法感兴趣的读者深入理解迷宫寻路问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

E . Ideal Path [ 问题 7218 ] [ 讨论 ]
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 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.

#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#pragma GCC optiize(2)
const int N=100005;
struct node
{
    int to,v;
    //friend operator
    node(int to=0,int v=0):to(to),v(v){}
} ;
vector<node>q[N];
vector<int>ans;
int dis[N];
int vis[N];
void bfs(int n)
{
    memset(vis,0,sizeof(vis));
    queue<int>p;
    dis[n]=0;
    vis[n]=1;
    p.push(n);
    while(p.size())
    {
        int d=p.front();p.pop();
        for(int i=0; i<q[d].size(); i++)
        {
            int v=q[d][i].to;
            if(!vis[v])
            {
                vis[v]=1;
                dis[v]=dis[d]+1;
                p.push(v);
            }
        }
    }
}
void bfs1(int n)
{
    ans.clear();
    vector<int>s;
    s.push_back(1);
    memset(vis,0,sizeof(vis));
    vis[1]=1;
    for(int i=0; i<dis[1]; i++)
    {
        int mincolor=INF;
        for(int j=0; j<s.size(); j++)
        {
            int u=s[j];
            for(int k=0; k<q[u].size(); k++)
            {
                int v = q[u][k].to, c = q[u][k].v;
                if (!vis[v] && dis[v] == dis[u] - 1)
                {
                    mincolor = min(mincolor, c);
                }
            }
        }
        ans.push_back(mincolor);
        vector<int>temp;
        for(int j=0; j<s.size(); j++)
        {
            int u=s[j];
            for(int k=0; k<q[u].size(); k++)
            {
                int v = q[u][k].to, c = q[u][k].v;
                if (!vis[v] && dis[v] == dis[u] - 1&&c==mincolor)
                {
                    temp.push_back(v);
                    vis[v]=1;
                }
            }
        }
        s=temp;
    }
}
int main()
{
    int n,m;
    while(~scanf("%d %d",&n,&m))
    {
        //memset(vis,0,sizeof(vis));
        memset(dis,-1,sizeof(dis));
        for(int i=1;i<=n;i++)
        {
            q[i].clear();
        }
        int a,b,c;
        for(int i=1; i<=m; i++)
        {
            
            scanf("%d %d %d",&a,&b,&c);
            q[a].push_back(node(b,c));
            q[b].push_back(node(a,c));
        }
        bfs(n);
        printf("%d\n",dis[1]);
        bfs1(n);
        for(int i=0; i<ans.size(); i++)
        {
            if(i!=0)
            printf(" %d",ans[i]);
            else
            printf("%d",ans[i]);
        }
        putchar('\n');
    }

    return 0;
}

vector(一次就开一个)。。。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值