hdu1874 畅通工程续 spfa/迪杰斯特拉

13 篇文章 0 订阅
11 篇文章 0 订阅

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1874

题意:

题解:

卿学姐视频: http://www.bilibili.com/video/av4224493/

代码:

spfa:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MS(a) memset(a,0,sizeof(a))
#define MP make_pair
#define PB push_back
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
inline ll read(){
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
//////////////////////////////////////////////////////////////////////////
const int maxn = 205+10;

std::vector<pair<int,int> > E[maxn];

int n,m;
int d[maxn],inq[maxn];

void init(){
    for(int i=0; i<maxn; i++) E[i].clear();
    for(int i=0; i<maxn; i++) inq[i] = 0;
    for(int i=0; i<maxn; i++) d[i] = 1e9;
}

int main(){
    while(cin >> n >> m){
        init();
        for(int i=0; i<m; i++){
            int u,v,w; scanf("%d%d%d",&u,&v,&w);
            E[u].push_back(MP(v,w));
            E[v].push_back(MP(u,w));
        }

        int s,t;
        scanf("%d%d",&s,&t);

        queue<int> Q;
        Q.push(s), d[s]=0, inq[s]=0;
        while(!Q.empty()){
            int now = Q.front();
            Q.pop(); inq[now] = 0;
            for(int i=0; i<(int)E[now].size(); i++){
                int v = E[now][i].first, w = E[now][i].second;
                if(d[now]+w < d[v]){
                    d[v] = d[now]+w;
                    if(inq[v]==1) continue;
                    inq[v] = 1;
                    Q.push(v);
                }
            }
        }

        if(d[t] == 1e9) printf("-1\n");
        else printf("%d\n",d[t]);
    }

    return 0;
}

dij:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MS(a) memset(a,0,sizeof(a))
#define MP make_pair
#define PB push_back
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
inline ll read(){
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
//////////////////////////////////////////////////////////////////////////
const int maxn = 205+10;

std::vector<pair<int,int> > E[maxn];

int n,m;
int d[maxn];

void init(){
    for(int i=0; i<maxn; i++) E[i].clear();
    for(int i=0; i<maxn; i++) d[i] = 1e9;
}

int main(){
    while(cin >> n >> m){
        init();
        for(int i=0; i<m; i++){
            int u,v,w; scanf("%d%d%d",&u,&v,&w);
            E[u].push_back(MP(v,w));
            E[v].push_back(MP(u,w));
        }

        int s,t;
        scanf("%d%d",&s,&t);

        priority_queue<pair<int,int> > Q;
        d[s]=0;
        Q.push(MP(-d[s],s));
        while(!Q.empty()){
            int now = Q.top().second;
            Q.pop();
            for(int i=0; i<(int)E[now].size(); i++){
                int v = E[now][i].first, w = E[now][i].second;
                if(d[now]+w < d[v]){
                    d[v] = d[now]+w;
                    Q.push(MP(-d[v],v));
                }
            }
        }

        if(d[t] == 1e9) printf("-1\n");
        else printf("%d\n",d[t]);
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值