[HihoCoder]#1081 : 最短路径·一

来源:互联网 发布:自动数控编程软件 编辑:程序博客网 时间:2024/05/20 18:15

华电北风吹
天津大学认知计算与应用重点实验室
2016-06-24

题目链接:
http://hihocoder.com/problemset/problem/1081

题目分析:

// problem1081.cpp : 定义控制台应用程序的入口点。// #1081 : 最短路径·一// 张正义 2016-05-18#include "stdafx.h"#include <iostream>#include <vector>#include <string.h>using namespace std;#define MaxNodeNum 1001int map[MaxNodeNum][MaxNodeNum];bool visited[MaxNodeNum];int main(){    memset(map, 0, sizeof(map));    memset(visited, 0, sizeof(visited));    int n, m, start, destination;    cin >> n >> m >> start >> destination;    start--; destination--;    while (m--)    {        int p1, p2, value;        cin >> p1 >> p2 >> value;        p1--; p2--;        if ((map[p1][p2] == 0) || (map[p1][p2] > value))        {            map[p1][p2] = value;            map[p2][p1] = value;        }    }    visited[start] = true;    vector<int> dist(n);    for (int i = 0; i < n; i++)    {        dist[i] = map[start][i];    }    while (true)    {        int minDistValue = 2147483647, index;        for (int i = 0; i < n; i++)        {            if ((visited[i] == false) && (dist[i]>0) && (dist[i] < minDistValue))            {                minDistValue = dist[i];                index = i;            }        }        if (index == destination)        {            cout << dist[index] << endl;            break;        }        visited[index] = true;        for (int i = 0; i < n; i++)        {            if ((visited[i] == false) && (map[index][i] > 0) && ((dist[i] == 0) || (dist[index] + map[index][i]) < dist[i]))            {                dist[i] = dist[index] + map[index][i];            }        }    }    return 0;}
0 0
原创粉丝点击