[Noip2013] freight car transportation

Source: Internet
Author: User
Description

A has n cities in China, ranging from 1 to n. There are m two-way roads between cities. Each road imposes weight limitations on vehicles. Now there are Q trucks carrying goods. drivers want to know that each truck can carry a maximum of multiple goods without exceeding the vehicle weight limit.

Format input format

The first line has two integers n, m separated by a space, indicating N cities and M roads in.

Next, line M contains three integers x, y, and z in each row. Each integer is separated by a space, which indicates that there is a road with a limited weight of Z from city X to city y. Note: X is not equal to Y. There may be multiple roads between two cities.

The next line contains an integer Q, indicating that a truck needs to deliver the goods.

Next, in row Q, two integers x and y are separated by a space, indicating that a freight car needs to transport goods from city X to city Y. Note: X is not equal to y.

Output Format

The output contains a total of Q rows. Each line has an integer representing the maximum load of each truck. If the freight car cannot reach the destination, output-1.

Example 1 input 1
 
??4 3 1 2 4 2 3 3 3 1 1 31 3 1 4 1 3
Sample output 1
 
3-13
Restrictions

1 s for each test point.

Prompt

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

Source

Noip 2013 raise group Day 1

 

First, based on the nature of the maximum spanning tree, it can ensure that the transportation of goods on the maximum spanning tree is better than that on other sides.

Then the problem is converted to finding the minimum edge weight of the edge with two points in the maximum generation tree.

How can this problem be solved? LCA !!

 

The solution to this question is: Find the LCA in the maximum generation tree.

 

 

# Include <cstdio>
# Include <vector>
# Include <algorithm>
# Define maxm 50010
# Define maxn10010
Using namespace STD;
Int n, m, Z [maxn], deep [maxn], Q;
Bool vis [maxn];
Struct edge {int from, to, DIST;} edge0 [maxm];
Struct tree {int to, DIST;} father [maxn];
Vector <tree> tree [maxn];
Bool CMP (edge a, edge B) {return a. Dist> B. Dist ;}
Int findp (int x) {return Z [x] = x? X: Z [x] = findp (Z [x]);}
Void addtree (INT ff, int TT, int dd) {tree [ff]. push_back (tree) {TT, dd });}
Void DFS (int u)
{
For (INT I = 0; I <tree [u]. Size (); I ++)
{
Tree & E = tree [u] [I];
If (! Vis [E. To])
{
Vis [E. To] = 1;
Father [E. To]. To = u;
Father [E. To]. Dist = E. Dist;
Deep [E. To] = deep [u] + 1;
DFS (E. );
}
}
}
Int LCA (int A, int B)
{
Int ret = 2100000000;
If (deep [a]> deep [B]) Swap (A, B );
While (deep [a]! = Deep [B]) ret = min (Ret, Father [B]. Dist), B = Father [B].;
While (! = B) ret = min (Ret, Father [B]. dist), B = Father [B]. to, ret = min (Ret, Father [A]. dist), A = Father [A]. to;
Return ret;
}
Int main ()
{
Scanf ("% d", & N, & M );
For (INT I = 1; I <= N; I ++) Z [I] = I;
For (INT I = 1; I <= m; I ++)
Scanf ("% d", & edge0 [I]. From, & edge0 [I]. To, & edge0 [I]. Dist );
Sort (edge0 + 1, edge0 + 1 + M, CMP );
For (INT I = 1; I <= m; I ++)
{
Int AA = edge0 [I]. From, BB = edge0 [I]. To, Cc = edge0 [I]. Dist;
Int F1 = findp (AA), f2 = findp (bb );
If (F1 = F2) continue;
Z [F2] = F1;
Addtree (AA, BB, CC );
Addtree (BB, AA, CC );
}
For (INT I = 1; I <= N; I ++)
If (! Vis [I])
{
Vis [I] = 1;
Father [I]. To = 0;
Father [I]. Dist =-1;
DFS (I );
}
Scanf ("% d", & Q );
While (Q --)
{
Int AA, BB;
Scanf ("% d", & aa, & bb );
Printf ("% d \ n", LCA (AA, BB ));
}
Return 0;
}

[Noip2013] freight car transportation

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.