HDU2544 最短路

HDU2544 最短路

题意:
求商店到操场的最短路径,无向图。

题解:
建图+dijkstra/ bellman_ford/ floyd/ spfa
因为这里的数据量很小,并且没有负环,所以四种算法都可以用。
dijkstar —— O(mlogn) 适合于没有负环的情况
bellman_Ford ——O(nm) 可以处理负环
Floyd ——O(n^3) 适合多源最短路
Spfa ——O(nm) (实际运行比这个小) 可以处理负环

AC代码:
dijkstra:

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
typedef pair <int,int> P;
const int maxn = 100+10;
const int INF = 0x3f3f3f3f;
int n,m;
int d[maxn];

struct edge
{
    int to,cost;
    edge(int u,int d) : to(u),cost(d) {}
};

vector <edge> G[maxn];

void input()
{
    for(int i = 0; i <= n; i++ )
        G[i].clear();
    for(int i = 0; i < m; i++)
    {
      int u,v,dist;
      scanf("%d%d%d",&u,&v,&dist);
      G[u].push_back(edge(v,dist));
      G[v].push_back(edge(u,dist));
    }
}

void dijkstra()
{
    memset(d,INF,sizeof(d));
    d[1] = 0;
    priority_queue < P,vector<P>,greater<P> >  Q;//P.first 是距离,.P.second 是顶点
    Q.push(P(0,1));
    while(!Q.empty())
    {
        P x = Q.top();
        Q.pop();
        int dist = x.first;
        int u = x.second;

        if(d[u] < dist) continue;//排除u相同时, dist不同的情况。
        for(int i = 0; i < G[u].size(); i++)
        {
            edge e = G[u][i];
            if(d[e.to] > d[u] + e.cost)
            {
                d[e.to] = d[u]+ e.cost;
                Q.push(P(d[e.to],e.to));
            }
        }
    }
    return ;
}

void output()
{
    printf("%d\n",d[n]);
}
int main()
{
    while(scanf("%d%d",&n,&m) && n && m)
    {
        input();
        dijkstra();
        output();
     }
    return 0;
}

Bellman_Ford:

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
typedef pair <int,int> P;
const int maxn = 100+10;
const int maxm = 1e4 + 100;
const int INF = 0x3f3f3f3f;
int n,m;
int d[maxn];

struct edge {
  int from,to,cost;
};

edge es[maxm*2];

void Bellman_Ford()
{
    memset(d,INF,sizeof(d));
    d[1] = 0;
    for(int i = 1; i < n; i++)
    {
        for(int j = 1; j <= 2*m; j++)
        {
            edge e = es[j];
            if(d[e.from] < INF && d[e.to] > d[e.from] + e.cost)  d[e.to] = d[e.from] + e.cost;
        }
    }
}

int main()
{
    while(scanf("%d%d",&n,&m)&& n && m)
    {
        int j = 1;
        for(int i = 1; i <= m; i++)
        {
            int u,v,dist;
            scanf("%d%d%d",&u,&v,&dist);
            edge e1,e2;
            e1.from = u,e1.to = v,e1.cost = dist;
            e2.from = v,e2.to = u,e2.cost = dist;
            es[j++] = e1;
            es[j++] = e2;
        }
        Bellman_Ford();
        printf("%d\n",d[n]);
    }
}

Floyd:

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
typedef pair <int,int> P;
const int maxn = 100+10;
const int maxm = 1e4 + 100;
const int INF = 0x3f3f3f3f;
int n,m;
int mp[maxn][maxn];

void Floyd()
{
    for(int k = 1; k <= n; k++)
        for(int i = 1; i <= n; i++)
          for(int j = 1; j <= n; j++)
               mp[i][j] = min(mp[i][j],mp[i][k]+mp[k][j]);
}

int main()
{
    while(scanf("%d%d",&n,&m)&& n && m)
    {
        memset(mp,INF,sizeof(mp));
        int j = 1;
        for(int i = 1; i <= m; i++)
        {
            int u,v,dist;
            scanf("%d%d%d",&u,&v,&dist);
            mp[u][v] = dist;
            mp[v][u] = dist;
        }
        Floyd();
        printf("%d\n",mp[1][n]);
    }
    return 0;
}

Spfa:

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
typedef pair <int,int> P;
const int maxn = 100+10;
const int maxm = 1e4 + 100;
const int INF = 0x3f3f3f3f;
int n,m;
int d[maxn];
int cnt[maxn];
bool inq[maxn];
struct edge
{
    int to,cost;
};

vector <edge> G[maxn];

bool Spfa()
{
    memset(d,INF,sizeof(d));
    memset(cnt,0,sizeof(cnt));
    memset(inq,false,sizeof(inq));
    queue <int> Q;
    Q.push(1);
    d[1] = 0;
    cnt[1] = 1;
    inq[1] = true;
    while(!Q.empty())
    {
        int t = Q.front();
        Q.pop();
        inq[t] = false;
        for(int i = 0; i < G[t].size(); i++)
        {
            edge e = G[t][i];
            if(d[e.to] > d[t] + e.cost)
            {
                d[e.to] = d[t] + e.cost;
                if(!inq[e.to]) {
                    if(++cnt[e.to] > n)  return false;
                    Q.push(e.to);
                    inq[e.to] = true;
                }
            }
        }
    }
    return true;
}

int main()
{
    while(scanf("%d%d",&n,&m)&& n && m)
    {

        for(int i = 1; i <= n; i++)  G[i].clear();
        for(int i = 1; i <= m; i++)
        {
            int u,v,dist;
            scanf("%d%d%d",&u,&v,&dist);
            edge e1,e2;
            e1.to = v, e1.cost = dist;
            e2.to = u, e2.cost = dist;
            G[u].push_back(e1);
            G[v].push_back(e2);
        }
        if(Spfa())
        printf("%d\n",d[n]);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值