Bzoj 3040 Shortest Path (road) heap optimization Dijkstra

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.