当前位置:网站首页>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;
}
边栏推荐
- R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图、设置draw_quantiles参数添加指定分位数横线(例如,50%分位数、中位数)
- 图的遍历(BFS&&DFS基础)
- R语言因子数据的表格和列联表(交叉表)生成:使用summay函数分析列表查看卡方检验结果判断两个因子变量是否独立(使用卡方检验验证独立性)
- vite在项目中配置路径别名
- Merge table rows - three levels of for loop traversal data
- Using fail2ban to protect web servers from DDoS Attacks
- 今日睡眠质量记录75分
- Deployment之滚动更新策略。
- 《如何打一场数据挖掘赛事》入门版
- Cool operation preheating! Code to achieve small planet effect
猜你喜欢

Map tiles: detailed explanation of vector tiles and grid tiles

Children's programming electronic society graphical programming level examination scratch Level 2 real problem analysis (judgment question) June 2022

Cool operation preheating! Code to achieve small planet effect

The domestic API management tool eolink is very easy to use, creating an efficient research and development tool
![[security] read rfc6749 and understand the authorization code mode under oauth2.0](/img/dc/e6d8626195b2e09a6c06050a9b552e.jpg)
[security] read rfc6749 and understand the authorization code mode under oauth2.0

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

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

Humiliation, resistance, reversal, 30 years, China should win Microsoft once

30天刷题计划(三)

《如何打一场数据挖掘赛事》入门版
随机推荐
R语言使用lm函数构建多元回归模型(Multiple Linear Regression)、并根据模型系数写出回归方程、使用confint函数给出回归系数的95%置信区间
I miss the year of "losing" Li Ziqi
Excellent performance! Oxford, Shanghai, AI Lab, Hong Kong University, Shangtang, and Tsinghua have joined forces to propose a language aware visual transformer for reference image segmentation! Open
Go language - Application of stack - expression evaluation
Denial of service DDoS Attacks
Rust from introduction to mastery 01 introduction
我秃了!唯一索引、普通索引我该选谁?
.NET的求复杂类型集合的差集、交集、并集
Tutorial on the principle and application of database system (062) -- MySQL exercise questions: operation questions 32-38 (6)
半波整流点亮LED
POJ3268最短路径题解
数据库系统原理与应用教程(062)—— MySQL 练习题:操作题 32-38(六)
DXF读写:对齐尺寸标注文字居中、上方的位置计算
R language uses LM function to build linear regression model and subset function to specify subset of data set to build regression model (use floor function and length function to select the former pa
Can second uncle cure young people's spiritual internal friction?
Tutorial on the principle and application of database system (059) -- MySQL exercise questions: operation questions 1-10 (III)
Jar package
Some thoughts on.Net desktop development
How to play a data mining game entry Edition
【黑马早报】字节估值缩水,降至2700亿美元;“二舅”视频作者回应抄袭;任泽平称取消商品房预售制是大势所趋;美联储宣布再加息75个基点...