码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 1874 畅通工程续

时间:2015-05-11 19:34:34      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

Dijkstra单源最短路算法

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;

const int maxn = 205;
int cost[maxn][maxn];
int n, m, u, v, c, su, eu;
int mincost[maxn], s[maxn];

int main()
{
    int i, j, ii;
    while (~scanf("%d%d", &n, &m))
    {
        for (i = 0; i <= n; i++)for (j = 0; j <= n; j++)cost[i][j] = 999999999;
        for (i = 0; i < m; i++)
        {
            scanf("%d%d%d", &u, &v, &c);
            if (c < cost[u][v]) cost[u][v] = c, cost[v][u] = c;
        }
        scanf("%d%d", &su, &eu);
        for (i = 0; i < n; i++) mincost[i] = cost[su][i], s[i] = 0;
        s[su] = 1; int x;
        mincost[su] = 0;
        for (ii = 0; ii < n - 1; ii++)
        {
            int minn = 999999999, flag = 0;
            for (i = 0; i < n; i++)
            if (!s[i] && mincost[i] < minn)
                minn = mincost[i], v = i, flag = 1;
            if (!flag) continue;
            s[v] = 1;
            for (i = 0; i < n; i++)
            if (!s[i] && mincost[v] + cost[v][i] < mincost[i])
                mincost[i] = mincost[v] + cost[v][i];
        }
        if (mincost[eu] != 999999999) printf("%d\n", mincost[eu]);
        else printf("-1\n");
    }
    return 0;
}

 

hdu 1874 畅通工程续

标签:

原文地址:http://www.cnblogs.com/zufezzt/p/4495191.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!