CCF(地铁修建):向前星+dijikstra+求a到b所有路径中最长边中的最小值
2024-08-31 08:13:48
地铁修建
201703-4
- 这题就是最短路的一种变形,不是求两点之间的最短路,而是求所有路径中的最长边的最小值。
- 这里还是使用d数组,但是定义不同了,这里的d[i]就是表示从起点到i的路径中最长边中的最小值。
- 在松弛的时候,注意是d[i]>max(d[u],cost),max保证了是所有路径中的最长边,>号保证了是最小值。
- 这里使用前向星+优先队列对dijikstra算法进行了优化。
//#include <iostream>
//#include<cstdio>
//#include<algorithm>
//#include<cstring>
#include<bits/stdc++.h>
using namespace std;
const int maxn=100005;
const int maxm=200005;
const int INF=0X3F3F3F3F;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int n,m;
int e;
struct node{
int dis;
int to;
bool operator<(const node& t)const{
return dis>t.dis;
}
};
struct edge{
int to;
int cost;
int next;
};
edge edges[2*maxm];
int head[maxn];
int d[maxn];//d[i]表示起点到i的所有路径中最大边的最小值
void dijikstra(int s){
priority_queue<node>q;
q.push(node{0,s});
memset(d,INF,sizeof(d));
d[s]=0;
while(!q.empty()){
node temp=q.top();
q.pop();
int u=temp.to;
int dis=temp.dis;
if(d[u]<dis){
continue;
}
for(int i=head[u];i!=-1;i=edges[i].next) {
edge ed=edges[i];
if(d[ed.to]>max(d[u],ed.cost)){
d[ed.to]=max(d[u],ed.cost);
q.push(node{d[ed.to],ed.to});
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
memset(head,-1,sizeof(head));
for(int i=0;i<m;i++){
int a,b,c;
cin>>a>>b>>c;
edges[e].to=b;
edges[e].cost=c;
edges[e].next=head[a];
head[a]=e++;
edges[e].to=a;
edges[e].cost=c;
edges[e].next=head[b];
head[b]=e++;
}
dijikstra(1);
cout<<d[n]<<endl;
return 0;
}
最新文章
- Python处理Excel表格
- session在本地可以正常使用,而在sae上却无法使用或者值为空的解决方法
- hadoop日常运维与升级总结
- 某篇ctr预估ppt的链接
- hdu 1034 (preprocess optimization, property of division to avoid if, decreasing order process) 分类: hdoj 2015-06-16 13:32 39人阅读 评论(0) 收藏
- PL/SQL
- OpenNMS界面图 .
- Ext.ComponentQuery.query()
- css3图片3D翻转效果
- PHPSingleton模式的例子
- C语言库函数大全及应用实例九
- 编写高性能SQL的注意事项
- net读取文件字节流要注意的小细节
- 20175211 2018-2019-2 《Java程序设计》第五周学习总结
- ensp 单臂路由实验
- [转]如何正确学习JavaScript
- Android中的task和stack
- springboot深入学习(四)-----tomcat配置、websocket
- 获取iOS 设备上崩溃日志 (Crash Log)的方法
- 20145214《网络对抗》MAL_后门原理与实践
热门文章
- 【uva 177】Paper Folding(算法效率--模拟)
- zoj3593One Person Game (扩展欧几里德)
- 8.PowerShell DSC之Push
- Redis-sentinel 哨兵(HA)
- HashMap三百问
- JavaScript预编译过程理解
- Steam 钓鱼模拟器
- how to auto open demo and create it in a new codesandbox
- Apple CSS Animation All In One
- onsen &; UI &; vue &; mobile UI