标签:turn memset next dijkstra names scanf while push 次短路
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int n,r,head[1000005],dis1[1000005],dis2[1000005],vis[1000005],cnt;
struct edge{
int v,w,next;
}e[1000005];
struct node{
int dis,u;
bool operator<(const node &x)const{return x.dis<dis;}
};
inline void add(int u,int v,int w){
e[++cnt].v=v;
e[cnt].w=w;
e[cnt].next=head[u];
head[u]=cnt;
}
inline void dijkstra(){
priority_queue<node> q;
q.push((node){0,1});
dis1[1]=0;
while(!q.empty()){
int x=q.top().u;
int dis=q.top().dis;//必须有
q.pop();
for(int i=head[x];i!=-1;i=e[i].next){
int y=e[i].v;
if(dis1[y]>=dis+e[i].w){
dis2[y]=dis1[y];
dis1[y]=dis+e[i].w;
q.push((node){dis1[y],y});
}
else if(dis2[y]>dis+e[i].w){
dis2[y]=dis+e[i].w;
q.push((node){dis2[y],y});//必须有
}
}
}
}//“必须有”因为不只考虑最优的
int main(){
memset(head,-1,sizeof(head));
memset(dis1,0x3f,sizeof(dis1));
memset(dis2,0x3f,sizeof(dis2));
scanf("%d%d",&n,&r);
for(int i=1;i<=r;i++){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
dijkstra();
printf("%d\n",dis2[n]);
}
标签:turn memset next dijkstra names scanf while push 次短路
原文地址:https://www.cnblogs.com/Y15BeTa/p/10976977.html