The shortest way.
Idea: the shortest way.
Stick to a more efficient heap optimization dij template.
CODE:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define _max 1000010#define MAX 10000010using namespace std; #define MIN (b) ((a) < (b)? a:b) long Long f[_max];struct heap{int num[ _max],pos[_max],size;void pushup (int p) {while (P > 1) {if (F[num[p]] < f[num[p >> 1]) {Swap (num[p],num[p >& Gt 1]); Swap (Pos[num[p]],pos[num[p >> 1]]);p >>= 1;} Elsebreak;}} void Insert (Long long x) {num[++size] = x;pos[x] = size; Pushup (size);} void Pop () {pos[num[1]] = 0;num[1] = num[size--];if (size) pos[num[1]] = 1;int now = 2;while (now < size) {if (F[num[now + 1]] < F[num[now]]) ++now;if (F[num[now] [< F[num[now >> 1]]) {swap (Num[now],num[now >> 1]); Swap (pos[num [Now]],pos[num[now >> 1]]; now <<= 1;} Elsebreak;}}} Heap;int Points,edges;long long T,rxa,rxc,rya,ryc,rp;int head[_max],total;int next[max],aim[max],length[max];inline void Add (int x,int y,int len) {next[++total] = head[x];aim[total] = Y;length[totAL] = len;head[x] = total;} void Dijkstra () {memset (f,0x3f,sizeof (f)); F[1] = 0;for (int i = 1; I <= points; ++i) heap. Insert (i); while (heap.size) {int x = heap.num[1]; heap. Pop (); for (int i = head[x]; i; i = Next[i]) if (F[aim[i]] > F[x] + length[i]) f[aim[i]] = f[x] + length[i],heap. Pushup (Heap.pos[aim[i]);}} int main () {cin >> points >> edges;cin >> T >> rxa >> rxc >> rya >> ryc >> Rp;int x = 0,y = 0,z = 0;int a,b;for (int i = 1; I <= T; ++i) {x = ((Long long) x * rxa + rxc)% Rp;y = ((long long) y * r Xa + rxc)% Rp;a = min (x% points + 1,y% points + 1); b = y% points + 1;if (A! = b) Add (a,b,1e8-100 * a);} for (int i = T + 1; I <= edges; ++i) {scanf ("%d%d%d", &x,&y,&z); ADD (x, y, z);} Dijkstra (); cout << f[points] << endl;return 0;}
Bzoj 3040 Shortest Path (road) heap optimization Dijkstra