您的位置:首页 > 其它

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