A very challenging topic of direct violence to build the words of No doubt O (n^2) will be tle each layer of virtual a point and will let no point of the layer can also connect the past
Refer to the method of Kuangbin chicory two virtual points per layer n+i*2-1 is the entrance n+i*2 is the exit and then the one-way side.
VA once, because MAXN should be twice times bigger than the data. Accidentally ignored as far as MAXM directly to 1e7
#include <iostream>#include<string>#include<cstdio>#include<cmath>#include<cstring>#include<queue>#include<map>#include<vector>#include<Set>#include<algorithm>#defineINF 0x3f3f3f3fusing namespacestd;Const intMAXN = 1e6 +Ten;Const intMAXM = 2e7 +Ten; typedef pair<int,int>PII;structcmp{BOOL operator()(ConstPII A,ConstPII b) { returnA.first >B.first; }};intsize, HEAD[MAXN], POINT[MAXM], NXT[MAXM], VAL[MAXM];intt, N, M, C, DIST[MAXN];voidinit () {size=0; memset (Head,-1,sizeofhead);} InlinevoidAddint from,intTo,intvalue) {Val[size]=value; Point[size]=to ; Nxt[size]= head[ from]; head[ from] = size++;}voidDij () {priority_queue<pii, Vector<pii>, cmp>Q; memset (Dist,0x3f,sizeofDist); Q.push (Make_pair (0,1)); dist[1] =0; while(!Q.empty ()) {PII u=Q.top (); Q.pop (); if(U.first > Dist[u.second])Continue; for(inti = Head[u.second]; ~i; i =Nxt[i]) { if(Dist[point[i] > Dist[u.second] +Val[i]) {Dist[point[i]]= Dist[u.second] +Val[i]; Q.push (Make_pair (Dist[point[i]), point[i]); } } }}intMain () {scanf ("%d", &t); for(intKase =1; Kase <= t; kase++) {init (); scanf ("%d%d%d", &n, &m, &c); for(inti =1; I <= N; i++){ intlayer; scanf ("%d", &layer); Add (n+2*layer-1I0); Add (i, n+2*layer,0); } for(inti =1; I < n; i++) {Add (n+2*i, n +2* (i+1) -1, c); Add (n+2* (i+1), n +2*i-1, c); } intu, V, W; for(inti =1; I <= m; i++) {scanf ("%d%d%d", &u, &v, &W); Add (U, V, W); Add (V, u, W); } dij (); printf ("Case #%d:%d\n", Kase, dist[n] = = INF? -1: Dist[n]); } return 0;}
KUANGBIN_SHORTPATHP (HDU 4725)