当前位置:网站首页>Uva1599 ideal path problem solution
Uva1599 ideal path problem solution
2022-07-28 13:49:00 【bj_ hacker】
UVA1599 Solution to the problem of ideal path
subject
link
https://www.luogu.com.cn/problem/UVA1599
Literal description
Given a n A little bit m Undirected graph of strip and edge , Each side is painted 1 Color . Find the point 1 point-to-point n A path for , To minimize the number of sides to pass , Under this premise , The color sequence passing through the edge is the smallest . There may be self rings and double edges . Input ensures that at least one connection exists 1 and n The path of .
2≤n≤10 ^5
1≤m≤2×10^5
1≤ci≤10^9
Topic ideas
- from n Traverse backward to 1BFS, Get the shortest circuit and the number of layers of each layer
- from 1 Directional layer -1 The child node of the parent node , Compare and record the minimum
Code implementation
#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;
}
边栏推荐
- 数据库系统原理与应用教程(061)—— MySQL 练习题:操作题 21-31(五)
- R语言可视化散点图、使用ggrepel包的geom_text_repel函数避免数据点之间的标签互相重叠(使用参数xlim和ylim将标签添加到可视化图像的特定区域、指定标签线段并添加箭头)
- 微信小程序中自定义模板
- Li Kou sword finger offer 51. reverse order pairs in the array
- [C language] the difference between structure pointer and structure variable as formal parameters
- Beyond istio OSS -- current situation and future of istio Service Grid
- 剖析 kubernetes 集群内部 DNS 解析原理
- 记一次使用pdfbox解析pdf,获取pdf的关键数据的工具使用
- Today's sleep quality record 75 points
- 算法---不同路径(Kotlin)
猜你喜欢

Half wave rectification light LED

Jenkins--持续集成服务器

持续(集成--&gt;交付--&gt;部署)

SQL daily practice (Niuke new question bank) - day 4: advanced operators

Realize the mutual value transfer between main window and sub window in WPF

基于神经网络的帧内预测和变换核选择

word打字时后面的字会消失是什么原因?如何解决?

【安全】 阅读 RFC6749 及理解 Oauth2.0 下的授权码模式

Denial of service DDoS Attacks

接口调不通,如何去排查?没想到10年测试老鸟栽在这道面试题上
随机推荐
[apue] 文件中的空洞
I miss the year of "losing" Li Ziqi
使用 Fail2ban 保护 Web 服务器免受 DDoS 攻击
接口调不通,如何去排查?没想到10年测试老鸟栽在这道面试题上
C language: random generated number + merge sort
R语言使用dpois函数生成泊松分布密度数据、使用plot函数可视化泊松分布密度数据(Poisson distribution)
Jenkins -- continuous integration server
Night God simulator packet capturing wechat applet
After finishing, help autumn move, I wish you call it an offer harvester
111. SAP UI5 FileUploader 控件实现本地文件上传,接收服务器端的响应时遇到跨域访问错误
要想组建敏捷团队,这些方法不可少
Countdown 2 days! 2022 China Computing Conference: Mobile cloud invites you to meet with computing network for innovative development
R语言使用lm函数构建线性回归模型、使用subset函数指定对于数据集的子集构建回归模型(使用floor函数和length函数选择数据前部分构建回归模型)
30天刷题计划(四)
C language: random number + quick sort
Facial expression recognition based on pytorch convolution - graduation project "suggestions collection"
长封闭期私募产品再现 业内人士看法各异
Socket类关于TCP字符流编程的理解学习
[dark horse morning post] byte valuation has shrunk to $270billion; "Second uncle" video author responded to plagiarism; Renzeping said that the abolition of the pre-sale system of commercial housing
二舅能治好年轻人的精神内耗吗?