CCF CSP The tenth time fourth question subway builds JAVA edition __java

Source: Internet
Author: User

Competition in the Java version of the code to see a relatively small, the blogger collation release:


Subway construction

Problem DescriptionA city has n transportation hubs, of which number 1th and n are very important, in order to enhance the transportation capacity, a city decided to build a subway between 1th and N hubs.
The MTR consists of a number of tunnels, each connecting two transport hubs. After exploration, there are M-segment tunnels as candidates, with a maximum of one candidate tunnel between the two transport hubs, and no tunnel ends are connected to the same traffic hub.
Now there are n tunnel construction companies, each candidate tunnel can only be a company construction, each company construction needs of the number of days in line. And each company can only build a candidate tunnel. All the companies started construction at the same time.
As the project leader, you get the candidate tunnel information, now you can choose a part of the tunnel to do the construction, how many days to build the whole subway.Input FormatThe first line entered contains two integers n, m, separated by a space, representing the number of traffic hubs and the number of candidate tunnels.
Line 2nd to m+1, each row contains three integers a, B, C, which indicates that a tunnel can be built between hub A and hub B, which takes a time of C days.output FormatOutput an integer, the minimum number of days to build the entire subway line.Sample Input6 6
1 2 4
2 3 4
3 6 7
1 4 2
4 5 5
5 6 6Sample Output6 examples show that there are two types of lines that can be built.
The first route through the hub is 1, 2, 3, 6, the time required is 4, 4, 7, then the entire subway line needs to be completed 7 days;
The second route is 1, 4, 5, 6, the time required is 2, 5, 6, then the entire subway line needs to be completed in 6 days.
The second scenario uses fewer days.evaluation use case scale and conventionsFor 20% of the evaluation use cases, 1≤n≤10,1≤m≤20;
For 40% of the evaluation use cases, 1≤n≤100,1≤m≤1000;
For 60% of the evaluation use cases, 1≤n≤1000,1≤m≤10000,1≤c≤1000;
For 80% of the evaluation use cases, 1≤n≤10000,1≤m≤100000;
For 100% of the evaluation cases, 1≤N≤100000,1≤M≤200000,1≤A, b≤n,1≤c≤1000000.

All evaluation cases ensure that hub 1th can reach all other hubs through tunnels when all candidate tunnels are repaired.


train of thought : The method of using and looking at the set, first of all the edges in order from small to large, and constantly select the smallest edge, until the first node and the last node connected (that is, their ancestors the same) until. Output the corresponding number of days (weights) of this edge, which is the request.


Java run slower than C + +, the code will run overtime, the title score 85 points.


Package ten;
Import java.util.ArrayList;
Import java.util.Collections;

Import Java.util.Scanner;
	public class Four {int [] par;
	
	int [] rank;
		public static void Main (string[] args) {//TODO auto-generated method stub Scanner scan=new Scanner (system.in);
		int N=scan.nextint ();
		int M=scan.nextint ();
		Edge [] es=new edge[m];
		Arraylist<edge> alist=new arraylist<edge> ();
			for (int i=0;i<m;++i) {es[i]=new Edge (Scan.nextint (), Scan.nextint (), Scan.nextint ());
		Alist.add (Es[i]);
		} Four f=new Four ();
		F.par=new int[n+5];
		F.rank=new int[n+5];
		F.init (n);
		Collections.sort (alist);
			for (int i=0;i<alist.size (); ++i) {F.unite (Alist.get (i). A,alist.get (i). b);
				if (F.same (1, N)) {System.out.println (Alist.get (i). Day);
			Return
			}} public void init (int n) {A for (int i=1;i<=n;++i) {par[i]=i;
		rank[i]=0;
		} public int find (int x) {if (par[x]==x) {return x;
		} else{return Par[x]=find (Par[x]); }} public void Unite (int x,int y) {x=find (x);
		Y=find (y);
		if (x==y) {return;
		} if (Rank[x]<rank[y]) {par[x]=y;
			} else{par[y]=x;
			if (Rank[x]==rank[y]) {rank[x]++;
	}} public boolean same (int x,int y) {return find (x) ==find (y);
	} class Edge implements comparable<edge>{int A;
	int b;

	int day;
		Edge (int a,int b,int day) {this.a=a;
		This.b=b;
	This.day=day;
		public int CompareTo (Edge e) {if (this.day<e.day) {return-1;
		else if (this.day>e.day) {return 1;
		} else{return 0;
 }
	}
}



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.