Poj 1151 invitation cards Dijkstra heap Optimization

Source: Internet
Author: User

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 ;}

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.