[算法] poj 2387 单源最短路 Dijkstra
2013-04-10 16:58
302 查看
单源最短路
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> using namespace std; const int N = 1005; #define INF 0x7fffffff int data ; int lowc ; bool vis ; int n, m; void djst(int p) { for(int i = 1; i <= n; i++) { lowc[i] = data[p][i]; vis[i] = false; } vis[p] = true; int i, j; for(i = 1; i <= n-1; i++) { int minv = INF, c = 0; for(j = 1; j <= n; j++) { if(!vis[j] && lowc[j] < minv) { minv = lowc[j]; c = j; } } if(c == 1) { printf("%d\n", minv); return; } vis[c] = true; for(j = 1; j <= n; j++) { if(!vis[j] && data[c][j] != INF && minv + data[c][j] < lowc[j]) { lowc[j] = minv + data[c][j]; } } } } int main() { cin >> m >> n; for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { data[i][j] = INF; } } int u, v, w; for(int i = 0; i < m; i++) { cin >> u >> v >> w; if(w < data[u][v]) { data[u][v] = w; data[v][u] = w; } } djst(n); return 0; }
相关文章推荐
- [算法] poj 2387 单源最短路 Dijkstra
- POJ 1125 单源最短路 dijkstra的算法初步探索
- POJ 2387-Til the Cows Come Home(最短路Dijkstra+优先队列)
- POJ 2387 Til the Cows Come Home (最短路 dijkstra)
- POJ 2387 Til the Cows Come Home 【最短路(dijkstra)】
- poj 2387 Til the Cows Come Home(最短路SPFA+Dijkstra)
- POJ 2387 - Til the Cows Come Home ( dijkstra求最短路 )
- Til the Cows Come Home POJ - 2387 单源最短路 SPFA实现
- POJ 2387 Til the Cows Come Home (最短路+Dijkstra)
- POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
- 【单源最短路模板】 poj 2387
- POJ - 2387 Til the Cows Come Home(最短路Dijkstra模板题)
- POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)
- poj 2387 Til the Cows Come Home (最短路,dijkstra模版题)
- POJ 2387 Til the Cows Come Home (单源最短路SPFA解法)
- 昂贵的聘礼 POJ - 1062 单源最短路 Dijkstra
- POJ - 3268 Silver Cow Party解题报告(dijkstra分别求单源起点和单源终点的最短路)
- POJ 2387 Til the Cows Come Home(最短路dijkstra)
- POJ 3268 Silver Cow Party Dijkstra 单源最短路
- HDU 1874 畅通工程续(单源最短路之Dijkstra n^2算法)