-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pokemon.java
118 lines (107 loc) · 2.78 KB
/
Pokemon.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
/*
br = new BufferedReader(new FileReader("input.txt"));
pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
*/
public class Pokemon {
private static BufferedReader br;
private static StringTokenizer st;
private static PrintWriter pw;
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
//int qq = 1;
//int qq = Integer.MAX_VALUE;
int qq = readInt();
for(int casenum = 1; casenum <= qq; casenum++) {
int n = readInt();
int e = readInt();
LinkedList<Edge>[] edges = new LinkedList[n];
for(int i = 0; i < n; i++) {
edges[i] = new LinkedList<Edge>();
}
while(e-- > 0) {
int a = readInt();
int b = readInt();
double d = readDouble();
edges[a].add(new Edge(b, d));
edges[b].add(new Edge(a, d));
}
double[] dp = new double[n];
dp[0] = 1;
PriorityQueue<Vertex> q = new PriorityQueue<Vertex>();
q.add(new Vertex(0, 1));
while(!q.isEmpty()) {
Vertex curr = q.poll();
if(dp[curr.v] != curr.w) continue;
for(Edge out: edges[curr.v]) {
int nextD = out.d;
double nextW = curr.w * out.w;
if(nextW > dp[nextD]) {
dp[nextD] = nextW;
q.add(new Vertex(nextD, dp[nextD]));
}
}
}
pw.printf("Case %d: %.2f\n", casenum, dp[1]);
}
exitImmediately();
}
static class Edge {
public int d;
public double w;
public Edge(int a, double b) {
d=a;
w=b;
}
}
static class Vertex implements Comparable<Vertex> {
public int v;
public double w;
public Vertex(int a, double b) {
v=a;
w=b;
}
public int compareTo(Vertex curr) {
return Double.compare(curr.w, w);
}
}
private static void exitImmediately() {
pw.close();
System.exit(0);
}
private static long readLong() throws IOException {
return Long.parseLong(nextToken());
}
private static double readDouble() throws IOException {
return Double.parseDouble(nextToken());
}
private static int readInt() throws IOException {
return Integer.parseInt(nextToken());
}
private static String nextLine() throws IOException {
if(!br.ready()) {
exitImmediately();
}
st = null;
return br.readLine();
}
private static String nextToken() throws IOException {
while(st == null || !st.hasMoreTokens()) {
if(!br.ready()) {
exitImmediately();
}
st = new StringTokenizer(br.readLine().trim());
}
return st.nextToken();
}
}