POJ_3255_Roadblocks
2016-09-23 23:01
260 查看
//见《挑战程序设计竞赛》第2.5.6节 #include<cstdio> #include<iostream> #include<queue> #include<functional> using namespace std; #define INF 10000000 #define MAX_N 5000 #define MAX_R 100000 typedef pair<int, int> P; struct edge { int to, cost; }; int N, R; vector<edge> G[MAX_N]; //图的邻接表表示 int a, b, l; int dist[MAX_N]; //最短距离 int dist2[MAX_N]; //次短距离 int main() { scanf("%d%d", &N, &R); for (int i = 0; i < R; i++) { scanf("%d%d%d", &a, &b, &l); a--; b--; edge e; e.cost = l; e.to = b; G[a].push_back(e); e.to = a; G[b].push_back(e); } priority_queue<P, vector<P>, greater<P> > que; fill(dist, dist + N, INF); fill(dist2, dist2 + N, INF); dist[0] = 0; que.push(P(0, 0)); while (!que.empty()) { P p = que.top(); que.pop(); int v = p.second, d = p.first; if (dist2[v] < d) continue; for (int i = 0; i < G[v].size(); i++) { edge &e = G[v][i]; int d2 = d + e.cost; if (dist[e.to] > d2) { swap(dist[e.to], d2); que.push(P(dist[e.to], e.to)); } if (dist2[e.to] > d2 && dist[e.to] < d2) { dist2[e.to] = d2; que.push(P(dist2[e.to], e.to)); } } } printf("%d\n", dist2[N - 1]); return 0; }
相关文章推荐
- poj 3255 Roadblocks
- POJ - 3255 Roadblocks
- 【POJ】3255 Roadblocks(次短路+spfa)
- POJ 3255 Roadblocks
- poj 3255 Roadblocks
- POJ-3255 Roadblocks(次短路)
- POJ-3255-Roadblocks
- POJ 3255 Roadblocks(次短路模板题)
- POJ-3255 Roadblocks
- POJ 3255 Roadblocks【次短路】
- poj 3255 Roadblocks (SPFA)
- POJ-3255-Roadblocks
- 次最短路径 POJ 3255 Roadblocks
- poj 3255 Roadblocks
- POJ 3255 Roadblocks
- poj 3255 Roadblocks(次短路)
- poj3255 - Roadblocks
- POJ 3255 Roadblocks
- POJ 3255 Roadblocks
- poj 3255 Roadblocks