The most common short-circuit ... Dijkstra water over.
------------------------------------------------------------------------------
#include <cstdio>#include <algorithm>#include <cstring>#include <iostream>#include <queue>#define REP (i, n) for (int i = 0; i < n; i++)#define CLR (x, C) memset (x, C, sizeof (x))using namespace std;const int MAXN = 2500 + 5;const int MAXM = 6200 + 5;const int inf = 0X3F3F3F3F;struct Edge {int to, Dist;edge* Next;};edge* pt, edge[MAXM << 1];edge* head[MAXN];void Edge_init () {pt = EDGE;CLR (head, 0);}void Add (int u, int v, int d) {pt-to = V;PT-dist = D;pt-Next = head[u];head[U] = pt++;}#define Add_edge (U, V, d) Add (U, V, D), add (V, U, D)struct Node {int x, D;BOOL operator < (const node &RHS) Const {return d > rhs.d;}};int d[MAXN];priority_queue< node > Q;void Dijkstra (int S) {CLR (d, INF);d[S] = 0;Q.push (node) {S, 0});While (! Q.empty ()) {node o = q.top ();Q.pop ();int x = o.x, dist = O.D;if (dist! = d[x]) continue;For (edge* e = head[x]; e; e = e = next) {int to = e -to;if (d[to] > dist + E, dist) {d[to] = dist + e-dist;Q.push (node) {To, d[to]});}}}}inline int Read () {char C = getchar ();int res = 0;while (! isdigit (c)) C = GetChar ();While (IsDigit (c)) {res = res * + C-' 0 ';C = GetChar ();}return res;}int main () {freopen ("test.in", "R", stdin);int n = read (), M = Read (), s = Read ()-1, t = Read ()-1;edge_init ();While (m--) {int u = Read ()-1, v = Read ()-1, D = Read (); Add_edge (U, V, d); }Dijkstra (s);cout << d[t] << "\ n";return 0;}
------------------------------------------------------------------------------
3408: [Usaco2009 oct]heat wave heat time limit: 3 Sec Memory Limit: MB
Submit: Solved: 59
[Submit] [Status] [Discuss] Description
Inputline 1th: 4 integer t,c,ts,te separated by a space.2nd to C+1: line i+l describes article I road. There are 3 integer rs,re,ci separated by a space.OutputA single integer represents the minimum cost for TS to TE. The data guarantees that there is at least one road. Sample Input7 11 5 4
2 4 2
1 4 3
7 2 2
3 4 3
5 7 5
7 3 3
6 1 1
6 3 4
2 4 3
5 6 3
7 2 1Sample Output7HINT
Source
Gold
Bzoj 3408: [Usaco2009 oct]heat wave heat wave (shortest way)