Noip2013d1t3 Lorry transport

Source: Internet
Author: User

Title Link: http://www.luogu.org/problem/show?pid=1967

Data: http://www.cnblogs.com/wanglichao/p/5592058.html

Title Description

A state-owned N-City, numbering from 1 to N, between cities there is m two-way road. Each road has a weight limit for the vehicle, which is referred to as the limit. There are now Q trucks transporting goods, and drivers want to know that each vehicle can carry more than the maximum amount of cargo in the case of a vehicle's limited weight.

Input/output format

Input format:

The input file name is truck.in.

The first line of the input file has two integer n,m separated by a space, indicating a state-owned N-City and M-Road

Lu. The next M-line is 3 integers x, y, Z, and every two integers separated by a space, indicating that from the city of X to the city of Y there is a road limited to Z. Meaning: x is not equal to Y, there may be many roads between the two cities.

The next line has an integer q, which indicates that there is a Q truck that needs shipping.

Next Q line, two integers x, y per line, separated by a space, indicating that a lorry needs to transport goods from the X city to the Y city, note: x is not equal to Y.

Output format:

The output file name is Truck.out.

The output is a total Q line, an integer per line, indicating the maximum load for each lorry. If the goods

The car cannot reach its destination, output-1.

Input and Output Sample input example # #:
4 31 2 42 3 33 1 131 31 41 3
Sample # # of output:
3-13
Description

For 30% data, 0 < n < 1,000,0 < m < 10,000,0 < q< 1,000; for 60% data, 0 < n < 1,000,0 < m < 50,000, 0 < q< 1,000; for 100% data, 0 < n < 10,000,0 < m < 50,000,0 < q< 30,000,0≤z≤100,000.

This is really a hole in the beginning Tle+mle+re

Later found that their space is wrong, to divide up the discovery of space is actually O (n) (originally almost opened the N2), resolved re and MLE

Then change the day and then found that they did not hit the path compression (WOC, it is too long not hit and check set)

And then a 233.

Exercises

First Step conversion:

A subset of the edges in the original artwork can be deleted, as long as the largest spanning tree is preserved

Write A and check set, with greed, each time the right to add the largest side, merging two sets, while generating a tree

There is a good way on the Internet (forget which Daniel's) for each edge, a new point, as "the root node of the two points it connects to" the root node, because it is the largest spanning tree, each time you add an edge, the two points are certainly not the same tree (the same and check set), So it's guaranteed to generate a tree (the happiest is this or the two Fork tree 233)

There is a small hidden problem: What do you do with a forest, not a tree, mdzz?

I'm using a fairly inefficient approach: add a lot of points to the binary tree (which is the right side of the 0 in the original), and it's obviously not going to affect the results, but it's no harm to think about not preserving the two-tree nature, and just adding a root node to it ... (Mainly I write half only to find this problem-natural retardation-two of the tree are finished, too lazy to change), so the number of sides should be n-1 (all connected).

Since it is a tree, then just find a path, do the LCA can be

1.Tarjan offline

is said to be Tarjan offline, but I do not intend to write, after all, now write online is always more than the offline heart of the bottom point (offline almost did not write, last Mo team's topic was I skip directly)

2.RMQ Online

My method:

Do the conversions again, spread the tree out according to the DFS sequence:

For example, the data in the sample

(Don't bother looking online, draw one yourself)

Is that each time a Dfs finds a point (including the back of the trail) it records once (and records the depth)

If you know the position of two points in the sequence (first occurrence), you can get the number of their root node (that is, the point with the least depth between two points)

So it turns out to be a magical interval-minimization problem.

Pre-processing the DP, preprocessing the minimum value of each small block

Make Dp[i][j] The minimum value of 2j elements starting from I

It's easy to think of the transfer equation from J-1 to J.

Dp[i][j]=min (Dp[i][j-1],dp[i+2j-1][j-1])

In each query, just find the power of the largest 2, which is less than or equal to the interval size, and it will be a bit more than two DP.

(even drawing is too lazy to use)

DP[X][J]

----------------------------------

|                            | Y is the end

This is the data to find the minimum value

X is the beginning | |

------------------------------

DP[Y-2J+1][J]

Min (Dp[y-2j+1][j],dp[x][j]) is the answer to the question

Then the minimum value of each non-leaf node (i.e. the edge of the original) on the path is calculated.

If 0 is not connected, not 0 output

Problem solving

On the Code

#include <cstdio>#include<algorithm>using namespacestd;structhaha{intA,b,c;} a[50001];BOOLcom (haha a,haha b) {return(a.c>B.C);}intd[200001],e[200001],po[10001],l[70001],r[70001],f[70001],gp[70001],dp[150001][ -],_dp[150001][ -],p[150001],q[150001];intN,m,_n;intRead () {intx=0;CharCh=GetChar ();  while(ch<'0'|| Ch>'9') ch=GetChar ();  while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx;}intDfsintKintPintQ//turn a diagram into a table by DFS order{D[p]=Q; E[P]=K; intans=0; if(k<=N) {po[k]=p; return 1; } ans+=dfs (l[k],p+1, q+1); Ans++; D[p+ans]=Q; E[p+ans]=K; Ans+=dfs (r[k],p+ans+1, q+1); Ans++; D[p+ans]=Q; E[p+ans]=K; return++ans;}intFaintP//the ancestor of the set (in fact, the root node of the tree){    if(f[gp[p]]==Gp[p])returnGp[p]; Else{Gp[p]=FA (F[gp[p]]); returnGp[p]; }}voidInit ()//process the input graph, add 0 sides, create a maximum spanning tree (avoid the forest){sort (a+1, a+m+1, com);  for(intI=1; i<=n+m;i++) {F[i]=i; Gp[i]=i; }     for(intI=1; i<=m;i++)    if(FA (A[I].A)! =fa (a[i].b)) {F[n+i]=n+i; L[n+i]=fa (A[I].A); R[n+i]=fa (a[i].b); F[FA (A[I].A)]=n+i; F[FA (a[i].b)]=n+i; }    int_i=n+m;  for(intI=1; i<n;i++)    if(FA (i)!=fa (i+1) ) {_i++; F[_i]=_i; Gp[_i]=_i; L[_i]=fa (i); R[_i]=FA (i+1); F[FA (i)]=_i; F[FA (i+1)]=_i; }}voidCalc ()//The interval minimum value preprocessing is obtained{     for(intI=1; i<=_n;i++) {dp[i][0]=D[i]; _dp[i][0]=i; }    intj=1, k=2;  while(k<=_n) {         for(intI=1; i<=_n-k+1; i++)        if(dp[i][j-1]<dp[i+k/2][j-1]) {Dp[i][j]=dp[i][j-1]; _DP[I][J]=_dp[i][j-1]; }        Else{Dp[i][j]=dp[i+k/2][j-1]; _DP[I][J]=_dp[i+k/2][j-1]; } J++; K*=2; } k=1; j=0;  for(intI=1; i<=_n;i++)    {        if(i>=2*k) {k*=2; J++; } P[i]=K; Q[i]=K; }}intLcaintXintY//find the interval minimum value{    return(dp[x][p[y-x+1]]>dp[y-q[y-x+1]+1][p[y-x+1]])? _dp[y-q[y-x+1]+1][p[y-x+1]]:_dp[x][p[y-x+1]];}intMain () {n=read (); M=read ();  for(intI=1; i<=m;i++) {a[i].a=read (); A[I].B=read (); A[I].C=read ();    } init (); _n=dfs (FA (1),1,0);    Calc (); M=read ();  for(intI=1; i<=m;i++)    {        intx, y; X=read (); Y=read (); X=po[x];y=Po[y]; if(x>y) Swap (x, y); intO=lca (x, y), sum=a[e[o]-n].c;  for(x=f[e[x]];x!=e[o];x=f[x]) sum=min (sum,a[x-n].c);  for(y=f[e[y]];y!=e[o];y=f[y]) sum=min (sum,a[y-n].c); printf ("%d\n", sum?sum:-1); }    return 0; }

Noip2013d1t3 Lorry transport

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.