Out of Hay
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8710 | Accepted: 3298 |
Description
The cows have run out of hay, a horrible event that must be remedied immediately. Bessie intends to visit the other farms to survey their hay situation. There are N (2 <= N <= 2,000) farms (numbered 1..N); Bessie starts at Farm 1. She'll traverse some or all of the M (1 <= M <= 10,000) two-way roads whose length does not exceed 1,000,000,000 that connect the farms. Some farms may be multiply connected with different length roads. All farms are connected one way or another to Farm 1.
Bessie is trying to decide how large a waterskin she will need. She knows that she needs one ounce of water for each unit of length of a road. Since she can get more water at each farm, she's only concerned about the length of the longest road. Of course, she plans her route between farms such that she minimizes the amount of water she must carry.
Help Bessie know the largest amount of water she will ever have to carry: what is the length of longest road she'll have to travel between any two farms, presuming she chooses routes that minimize that number? This means, of course, that she might backtrack over a road in order to minimize the length of the longest road she'll have to traverse.
Bessie is trying to decide how large a waterskin she will need. She knows that she needs one ounce of water for each unit of length of a road. Since she can get more water at each farm, she's only concerned about the length of the longest road. Of course, she plans her route between farms such that she minimizes the amount of water she must carry.
Help Bessie know the largest amount of water she will ever have to carry: what is the length of longest road she'll have to travel between any two farms, presuming she chooses routes that minimize that number? This means, of course, that she might backtrack over a road in order to minimize the length of the longest road she'll have to traverse.
Input
* Line 1: Two space-separated integers, N and M.
* Lines 2..1+M: Line i+1 contains three space-separated integers, A_i, B_i, and L_i, describing a road from A_i to B_i of length L_i.
* Lines 2..1+M: Line i+1 contains three space-separated integers, A_i, B_i, and L_i, describing a road from A_i to B_i of length L_i.
Output
* Line 1: A single integer that is the length of the longest road required to be traversed.
Sample Input
3 3 1 2 23 2 3 1000 1 3 43
Sample Output
43
分析:输入n个顶点,m条边,求图最小生成树的最大权值。
注意1:本题有重复的边,用prime算法的话得将重复的边去掉,将两点间的所有边选择一条最小的就可以了。我用的kruskal算法,不用考虑重复的边。
注意2:用scanner太慢
package pk; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.StreamTokenizer; import java.util.Arrays; import java.util.PriorityQueue; /** * @功能Function Description: kruskal算法 * @开发环境Environment: eclipse * @技术特点Technique: * @算法 * @版本Version: Scanner * @作者Author: follow your dreams * @日期Date: 20120830 * @备注Notes: * @链接: http://poj.org/problem?id=2395 */ public class PKU2395_2_20120831 { public static PriorityQueue<Node> pq; public static int father[]; public static int rank[];//把rank[]去掉,时间是1500ms(去掉秩时间上提高的不多) public static void main(String[] args) throws IOException { //Scanner sc = new Scanner(System.in);//注意:scanner 运行时间:27XXms StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));//StreamTokenizer运行时间:14XXms(和scanner差1倍) int m, n, a, b, value; pq = new PriorityQueue<Node>(); while(st.nextToken()!=StreamTokenizer.TT_EOF) { n = (int)st.nval; st.nextToken(); m = (int)st.nval; father = new int[n+1]; rank = new int[n+1]; for(int i=1; i<=m; i++) { st.nextToken(); a = (int)st.nval; st.nextToken(); b = (int)st.nval; st.nextToken(); value = (int)st.nval; pq.add(createNode(a, b, value)); } System.out.println(kruskal(n)); pq.clear(); } } public static void make_set() { int len = father.length; for(int i=0; i<len; i++) { father[i] = i; } Arrays.fill(rank, 0); } public static int find(int x) { if(x != father[x]) { father[x] = find(father[x]); } return father[x]; } public static void union(int x, int y) { x = find(x); y = find(y); if(rank[x] > rank[y]) { father[y] = x; } else { father[x] = y; if(rank[x] == rank[y]) { rank[y]++; } } } public static int kruskal(int n) { int max = Integer.MIN_VALUE; make_set(); int count = 0; while(count != n-1) { Node temp = pq.poll(); int set1 = find(temp.a); int set2 = find(temp.b); if(set1 != set2) { if(max < temp.value) { max = temp.value; } union(set1, set2); ++count; } } return max; } public static Node createNode(int a, int b, int value) { return new PKU2395_2_20120831().new Node(a, b, value); } private class Node implements Comparable<Node>{ int a; int b; int value; public Node(int a, int b, int value) { this.a = a; this.b = b; this.value = value; } @Override public int compareTo(Node arg0) { return this.value - arg0.value; } } }