• luogu2865 路障 (dijkstra)


    求次短路,dijkstra时同时记下到某点的最短距离和次短距离即可。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<vector>
     5 #include<queue>
     6 #include<map>
     7 #include<cmath>
     8 #include<ctime>
     9 #include<set>
    10 #define pa pair<int,int>
    11 #define lowb(x) ((x)&(-(x)))
    12 #define REP(i,n) for(i=1;i<=n;i++)
    13 #define MAX(a,b) ((a>b)?a:b)
    14 #define MIN(a,b) ((a<b)?a:b)
    15 #define CLR(a,x) memset(a,x,sizeof(a))
    16 #define rei register int
    17 using namespace std;
    18 const int maxn=5050,maxm=100010;
    19 typedef long long ll;
    20 
    21 ll rd(){
    22     ll x=0;char c=getchar();int neg=1;
    23     while(c<'0'||c>'9'){if(c=='-') neg=-1;c=getchar();}
    24     while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
    25     return x*neg;
    26 }
    27 
    28 struct Edge{
    29     int a,b,l,ne;
    30 }eg[maxm*2];
    31 int M,N,egh[maxn],ect;
    32 int dis[maxn][2];
    33 priority_queue<pa,vector<pa>,greater<pa> > q;
    34 
    35 inline void adeg(int a,int b,int l){
    36     eg[ect].a=a;eg[ect].b=b;eg[ect].l=l;
    37     eg[ect].ne=egh[a];egh[a]=ect++;
    38 }
    39 
    40 inline void change(int x,int d){
    41     if(d<dis[x][0]){
    42         dis[x][1]=dis[x][0];dis[x][0]=d;
    43         q.push(make_pair(d,x));
    44     }else if(d<dis[x][1]&&d>dis[x][0]){
    45         dis[x][1]=d;q.push(make_pair(d,x));
    46     }
    47 }
    48 
    49 void dijkstra(){
    50     CLR(dis,127);
    51     dis[1][0]=0;q.push(make_pair(0,1));
    52     while(!q.empty()){
    53         int p=q.top().second;q.pop();
    54         for(int i=egh[p];i!=-1;i=eg[i].ne){
    55             int b=eg[i].b;
    56             change(b,dis[p][0]+eg[i].l);change(b,dis[p][1]+eg[i].l);
    57         }
    58     }
    59 }
    60 
    61 int main(){
    62     //freopen(".in","r",stdin);
    63     rei i,j,k;
    64     N=rd(),M=rd();CLR(egh,-1);
    65     REP(i,M){
    66         int a=rd(),b=rd(),c=rd();
    67         adeg(a,b,c);adeg(b,a,c);
    68     }//printf("lll
    ");
    69     dijkstra();
    70     
    71     printf("%d
    ",dis[N][1]);
    72     return 0;
    73 }
  • 相关阅读:
    Leetcode 283. Move Zeroes
    算法总结
    随机森林
    BRICH
    DBSCAN算法
    k-means算法的优缺点以及改进
    soket编程
    手电筒过河
    字符串反转
    URAL 1356. Something Easier(哥德巴赫猜想)
  • 原文地址:https://www.cnblogs.com/Ressed/p/9599494.html
Copyright © 2020-2023  润新知