Til the Cows Come Home POJ - 2387 【最短路Dijkstra】

链接:http://poj.org/problem?id=2387

题意:裸奔最短路

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

const int MAX_N = 2000+5;
const int MAX_COST = 9999999;
int cost[MAX_N][MAX_N];
int mincost[MAX_N];
bool used[MAX_N];
void init() {
    for (int i = 0; i < MAX_N; i++) {
        used[i] = false;
        mincost[i] = MAX_COST;
        for (int j = 0; j < MAX_N; j++) {
            cost[i][j] = MAX_COST;
        }
    }
}
int Dijkstra(int n) {
    mincost[1] = 0;
    for (int i = 1; i <= n; i++) {
        int index = -1;
        int mmin = MAX_COST;
        for (int j = 1; j <= n; j++) {
            if (!used[j] && mincost[j] < mmin) {
                index = j;
                mmin = mincost[j];
            }
        }
        used[index] = true;
        for (int j = 1; j <= n; j++) {
            if (used[j]) continue;
            mincost[j] = min(mincost[j], cost[index][j] + mincost[index]);
        }
    }
    return mincost[n];
}
int main()
{
    int n, t;
    cin >> t >> n;
    init();
    int u, v, costt;
    for (int i = 1; i <= t; ++i) {
        cin >> u >> v >> costt;
        cost[u][v] = cost[v][u] = min(costt, cost[u][v]);
    }
    int res = Dijkstra(n);
    cout << res << endl;

    return 0;
}

 

全部评论

相关推荐

10-15 16:27
门头沟学院 C++
LeoMoon:建议问一下是不是你给他付钱😅😅
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务