HDUOJ 1874(Dijkstra + 堆优化,floyd,Bellman_Ford,spfa)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874
迪杰斯特拉模板题目
AC代码

#include<bits/stdc++.h>
#define ll long long
#define lowbit(x) x&(-x)
#define eps 1e-8
#define lson l,m,td<<1
#define rson m+1,r,td<<1|1
#define INF 0x3f3f3f3f
#define clear(a) memset(a,0,sizeof a)
#define clear_1(a) memset(a,-1,sizeof a)
#define line puts("");
#define LOCALR freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
#define LOCALW freopen("C:\\Users\\Administrator\\Desktop\\out.txt","w",stdout);
using namespace std;
/*******************************************************************/
/*******************************************************************/
template<class T>
inline int read(T &x){
    char ch;
    bool flag=false;
    if((ch=getchar())==EOF)return -1;
    for(;!isdigit(ch);ch=getchar())if(ch=='-')flag=true;
    for(x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());
    x=flag?-x:x;
    return 1;
}
template<class T>
inline void write(T x,bool isnewline=false){
    static const int maxlen=100;
    static char s[maxlen];
    if(x<0){putchar('-');x=-x;}
    if(!x){putchar('0');return;}
    int len=0;
    for(;x;x/=10)s[len++]=x%10+'0';
    if(isnewline)puts(s);
    else for(int i=len-1;i>=0;i--)putchar(s[i]);
}
/*******************************************************************/
/*******************************************************************/
int n,m;
int dis[210],vis[210],MAP[210][210];
struct node{
    int id,DIS;
    friend bool operator < (node a,node b){return a.DIS>b.DIS;}
};
priority_queue<node>qu;
void init(){
    memset(vis,0,sizeof vis);
    memset(MAP,INF,sizeof MAP);
    memset(dis,INF,sizeof dis);
    while(!qu.empty())qu.pop();
}
void Dijkstra(int s){
    dis[s]=0;
    qu.push({s,0});
    while(!qu.empty()){
        int k=qu.top().id;qu.pop();
        if(vis[k])continue;
        vis[k]=1;
        for(int i=0;i<n;i++){
            if(!vis[i]&&MAP[k][i]!=INF&&dis[k]+MAP[k][i]<dis[i]){
                dis[i]=dis[k]+MAP[k][i];
                qu.push({i,dis[i]});
            }
        }
    }
}
int main(){
    while(~read(n)&&~read(m)){
        init();
        int a,b,c,s,e;
        for(int i=0;i<m;i++){
            read(a),read(b),read(c);
            if(c<MAP[a][b])MAP[a][b]=MAP[b][a]=c;
        }
        read(s),read(e);
        Dijkstra(s);
        if(dis[e]==INF)write(-1),line
        else write(dis[e]),line
    }
    return 0;
}

floyd算法

#include<bits/stdc++.h>
#define ll long long
#define lowbit(x) x&(-x)
#define eps 1e-8
#define lson l,m,td<<1
#define rson m+1,r,td<<1|1
#define INF 0x3f3f3f3f
#define clear(a) memset(a,0,sizeof a)
#define clear_1(a) memset(a,-1,sizeof a)
#define line puts("");
#define LOCALR freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
#define LOCALW freopen("C:\\Users\\Administrator\\Desktop\\out.txt","w",stdout);
using namespace std;
/*******************************************************************/
/*******************************************************************/
template<class T>
inline int read(T &x){
    char ch;
    bool flag=false;
    if((ch=getchar())==EOF)return -1;
    for(;!isdigit(ch);ch=getchar())if(ch=='-')flag=true;
    for(x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());
    x=flag?-x:x;
    return 1;
}
template<class T>
inline void write(T x,bool isnewline=false){
    static const int maxlen=100;
    static char s[maxlen];
    if(x<0){putchar('-');x=-x;}
    if(!x){putchar('0');return;}
    int len=0;
    for(;x;x/=10)s[len++]=x%10+'0';
    if(isnewline)puts(s);
    else for(int i=len-1;i>=0;i--)putchar(s[i]);
}
/*******************************************************************/
/*******************************************************************/
int MAP[210][210],n,m;
int main(){
    while(~read(n)&&~read(m)){
        memset(MAP,INF,sizeof MAP);
        for(int i=0;i<210;i++)MAP[i][i]=0;
        int a,b,c,s,e;
        for(int i=0;i<m;i++){
            read(a),read(b),read(c);
            if(c<MAP[a][b])MAP[a][b]=MAP[b][a]=c;
        }
        for(int k=0;k<n;k++){
            for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
                    MAP[i][j]=min(MAP[i][j],MAP[i][k]+MAP[k][j]);
                }
            }
        }
        read(s),read(e);
        if(MAP[s][e]==INF)write(-1),line
        else write(MAP[s][e]),line
    }
    return 0;
}

Bellman_Ford 代码

#include<bits/stdc++.h>
#define ll long long
#define lowbit(x) x&(-x)
#define eps 1e-8
#define lson l,m,td<<1
#define rson m+1,r,td<<1|1
#define INF 0x3f3f3f3f
#define clear(a) memset(a,0,sizeof a)
#define clear_1(a) memset(a,-1,sizeof a)
#define line puts("");
#define LOCALR freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
#define LOCALW freopen("C:\\Users\\Administrator\\Desktop\\out.txt","w",stdout);
using namespace std;
/*******************************************************************/
/*******************************************************************/
template<class T>
inline int read(T &x){
    char ch;
    bool flag=false;
    if((ch=getchar())==EOF)return -1;
    for(;!isdigit(ch);ch=getchar())if(ch=='-')flag=true;
    for(x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());
    x=flag?-x:x;
    return 1;
}
template<class T>
inline void write(T x,bool isnewline=false){
    static const int maxlen=100;
    static char s[maxlen];
    if(x<0){putchar('-');x=-x;}
    if(!x){putchar('0');return;}
    int len=0;
    for(;x;x/=10)s[len++]=x%10+'0';
    if(isnewline)puts(s);
    else for(int i=len-1;i>=0;i--)putchar(s[i]);
}
/*******************************************************************/
/*******************************************************************/
int dis[210],n,m;
struct node{
    int u,v,DIS;
    void get(){read(u),read(v),read(DIS);}
    void scopy(node a){
        u=a.v,v=a.u,DIS=a.DIS;
    }
}edge[2010];
void Bellman_Ford(int s){
    m<<=1;
    dis[s]=0;
    for(int i=1;i<n;i++){
        bool flag=false;
        for(int j=0;j<m;j++){
            if(dis[edge[j].v]>dis[edge[j].u]+edge[j].DIS){
                flag=true;
                dis[edge[j].v]=dis[edge[j].u]+edge[j].DIS;
            }
        }
        if(!flag)break;
    }
}
int main(){
    while(~read(n)&&~read(m)){
        memset(dis,INF,sizeof dis);
        for(int i=0;i<m;i++){
            edge[i].get();
            edge[i+m].scopy(edge[i]);
        }
        int s,e;read(s),read(e);
        Bellman_Ford(s);
        if(dis[e]==INF)write(-1),line
        else write(dis[e]),line
    }
    return 0;
}

SPFA 算法

#include<bits/stdc++.h>
#define ll long long
#define lowbit(x) x&(-x)
#define eps 1e-8
#define lson l,m,td<<1
#define rson m+1,r,td<<1|1
#define INF 0x3f3f3f3f
#define clear(a) memset(a,0,sizeof a)
#define clear_1(a) memset(a,-1,sizeof a)
#define line puts("");
#define LOCALR freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
#define LOCALW freopen("C:\\Users\\Administrator\\Desktop\\out.txt","w",stdout);
using namespace std;
/*******************************************************************/
/*******************************************************************/
template<class T>
inline int read(T &x){
    char ch;
    bool flag=false;
    if((ch=getchar())==EOF)return -1;
    for(;!isdigit(ch);ch=getchar())if(ch=='-')flag=true;
    for(x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());
    x=flag?-x:x;
    return 1;
}
template<class T>
inline void write(T x,bool isnewline=false){
    static const int maxlen=100;
    static char s[maxlen];
    if(x<0){putchar('-');x=-x;}
    if(!x){putchar('0');return;}
    int len=0;
    for(;x;x/=10)s[len++]=x%10+'0';
    if(isnewline)puts(s);
    else for(int i=len-1;i>=0;i--)putchar(s[i]);
}
/*******************************************************************/
/*******************************************************************/
const int N=210;
int MAP[N][N],dis[N],vis[N],n,m;
queue<int>qu;
void spfa(int s){
    qu.push(s),dis[s]=0,vis[s]=1;
    while(!qu.empty()){
        int u=qu.front();qu.pop();
        vis[u]=0;
        for(int i=0;i<n;i++){
            if(dis[i]>dis[u]+MAP[u][i]){
                dis[i]=dis[u]+MAP[u][i];
                if(!vis[i])vis[i]=1,qu.push(i);
            }
        }
    }
}
int main(){
    while(~read(n)&&~read(m)){
        memset(MAP,INF,sizeof MAP);
        memset(vis,0,sizeof vis);
        memset(dis,INF,sizeof dis);
        int a,b,c,s,e;
        for(int i=0;i<m;i++){
            read(a),read(b),read(c);
            if(MAP[a][b]>c)MAP[a][b]=MAP[b][a]=c;
        }
        read(s),read(e);
        spfa(s);
        if(dis[e]==INF)write(-1),line
        else write(dis[e]),line
    }
}

    原文作者:Bellman - ford算法
    原文地址: https://blog.csdn.net/qq_41635132/article/details/81713332
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞