Skip to content

Commit 07ac7c9

Browse files
committed
add POJ 3268 for Java
1 parent 63684ad commit 07ac7c9

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

Java/POJ/3268/Main.java

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import java.util.LinkedList;
2+
import java.util.PriorityQueue;
3+
import java.util.Scanner;
4+
5+
public class Main {
6+
final int INF = Integer.MAX_VALUE / 3;
7+
class Edge {
8+
int b, t;
9+
Edge(int to, int cost) {
10+
this.b = to; this.t = cost;
11+
}
12+
}
13+
class Pair implements Comparable<Pair> {
14+
int f, s;
15+
Pair(int cost, int to) {
16+
this.f = cost; this.s = to;
17+
}
18+
public int compareTo(Pair p) {
19+
return this.f - p.f;
20+
}
21+
}
22+
class Graph {
23+
LinkedList<Edge> edges = new LinkedList<Edge>();
24+
}
25+
void run() {
26+
Scanner scan = new Scanner(System.in);
27+
int n = scan.nextInt();
28+
int m = scan.nextInt();
29+
int x = scan.nextInt();
30+
Graph[] g = new Graph[n + 1];
31+
for (int i = 0; i <= n; i++)
32+
g[i] = new Graph();
33+
for (int i = 0; i < m; i++) {
34+
int a = scan.nextInt();
35+
int b = scan.nextInt();
36+
int t = scan.nextInt();
37+
g[a].edges.add(new Edge(b, t));
38+
}
39+
int[][] d = new int[n+1][n+1];
40+
41+
PriorityQueue<Pair> que = new PriorityQueue<Pair>();
42+
43+
for (int t = 1; t <= n; t++) {
44+
for (int i = 0; i <= n; i++)
45+
d[t][i] = INF;
46+
que.clear();
47+
d[t][t] = 0;
48+
que.add(new Pair(0, t));
49+
50+
while(!que.isEmpty()) {
51+
Pair p = que.poll();
52+
int v = p.s;
53+
if (d[t][v] < p.f) continue;
54+
for (int i = 0; i < g[v].edges.size(); i++) {
55+
Edge e = g[v].edges.get(i);
56+
if (d[t][e.b] > d[t][v] + e.t) {
57+
d[t][e.b] = d[t][v] + e.t;
58+
que.add(new Pair(d[t][e.b], e.b));
59+
}
60+
}
61+
}
62+
}
63+
int ans = 0;
64+
for (int i = 1; i <= n; i++)
65+
ans = Math.max(ans, d[i][x] + d[x][i]);
66+
System.out.println(ans);
67+
}
68+
public static void main(String[] args) {
69+
new Main().run();
70+
}
71+
}

0 commit comments

Comments
 (0)