• 解题报告:poj2387 dijkstra


    2017-09-17 17:37:03

    writer:pprp

    dijkstra模板题目,注意去重

    代码如下:

    /*
    @theme:poj 2387
    @declare:最短路,从N到1点
    @writer:pprp
    @date:2017/9/17
    */
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <stack>
    
    using namespace std;
    int N, T, Min;
    const int maxn = 1010;
    const int INF = 0x3f3f3f3f;
    int mp[maxn][maxn];
    bool vis[maxn];
    int dis[maxn];
    
    void Dijkstra(int st)
    {
        for(int i = 1 ; i <= N; i++)
        {
            dis[i] = mp[st][i];
        }
    
    
        vis[st] = 1;
        dis[st] = 0;
    
        int rec = -1;
        for(int i = 1 ; i <= N ; i++)//起到了循环的作用
        {
            Min = INF;
            rec = -1;
            for(int j = 1; j <= N; j++)
            {
                if(!vis[j] && Min > dis[j])
                {
                    rec = j;
                    Min = dis[j];
                }
            }
            if(rec == -1)return ;
    
            vis[rec] = 1;
    
            for(int j = 1; j <= N; j++)
            {
                if(!vis[j] && mp[rec][j] != INF && dis[rec] + mp[rec][j] < dis[j])
                    dis[j] = mp[rec][j] + dis[rec];
            }
        }
    }
    
    int main()
    {
        freopen("in.txt","r",stdin);
        int x, y, v;
        cin >> T >> N;
    
        for(int i = 0 ; i < maxn; i++)
            for(int j = 0 ; j < maxn; j++)
                mp[i][j] = INF;
        memset(vis,0,sizeof(vis));
    
        for(int i = 0 ; i < T ; i++)
        {
            cin >> x >> y >> v;
            if(v < mp[x][y])//去重
            mp[x][y] = mp[y][x] = v;
        }
    
        Dijkstra(1);
        cout << dis[N] << endl;
    
        return 0;
    }
  • 相关阅读:
    execvp
    Linux系统调用getrlimit()与setrlimit()函数详解
    Va_start及Vsprintf应用
    waitpid()
    sdut 2408 Pick apples 夜
    poj 1273 Drainage Ditches 夜
    poj 1408 Fishnet 夜
    poj 1113 Wall 夜
    poj 1584 A Round Peg in a Ground Hole 夜
    poj 3007 Organize Your Train part II 夜
  • 原文地址:https://www.cnblogs.com/pprp/p/7536412.html
Copyright © 2020-2023  润新知