您的位置:首页 > 其它

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

2016-06-24 00:37 330 查看
华电北风吹

天津大学认知计算与应用重点实验室

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 1001

int 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: