[HihoCoder]#1081 : 最短路径·一
2016-06-24 00:37
330 查看
华电北风吹
天津大学认知计算与应用重点实验室
2016-06-24
题目链接:
http://hihocoder.com/problemset/problem/1081
题目分析:
天津大学认知计算与应用重点实验室
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; }
相关文章推荐
- 基于SharpMap和DotNetBar的地图导航(地图缩放滑块)控件开发实践
- [HihoCoder]#1121 : 二分图一•二分图判定
- 屏蔽所有异常的方法
- 问题 B: Octal to Binary Converter
- 热门题---链表面试题总结
- 1016 - Brush (II)
- Android Studio导入Project的方法
- 算法改变世界――《算法之美――隐匿在数据结构背后的原理(C++版)》
- 【瞎扯】从维护角度看日志的重要性——请做一个靠谱的开发
- ssm框架搭建+easyui增删改查实现
- Eclipse调试技巧:远程调试,条件断点和异常断点
- 起重机横跨梁结构ANSYS有限元分析
- UnicodeEncodeError: 'ascii' codec can't encode characters
- [HihoCoder]#1066 : 无间道之并查集
- IllegalStateException
- HDU2010 水仙花数
- 自已的第一个APP!!!
- [HihoCoder]#1051 : 补提交卡
- Codeforces Bubble Cup 8 - Finals [Online Mirror] B. Bribes lca
- MapReduce 应用:TF-IDF 分布式实现