Topic Link: Click to open the link
Problem Solving Ideas:
According to the Dijkstra thought to do the second short circuit, the first time with the adjacency table, note that the two-way side of the problem and the subscript of the node to 1.
Full code:
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include < cmath> #include <set> #include <vector> #include <climits> #include <queue>using namespace Std;typedef Long Long ll;const int maxn = 100001;typedef pair<int, int> p;struct edge{int to, cost;}; int INF = 99999999;int N, r;vector<edge> g[maxn];int dist[maxn];int dist2[maxn];void 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 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)); }}} cout << dist2[n-1] << Endl;} int main () {#ifdef Doubleq freopen ("In.txt", "R", stdin); #endif while (CIN >> n >> r) {int A, B, D; for (int i = 0; i < R; i + +) {cin >> a >> b >> D; A--; B--; struct Edge temp; Temp.to = b; Temp.cost = D; G[a].push_back (temp); struct Edge temp2; Temp2.to = A; Temp2.cost = D; G[b].push_back (TEMP2); } solve (); } return 0;}
More highlights please visit: Click to open the link
POJ3255 (secondary short circuit)