题意: 次短路,
裸的板子;
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<map> #include<set> #include<vector> #include<queue> #include<stack> using namespace std; const int maxn = 1e5 +131; const int INF = 0x7fffffff; struct Edge{ int to, cost; Edge(int a, int b) : to(a), cost(b) {} }; vector<Edge> G[maxn]; void AddEdge(int from, int to, int cost) { G[from].push_back(Edge(to, cost)); G[to].push_back(Edge(from, cost)); } typedef pair<int, int> P; int N, R; int Dist[maxn], Dist2[maxn]; int Solve() { 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 tmp = que.top(); que.pop(); int v = tmp.second, d = tmp.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)); } } } return Dist2[N-1]; } int main() { while(scanf("%d%d", &N, &R)!=EOF) { if(N == 0 && R == 0) break; for(int i = 0; i <= N; ++i) G[i].clear(); for(int i = 0; i < R; ++i) { int u, v, c; scanf("%d%d%d", &u, &v, &c); AddEdge(u-1,v-1,c); } printf("%d\n",Solve()); } return 0; }
时间: 2024-10-05 08:31:28