HDU1874 畅通工程续【Dijkstra算法】

畅通工程续

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 61859    Accepted Submission(s): 23178


Problem Description
某省自从实行了很多年的畅通工程计划后,终于修建了很多路。不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多。这让行人很困扰。

现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。
 

Input
本题目包含多组数据,请处理到文件结束。
每组数据第一行包含两个正整数N和M(0<N<200,0<M<1000),分别代表现有城镇的数目和已修建的道路的数目。城镇分别以0~N-1编号。
接下来是M行道路信息。每一行有三个整数A,B,X(0<=A,B<N,A!=B,0<X<10000),表示城镇A和城镇B之间有一条长度为X的双向道路。
再接下一行有两个整数S,T(0<=S,T<N),分别代表起点和终点。
 

Output
对于每组数据,请在一行里输出最短需要行走的距离。如果不存在从S到T的路线,就输出-1.
 

Sample Input
 
 
3 30 1 10 2 31 2 10 23 10 1 11 2
 
Sample Output
 
 
2-1
 

Author
linle
 

Source


问题链接:HDU1874 畅通工程续

问题描述参见上文。

问题分析

  这是一个经典的单源最短路径问题,使用Dijkstra算法

程序说明

  图的表示主要有三种形式,一是邻接表,二是邻接矩阵,三是边列表。邻接矩阵对于结点多和边少的情况都不理想。程序中用邻接表存储图,即g[],是一种动态的存储。数组dist[]中存储单源(结点s)到各个结点的最短距离。优先队列q按照边的权值从小到大排队,便于计算最短路径。

  这个问题,由于结点数量比较少,图还可以用邻接矩阵表示。那样的话,代码则是另外一种写法。

需要注意的一点是,有可能不存在路径,程序中93行增加条件“dist[t]==INT_MAX2”进行判断。


AC的C++语言程序如下:

/* HDU1874 畅通工程续 */

#include <iostream>
#include <vector>
#include <queue>
#include <cstdio>

using namespace std;

const int INT_MAX2 = ((unsigned int)(-1) >> 1);
const int MAXN = 200;

// 边
struct _edge {
    int v, cost;
    _edge(int v2, int c){v=v2; cost=c;}
};

// 结点
struct _node {
    int u, cost;
    _node(){}
    _node(int u2, int l){u=u2; cost=l;}

    bool operator<(const _node n) const {
        return cost > n.cost;
    }
};

vector<_edge> g[MAXN+1];
int dist[MAXN+1];
bool visited[MAXN+1];

void dijkstra(int start, int n)
{
    priority_queue<_node> q;

    for(int i=0; i<=n; i++) {
        dist[i] = INT_MAX2;
        visited[i] = false;
    }

    dist[start] = 0;

    q.push(_node(start, 0));

    _node f;
    while(!q.empty()) {
        f = q.top();
        q.pop();

        int u = f.u;
        if(!visited[u]) {
            visited[u] = true;

            int len = g[u].size();
            for(int i=0; i<len; i++) {
                int v2 = g[u][i].v;

                if(visited[v2])
                    continue;

                int tempcost = g[u][i].cost;
                int nextdist = dist[u] + tempcost;

                if(dist[v2] > nextdist) {
                    dist[v2] = nextdist;
                    q.push(_node(v2, dist[v2]));
                }
            }
        }
    }
}

int main()
{
    int n, m, src, dest, cost2, s, t;

    // 输入数据,构建图
    while(scanf("%d%d",&n,&m) != EOF && (n + m)) {
        for(int i=1; i<=m; i++) {
            scanf("%d%d%d", &src, &dest, &cost2);

            g[src].push_back(_edge(dest, cost2));
            g[dest].push_back(_edge(src, cost2));
        }
        scanf("%d%d", &s, &t);

        // Dijkstra算法
        dijkstra(s, n);

        // 输出结果
        printf("%d\n", (dist[t] == INT_MAX2) ? -1 : dist[t]);

        // 释放存储
        for(int i=0; i<=n; i++)
            g[i].clear();
    }

    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值