2014多校(1011)hdu4871

Shortest-path tree

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 130712/130712 K (Java/Others)
Total Submission(s): 407    Accepted Submission(s): 125


Problem Description
Given a connected, undirected graph G, a shortest-path tree rooted at vertex v is a spanning tree T of G, such that the path distance from root v to any other vertex u in T is the shortest path distance from v to u in G. 
We may construct a shortest-path tree using the following method:
We consider a shortest-path tree rooted at node 1. For every node i in the graph G, we choose a shortest path from root to i. If there are many shortest paths from root to i, we choose the one that the sequence of passing nodes' number is lexicographically minimum. All edges on the paths that we chose form a shortest-path tree.
Now we want to know how long are the longest simple paths which contain K nodes in the shortest-path tree and how many these paths? Two simple paths are different if the sets of nodes they go through are different.
 

Input
The first line has a number T (T <= 10), indicating the number of test cases.
For each test case, the first line contains three integers n, m, k(1<=n<=30000,1<=m<=60000,2<=k<=n), denote the number of nodes, the number of edges and the nodes of required paths.
Then next m lines, each lines contains three integers a, b, c(1<=a, b<=n, 1<=c<=10000),denote there is an edge between a, b and length is c.
 

Output
For each case, output two numbers, denote the length of required paths and the numbers of required paths.
 

Sample Input
    
    
    
    
1 6 6 4 1 2 1 2 3 1 3 4 1 2 5 1 3 6 1 5 6 1
 

Sample Output
    
    
    
    
3 4

第一次写树的分治,感觉又涨姿势了,哈哈~~~
思路:先求最短路树,我的方法是先按节点号从小到大插入邻接表,求出最短路后用DFS按节点号从小到大跑出最短路树(感觉这种方法还是太挫了,想不到更好的),
然后用树的分治求解,总体思路就是每次找到树的重心,然后以重心为中心(也就是说这条路径要经过中心)进行K个节点最大树链的求解,重心怎么求呢,可以去百度,这里不做介绍了。用一个pow[k]和pnum[k]数组维护当前分治求解的这颗树中的经过k个节点最大的路径长度和数量,然后每次从中心出发开拓新的子树的时候计数即可。


你可能感兴趣的:(ACM)