POJ 2387 Til the Cows Come Home
2014-12-15 10:01
375 查看
题目大意:
给你N个点 T条边, 求N->1的最短路
给你N个点 T条边, 求N->1的最短路
#include <iostream> #include <cstdlib> #include <cstdio> #include <algorithm> #include <vector> #include <queue> using namespace std; #define INF 0xfffffff #define maxn 1002 struct Edge { int e, w; }; vector<Edge> G[1002]; bool vis[maxn]; int dist[maxn], n, m;; void Init() { for(int i=0; i<=n; i++) { G[i].clear(); dist[i] = INF; vis[i] = false; } } int Spfa() { Edge P, Pn; queue<Edge> Q; P.e = n, P.w = 0; dist = 0; Q.push(P); while( !Q.empty() ) { P = Q.front(); Q.pop(); vis[P.e] = false; int len = G[P.e].size(); for(int i=0; i<len; i++) { Pn = G[P.e][i]; if(dist[Pn.e] > dist[P.e] + Pn.w) { dist[Pn.e] = dist[P.e] + Pn.w; if( !vis[Pn.e] ) { Q.push(Pn); vis[Pn.e] = true; } } } } return dist[1]; } int main() { Edge P; while(cin >> m >> n) { Init(); for(int i=0; i<m; i++) { int a, b ,c; scanf("%d%d%d",&a,&b,&c); P.e = b, P.w = c; G[a].push_back(P); P.e = a; G[b].push_back(P); } int ans = Spfa(); cout << ans << endl; } return 0; }
相关文章推荐
- POJ 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- poj_2387_Til the Cows Come Home
- 迪杰斯特拉 示例 : poj 2387 Til the Cows Come Home
- POJ -- 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- 迪杰斯特拉 示例 : poj 2387 Til the Cows Come Home
- poj-2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- Poj 2387 Til the Cows Come Home