ACM-ICPC Asia (Urumqi Division) online Race H. Skiing (topological sort + false DP)

Source: Internet
Author: User

Title Link: https://nanti.jisuanke.com/t/16957

Topic:

In this winter holiday, Bob have a plan for skiing at the mountain resort.

This ski resort have MMM different ski paths and NNN Different flags situated at those turning points.< /c9>

TheIiI-th path from theSis_iSI?-th Flag to theTit_it i -th flag has length lil_i < Span class= "Mord mathit" >l i

Each path must follow the principal of reduction of heights and the start point must is higher than the end point strictly .

An available ski trail would start from a flag, passing through several flags along the paths, and end at another flag.

Now, your should help Bob find the longest available ski trail in the ski resort.

Input Format

The first line contains a integer ttT, indicating that there is TTt cases.

In each test case, the first line contains the integersNnN andMmM where0<n≤100000 < N \leq 100000 <n≤10000 and 0<m≤1000000 < M \leq 1000000< m≤100000 as Described above.

Each of the followingMmM lines contains three integersSis_iSI?,Tit_iTI?, andLi (0<li<1000) l_i~ (0 < l_i < 1000)Li  ( Span class= "Mord mathrm" >0<L < Span class= "Sizing reset-size6 size3 mtight" >i?<10< Span class= "Mord mathrm" >00) describing a path in the ski resort.

Output Format

For each test case, ouput one integer representing the length of the longest ski trail.

Sample input
15 41 3 32 3 43 4 13 5 2
Sample output
6
Test instructions: In this winter vacation, Bob has plans to ski in the mountain resort. This ski resort has m different ski runs and n different signs located at those corner points. The path from the first mark to the T flag has a length of L. Each path must follow the principle of lowering the height, and the starting point must be strictly above the end point. Now, you should help Bob find the longest ski runs.
Idea: The idea is to use topological sequencing to determine the path, because the degree of 0 points must be a starting point, and then gradually push back, and then use a DP array to store all the maximum distance to reach the pity Dorado, although I use a DP array, but this is actually not DP. However, in order to store the distance between the two marks, we change the usual topsort vector slightly, the subscript does not store a value, but instead stores the starting point and length, because open a 1w*1w two-dimensional array space + time is a bit fried. Finally, the maximum value of the DP for all flags can be calculated by traversing it again.

The code is implemented as follows:
1#include <queue>2#include <cstdio>3#include <vector>4#include <cstdlib>5#include <cstring>6#include <algorithm>7 using namespacestd;8 9 Const intMAXN = 1e4 +7;Ten intt, N, M, U, V, W; One intDP[MAXN],inch[MAXN]; Avector<pair<int,int>>G[MAXN]; -  - voidTopsort () { thequeue<int>Q; -      for(inti =1; I <= N; i++) { -         if(inch[I] = =0) Q.push (i); -     } +     intx; -      while(!Q.empty ()) { +x =Q.front (), Q.pop (); A         intK =g[x].size (); at          for(inti =0; I < K; i++) { -             if(--inch[G[x][i].first] = =0) Q.push (g[x][i].first); -Dp[g[x][i].first] = max (Dp[g[x][i].first], dp[x] +g[x][i].second); -         } -     } - } in  - intMain () { toscanf"%d", &t); +      while(t--) { -scanf"%d%d", &n,&m); theMemsetinch,0,sizeof(inch)); *Memset (DP,0,sizeof(DP)); $          for(inti =0; I <= N; i++) {Panax Notoginseng g[i].clear (); -         } the          for(inti =0; I < m; i++) { +scanf"%d%d%d", &u, &v, &W); A G[u].push_back (Make_pair (V, W)); the             inch[v]++; +         } - Topsort (); $         intMX =-1; $          for(inti =1; I <= N; i++) { -MX =max (MX, dp[i]); -         } theprintf"%d\n", MX); -     }Wuyi     return 0; the}

ACM-ICPC Asia (Urumqi Division) online Race H. Skiing (topological sort + false DP)

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.