[JOBDU] Topic 1545: Strange connected diagrams

Source: Internet
Author: User

Title Description:

A non-weighted graph is known to find the smallest integer k. To make the only use value less than or equal to K, node 1 can be connected to node N.

Input:

The input contains multiple sets of test cases, each set of test cases starting with an integer n (1 <= n <= 10000), M (1 <= m <= 100000), representing the number of vertices, and the number of edges, of the weighted graph.
Next to the M line, the information describing the top of the graph, including three integers, a (1 <= a <= N), B (1 <= b <= N), C (1 <= C <= 1000000), represents the non-forward edge of the connection vertex A and vertex B, with a weight of c.

Output:

The output is an integer k, and output-1 if an integer is not found to satisfy the condition.

Sample input:
 
  
Sample output:
35-1

Start to see the topic feel good complex, first think of is a binary search, but even the two-point search, the complexity is too high. In fact, this is the test and check set, by the length of the side of the order, the first time found that N and 1 connected to the edge is the K. Because it's already been sorted.

1#include <iostream>2#include <vector>3#include <algorithm>4#include <cstdio>5 using namespacestd;6  7 structedge_t {8     intA, B;9     intDist;Ten }; One   A intN, M; -Vector<edge_t>edges; -vector<int>father; the   - intFindfather (intx) { -      while(X! =Father[x]) { -x =Father[x]; +     } -     returnx; + } A   at BOOLcmpConstedge_t &a,ConstEdge_t &b) { -     returnA.dist <b.dist; - } -   - voidinit () { -      for(inti =0; I < father.size (); ++i) inFather[i] =i; - sort (Edges.begin (), Edges.end (), CMP); to } +   - voidsolve () { the      for(inti =0; I < m; ++i) { *         intFA =Findfather (EDGES[I].A); $         intFB =Findfather (edges[i].b);Panax Notoginseng         if(FA < FB) FATHER[FB] =FA; -         ElseFATHER[FA] =FB; the         if(Findfather (n) = =1) { +cout << edges[i].dist <<Endl; A             return; the         } +     } -cout <<"-1"<<Endl; $ } $   - intMain () { -      while(SCANF ("%d%d", &n, &m)! =EOF) { theFather.resize (n +1); - edges.resize (m);Wuyi          for(inti =0; I < m; ++i) { thescanf" %d%d%d", &AMP;EDGES[I].A, &edges[i].b, &edges[i].dist); -         } Wu init (); - solve (); About     } $     return 0; - } - /************************************************************** - problem:1545 A user:hupo250 + language:c++ the result:accepted - time:640 Ms $ memory:2248 KB the ****************************************************************/

[JOBDU] Topic 1545: Strange connected diagrams

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.