The shortest path is bare, but the number of nodes and the number of sides are both 1e6. dij cannot be used directly, and the sparse graph can be optimized with heap.
O (Priority □priority) O note that the priority queue in STL is in front of the priority queue (with a large value). I didn't pay attention to WA several times at the beginning. Ah, it's so careless.
# Include <cstdio> # include <cstring> # include <cmath> # include <algorithm> # include <climits> # include <string> # include <iostream> # include <map> # include <cstdlib> # include <list> # include <set> # include <queue> # include <stack> using namespace STD; typedef long ll; const int maxn = 1000000 + 5; const ll INF = 0000000005ll; int U [maxn], V [maxn], W [maxn]; long long d [maxn]; int first [maxn], NXT [maxn]; bool vis [Max N]; int n, m; void add (INT _ u, int ID) {NXT [ID] = first [_ u]; first [_ u] = ID ;} struct node {long a; int B; node (long a, int B): A (a), B (B) {} bool operator <(const node & X) const {return a> X. A ;}}; // priority queue optimized inclustravoid Dijkstra (int * V) {memset (VIS, 0, sizeof (VIS); For (INT I = 1; I <= N; I ++) d [I] = inf; d [1] = 0; priority_queue <node> q; q. push (node (d [1], 1); While (! Q. empty () {node now = Q. top (); q. pop (); int x = now. b; If (vis [x]) continue; vis [x] = true; For (INT I = first [X]; I! = 0; I = NXT [I]) {If (d [V [I]> d [x] + W [I]) {d [V [I] = d [x] + W [I]; q. push (node (d [V [I], V [I]) ;}}} int main () {int t; scanf ("% d ", & T); For (INT Kase = 1; Kase <= T; Kase ++) {memset (first, 0, sizeof (first); memset (NXT, 0, sizeof (NXT); long ans = 0; scanf ("% d", & N, & M); For (INT I = 1; I <= m; I ++) {scanf ("% d", & U [I], & V [I], & W [I]); add (U [I], I);} Dijkstra (V); For (INT I = 1; I <= N; I ++) ans + = d [I]; memset (first, 0, sizeof (first); memset (NXT, 0, sizeof (NXT); For (INT I = 1; I <= m; I ++) {Add (V [I], I);} Dijkstra (U); For (INT I = 1; I <= N; I ++) ans + = d [I]; cout <ans <Endl;} return 0 ;}