地铁修建

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;
}

最新文章

  1. Python处理Excel表格
  2. session在本地可以正常使用,而在sae上却无法使用或者值为空的解决方法
  3. hadoop日常运维与升级总结
  4. 某篇ctr预估ppt的链接
  5. hdu 1034 (preprocess optimization, property of division to avoid if, decreasing order process) 分类: hdoj 2015-06-16 13:32 39人阅读 评论(0) 收藏
  6. PL/SQL
  7. OpenNMS界面图 .
  8. Ext.ComponentQuery.query()
  9. css3图片3D翻转效果
  10. PHPSingleton模式的例子
  11. C语言库函数大全及应用实例九
  12. 编写高性能SQL的注意事项
  13. net读取文件字节流要注意的小细节
  14. 20175211 2018-2019-2 《Java程序设计》第五周学习总结
  15. ensp 单臂路由实验
  16. [转]如何正确学习JavaScript
  17. Android中的task和stack
  18. springboot深入学习(四)-----tomcat配置、websocket
  19. 获取iOS 设备上崩溃日志 (Crash Log)的方法
  20. 20145214《网络对抗》MAL_后门原理与实践

热门文章

  1. 【uva 177】Paper Folding(算法效率--模拟)
  2. zoj3593One Person Game (扩展欧几里德)
  3. 8.PowerShell DSC之Push
  4. Redis-sentinel 哨兵(HA)
  5. HashMap三百问
  6. JavaScript预编译过程理解
  7. Steam 钓鱼模拟器
  8. how to auto open demo and create it in a new codesandbox
  9. Apple CSS Animation All In One
  10. onsen &amp; UI &amp; vue &amp; mobile UI