模板最短路Dijkstra+Heap,SPFA

來源:互聯網
上載者:User

不解釋,只是模板。

Dijkstra+Heap:

#include <queue>#include <cmath>#include <cstdio>#include <vector>#include <cstring>#include <iostream>#include <algorithm>#define DB double#define SG string#define LL long long#define Fp(A,B,C,D) for(A=B;A<=C;A+=D)#define Fm(A,B,C,D) for(A=B;A>=C;A-=D)#define Clear(A) memset(A,0,sizeof(A))using namespace std;const LL Max=1e4+5;const LL Mod=1e9+7;const LL Inf=1e18;LL N,M,Start,End,Dis[Max],Vis[Max];LL Cnt,To[Max<<1],Edge[Max<<1],Next[Max<<1],Head[Max];struct Node{LL Num,Dis;bool operator < (const Node &A) const{return A.Dis<Dis;}};inline int Read(){bool F=0;int X=0;char CH=getchar();while(CH>'9'||CH<'0'){if(CH=='-')F=1;CH=getchar();}while(CH>='0'&&CH<='9'){X=(X<<3)+(X<<1)+CH-'0';CH=getchar();}return F?-X:X;}inline void Write(int X){if(X<0)X=-X,putchar('-');if(X>9)Write(X/10);putchar(X%10+48);}void Insert(LL X,LL Y,LL Z){To[++Cnt]=Y;Edge[Cnt]=Z;Next[Cnt]=Head[X];Head[X]=Cnt;}void Dijkstra_Heap(LL S){LL I,J,K;priority_queue<Node>Q;Fp(I,1,N,1){Dis[I]=Inf;}Dis[S]=0;Node Tmp;Tmp.Num=S;Q.push(Tmp);while(Q.size()){LL X=Q.top().Num;Q.pop();if(Vis[X]==false){Vis[X]=true;for(I=Head[X];I;I=Next[I]){LL Y=To[I],Z=Edge[I];if(!Vis[Y]&&Dis[Y]>Dis[X]+Z){Dis[Y]=Dis[X]+Z;Tmp.Num=Y;Tmp.Dis=Dis[Y];Q.push(Tmp);}}}}}int main(){LL I,J,K;N=Read(),M=Read();Fp(I,1,M,1){LL X=Read(),Y=Read(),Z=Read();Insert(X,Y,Z);}Start=Read();End=Read();Dijkstra_Heap(Start);Write(Dis[End]);return 0;}

SPFA:

#include <stdio.h>#include <queue>#define DB double#define SG string#define LL long long#define Fp(A,B,C,D) for(A=B;A<=C;A+=D)#define Fm(A,B,C,D) for(A=B;A>=C;A-=D)#define Clear(A) memset(A,0,sizeof(A))using namespace std;const LL Max=4e5+5;const LL Mod=1e9+7;const LL Inf=1e18;queue<LL>Q;LL N,M,Start,End,Dis[Max],Vis[Max];LL Cnt,To[Max<<1],Edge[Max<<1],Next[Max<<1],Head[Max];inline LL Read(){LL X=0;char CH=getchar();bool F=0;while(CH>'9'||CH<'0'){if(CH=='-')F=1;CH=getchar();}while(CH>='0'&&CH<='9'){X=(X<<1)+(X<<3)+CH-'0';CH=getchar();}return F?-X:X;}inline void Write(LL X){if(X<0)X=-X,putchar('-');if(X>9)Write(X/10);putchar(X%10+48);}void Insert(LL X,LL Y,LL Z){To[++Cnt]=Y;Edge[Cnt]=Z;Next[Cnt]=Head[X];Head[X]=Cnt;}LL SPFA(LL S){LL I,X;Fp(I,1,N,1){Dis[I]=Inf;}Q.push(S),Vis[S]=1,Dis[S]=0;while(Q.size()){X=Q.front();Q.pop();Vis[X]=0;for(I=Head[X];I;I=Next[I]){LL Y=To[I],Z=Edge[I];if(Dis[X]+Z<Dis[Y]){Dis[Y]=Dis[X]+Z;if(Vis[Y]==0){Q.push(Y);Vis[Y]=1; }}}}return Dis[End];}int main(){LL I,J,K;N=Read();M=Read();Fp(I,1,M,1){LL X=Read(),Y=Read(),Z=Read();Insert(X,Y,Z);}Start=Read();End=Read();Write(SPFA(Start));return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.