当前位置:网站首页>UVA1599理想路径题解
UVA1599理想路径题解
2022-07-28 12:37:00 【bj_hacker】
题目
链接
https://www.luogu.com.cn/problem/UVA1599
字面描述
给定一个n个点m条边的无向图,每条边上都涂有1种颜色。求点1到点n的一条路径,使得经过的边数最少,在此前提下,经过边的颜色序列最小。可能有自环与重边。输入保证至少存在一条连接1和n的道路。
2≤n≤10 ^5
1≤m≤2×10^5
1≤ci≤10^9
题目思路
- 从n反向遍历到1BFS,得出最短路和每层层数
- 从1向层-1的子节点父节点,进行比较记录最小值
代码实现
#include<bits/stdc++.h>
using namespace std;
const int maxn=4e5+10;
const int inf=1e9;
int n,m;
int dis[maxn],vis[maxn],que[maxn],ans[maxn];
vector< pair<int,int> >e[maxn];
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(a==b)continue;
e[a].push_back(make_pair(b,c));
e[b].push_back(make_pair(a,c));
}
for(int i=0;i<=n;i++)ans[i]=inf;
int head=0,tail=0;
que[tail++]=n;
vis[n]=true;
while(head<tail){
int x=que[head++];
if(x==1)break;
for(int i=0;i<e[x].size();i++){
if(!vis[e[x][i].first]){
vis[e[x][i].first]=true;
que[tail++]=e[x][i].first;
dis[e[x][i].first]=dis[x]+1;
}
}
}
//for(int i=1;i<=n;i++)printf("%d ",dis[i]);
//printf("\n");
printf("%d\n",dis[1]);
memset(vis,false,sizeof(vis));
vis[1]=true;
head=0,tail=0;
que[tail++]=1;
while(head<tail){
int x=que[head++];
for(int i=0;i<e[x].size();i++){
if(dis[e[x][i].first]==dis[x]-1){
vis[e[x][i].first]=true;
que[tail++]=e[x][i].first;
ans[dis[1]-dis[e[x][i].first]]=min(ans[dis[1]-dis[e[x][i].first]],e[x][i].second);
}
}
}
for(int i=1;i<=dis[1];i++)printf("%d ",ans[i]);
return 0;
}
边栏推荐
猜你喜欢

Leetcdoe-342. Power of 4
![[报错]使用ssh登陆到另一台机器后,发现主机名还是自己|无法访问yarn8088](/img/81/641a5b3445534fc3b8c87ee6deaa64.png)
[报错]使用ssh登陆到另一台机器后,发现主机名还是自己|无法访问yarn8088

少儿编程 电子学会图形化编程等级考试Scratch二级真题解析(判断题)2022年6月

国产API管理工具Eolink太好用了,打造高效的研发利器

Customized template in wechat applet

ES6 null merge operator (?)

Why is crypto game changing the game industry?

从手机厂高位“出走”的三个男人

SAP UI5 FileUploader 控件实现本地文件上传,接收服务器端的响应时遇到跨域访问错误的试读版

Auto.js enables Taobao to quickly submit orders
随机推荐
面经整理,助力秋招,祝你称为offer收割机
Guide for using IP phone system and VoIP system
国产口服新冠药阿兹夫定安全吗?专家权威解读
C语言学生成绩管理系统详解[通俗易懂]
拒绝服务 DDoS 攻击
什么叫杂谈(e网杂谈)
长封闭期私募产品再现 业内人士看法各异
JS encapsulation at a glance
[报错]使用ssh登陆到另一台机器后,发现主机名还是自己|无法访问yarn8088
C language: quick sorting of sequential storage structure
基于深度学习的超分辨率重建
验证码暴力破解测试[通俗易懂]
剖析 kubernetes 集群内部 DNS 解析原理
Beyond Istio OSS——Istio服务网格的现状与未来
Today's sleep quality record 75 points
《如何打一场数据挖掘赛事》入门版
Jenkins--持续集成服务器
Have a part of the game, after NFT is disabled in my world
不用Swagger,那我用啥?
《暗黑破坏神4》PS4/PS5测试版已加入PlayStation数据库