The shortest way to the topic.
Train of thought: shortest way.
Post a more efficient heap optimization dij template.
CODE:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define _max
1000010 #define MAX 10000010 using namespace std;
#define MIN (a,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 >> 1]);
Swap (Pos[num[p]],pos[num[p >> 1]]);
P >>= 1;
else break;
} 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;
else break;
}}}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 = 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 * rxa + 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; }