POJ3159 dijkstra+ adjacency table + priority queue

Source: Internet
Author: User

today I learned a new posture, using adjacency table + priority queue to optimize Dijkstra, so that time complexity from O (n^2+e) to O (nlogn+e), mom no longer have to worry about my timeout! ~\ (^o^)/

Candies

Time Limit: 1500MS Memory Limit: 131072K
Total Submissions: 25077 Accepted: 6810

Description

During The Kindergarten days, Flymouse is the monitor of his class. Occasionally the head-teacher brought the kids of Flymouse ' s Class A large bag of candies and had flymouse distribute them . All the kids loved candies very much and often compared the numbers for candies they got with others. A kid A could had the idea that though it might is the case that another kid B is better than him in some aspect and ther Efore had a reason for deserving more candies than he did, he should never get a certain number of candies fewer than B di D no matter how many candies he actually got, otherwise he would feel dissatisfied and go to the head-teacher to complain About Flymouse ' s biased distribution.

Snoopy shared class with Flymouse at that time. Flymouse always compared the number of he candies with that of Snoopy ' s. He wanted to make the difference between the numbers as large as possible while keeping every kid satisfied. Now he had just got another bag of candies from the Head-teacher, what is the largest difference he could make out of it?

Input

The input contains a single test cases. The test cases starts with a line with both integers N and M not exceeding and respectivel Y. N is the number of kids in the class and the kids were numbered 1 throughN. Snoopy and Flymouse were always numbered 1 and N. Then follow M lines each holding three integers A, B and C in order, meaning that kid A believed that kid B should never get over C candies more than he did.

Output

Output one line with only the largest difference desired. The difference is guaranteed to be finite.

Sample Input

2 21 2 52 1 4

Sample Output

5
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <queue>6#include <vector>7 using namespacestd;8 #defineINF 0x7fffffff9 structnodeTen { One     intk,w; A }; - BOOL operator< (ConstNode &t1,ConstNode &T2) - { the     returnT1.w>T2.W; - } -Priority_queue<node>Q; - BOOLvis[30010]; +vector<vector<node> >v; - intMain () + { A     intn,m,i,j,a,b,c; at node p; -      while(~SCANF ("%d%d",&n,&m)) -     { - v.clear (); -V.resize (n+1); -memset (Vis,0,sizeof(Vis)); in          for(i=0; i<m;i++) -         { toscanf"%d%d%d",&a,&b,&c); +p.k=b; -p.w=C; the V[a].push_back (p); *         } $p.k=1;Panax Notoginsengp.w=0; - Q.push (p); the          while(!q.empty ()) +         { Ap=q.top (); the Q.pop (); +             if(VIS[P.K]) -                 Continue; $vis[p.k]=true; $             if(p.k==N) -                  Break; -              for(i=0; I<v[p.k].size (); i++) the             { - node S;Wuyis.k=V[P.K][I].K; the                 if(VIS[S.K])Continue; -s.w=p.w+V[P.K][I].W; Wu Q.push (s); -             } About         } $printf"%d\n", P.W); -     } -     return 0; -}
View Code

POJ3159 dijkstra+ adjacency table + priority queue

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.