algorithm Training Shortest Path
time limit: 1.0s memory limit: 256.0MB
Problem Description
Given an n vertex, the forward graph of the M-Edge (some of which may be negative, but no negative ring is guaranteed). Please calculate the shortest path from point 1th to other points (vertices are numbered from 1 to n).
Input format
First line two integers n, M.
The next M-line, each line has three integers u, V, L, indicating that u to V has an edge of length L.
Output format
A total of n-1 lines, line I represents the shortest path from point No. 1th to I+1.
Sample input
3 3
1 2-1
2 3-1
3 1 2
Sample output
-1
-2
Data size and conventions
For 10% of data, n = 2,m = 2.
For 30% of data, n <= 5,m <= 10.
For 100% of data, 1 <= n <= 20000,1 <= m <= 200000,-10000 <= L <= 10000, guaranteeing that all vertices can be reached from any vertex.
Title Link: http://lx.lanqiao.org/problem.page?gpid=T15
Title Analysis: Remember SPFA's vector version of the board
#include <cstdio> #include <cstring> #include <queue> #include <vector>using namespace Std;int Const MAX = 200005;int Const INF = 1 << 30;int n, m;struct edge{int u, v; int Val;} E[max << 2];struct node{int V, W; NODE (int vv, int ww) {v = vv; W = ww; }};vector <NODE> vt[max];int dis[max];bool vis[max];void spfa (int v0) {memset (Vis, false, sizeof (VIS)); for (int i = 1; I <= n; i++) dis[i] = INF; Dis[v0] = 0; Queue <int> q; Q.push (V0); while (!q.empty ()) {int u = q.front (); Q.pop (); Vis[u] = false; int sz = Vt[u].size (); for (int i = 0; i < sz; i++) {int v = VT[U][I].V; int w = VT[U][I].W; if (Dis[v] > Dis[u] + W) {Dis[v] = Dis[u] + W; if (!vis[v]) {Q.push (v); VIS[V] = true; } } }}}int Main () {scanf ("%d%d", &n, &m); for (int i = 0; i < m; i++) scanf ("%d%d%d", &e[i].u, &E[I].V, &e[i].val); for (int i = 0; i < m; i++) Vt[e[i].u].push_back (NODE (E[I].V, e[i].val)); SPFA (1); for (int i = 2; I <= n; i++) printf ("%d\n", Dis[i]);}
Blue Bridge Cup Training Shortest path (SPFA template vector)