Title Link:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_A
Single Source Shortest Path
Input
An edge-weighted graph G ( V , E ) and the source r .
|V| |Ers0t0d0s1t1d1:s|E|?1t|E|?1d|E|?1
|V|Is the number of vertices and are the number of |E| edges in G . The graph vertices is named with the numbers 0, 1,..., |V|?1 respectively. Is the source of the r graph.
siand ti represent source and target vertices of i -th edge (directed) and represents the cost of the di i -th E Dge.
Output
c0c1:c|V|?1
The output consists of |V| lines. Print the cost of the shortest path from the source to each r vertex 0, 1, ... in |V|?1 order. If there is no path from the source to a vertex, print INF.
Constraints
- 1 ≤ |V| ≤100000
- 0≤ di ≤10000
- 0≤ |E| ≤500000
- There is no parallel edges
- There is no self-loops
Sample Input 1
4 5 00 1 10 2 41 2 22 3 11 3 5
Sample Output 1
0134
Sample Input 2
4 6 10 1 10 2 42 0 11 2 23 1 13 2 5
Sample Output 2
302INF
This data range is larger, with a maximum of 100,000 vertices, with the adjacency matrix, the space will definitely overrun.
I use the adjacency table of Dijiksra to solve it.
Set up a template to come out:
#include <iostream>#include<algorithm>#include<map>#include<vector>using namespaceStd;typedefLong Longll;#defineINF 2147483647structedge{intto,cost;};intv,e; Vector<edge> g[100010];multimap<int,int>l;ll d[500010];voidDijkstraints) {Fill (d,d+V,inf); D[s]=0; L.insert (Make_pair (0, s)); while(L.size () >0){ intp = L.begin ()First ; intv = l.begin ()second; L.erase (L.begin ()); if(D[v] < P)Continue; for(inti =0; i < g[v].size (); i++) {Edge e=G[v][i]; if(D[e.to] > D[v] +e.cost) {D[e.to]= D[v] +E.cost; L.insert (Make_pair (d[e.to], e.to)); } } }}intMain () {intR; CIN>> V >> E >>R; for(inti =0; i < E; i++) {Edge E; int from; CIN>> from>> e.to >>E.cost; g[ from].push_back (e); } Dijkstra (R); for(inti =0; i < V; i++){ if(D[i]! = INF) cout << D[i] <<Endl; Elsecout <<"INF"<<Endl; } return 0;}
AOJ grl_1_a:single Source Shortest path (Dijktra algorithm for single source shortest path, adjacency table)