POJ-2387-Til the Cows Come Home [最短路]
2017-08-21 09:02
316 查看
题目传送门
题意:输入一个无向图,求N到1的最短路径。
思路:最短路模板题。
题意:输入一个无向图,求N到1的最短路径。
思路:最短路模板题。
#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <cmath> #include <queue> #include <map> #include <set> using namespace std; int mp[1200][1200], N, T; const int inf = 0x3f3f3f3f; void init() { for (int i = 0; i <= 1000; i++) for (int j = 0; j <= 1000; j++) mp[i][j] = inf; } void dij(int x) { int dis[1200], book[1200]; memset(book,0,sizeof(book)); for (int i = 1; i <= N; i++) dis[i] = mp[x][i]; book[x] = 1; for (int i = 1; i <= N; i++) { int mi = inf, f = -1; for (int j = 1; j <= N; j++) { if (!book[j] && dis[j]<mi) { mi = dis[j]; f = j; } } if (f==-1) break; book[f]=1; for (int j = 1; j <= N; j++) { if (!book[j] && dis[j]>dis[f]+mp[f][j]) { dis[j] = dis[f]+mp[f][j]; } } } printf("%d\n", dis ); return; } int main(void) { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); while (~scanf("%d %d", &T, &N)) { init(); while (T--) { int x, y, s; scanf("%d %d %d", &x, &y, &s); if (mp[x][y]>s && mp[y][x]>s) mp[x][y] = mp[y][x] = s; } dij(1); } return 0; }
相关文章推荐
- POJ 2387 Til the Cows Come Home(迪杰斯特拉/优先队列/最短路)
- 第一次最短路:POJ 2387 Til the Cows Come Home(bfs方法~)
- POJ 2387 Til the Cows Come Home(最短路 迪杰斯特拉)
- POJ - 2387 Til the Cows Come Home (最短路)
- poj 2387 Til the Cows Come Home (最短路,dijkstra模版题)
- POJ 2387 Til the Cows Come Home 最短路 Dijstra
- POJ - 2387 Til the Cows Come Home (最短路)
- POJ 2387 Til the Cows Come Home (最短路+Dijkstra)
- poj 2387 Til the Cows Come Home -- 最短路dijstra
- POJ 2387 Til the Cows Come Home (最短路)
- [kuangbin带你飞]专题4 最短路 A - Til the Cows Come Home POJ - 2387
- POJ - 2387 Til the Cows Come Home(最短路)
- POJ 2387 Til the Cows Come Home(最短路)
- POJ-2387 Til the Cows Come Home(最短路 Dijkstra算法)
- poj 2387 Til the Cows Come Home(最短路水题 = =)
- POJ 2387 Til the Cows Come Home(最短路)
- POJ 2387 - Til the Cows Come Home ( dijkstra求最短路 )
- POJ 2387-Til the Cows Come Home(最短路Dijkstra+优先队列)
- poj 2387 Til the Cows Come Home (最短路)
- POJ-2387 Til the Cows Come Home ( 最短路 )