結果
| 問題 |
No.468 役に立つ競技プログラミング実践編
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2023-12-14 17:44:27 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 948 ms / 2,000 ms |
| コード長 | 3,473 bytes |
| コンパイル時間 | 3,051 ms |
| コンパイル使用メモリ | 91,680 KB |
| 実行使用メモリ | 90,088 KB |
| 最終ジャッジ日時 | 2024-09-27 05:44:40 |
| 合計ジャッジ時間 | 18,309 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 31 |
| other | AC * 6 |
ソースコード
import java.io.*;
import java.util.*;
import java.util.stream.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner();
int n = sc.nextInt();
int m = sc.nextInt();
ArrayList<ArrayList<Path>> graph = new ArrayList<>();
ArrayList<ArrayList<Path>> reverse = new ArrayList<>();
for (int i = 0; i < n; i++) {
graph.add(new ArrayList<>());
reverse.add(new ArrayList<>());
}
int[] counts = new int[n];
int[] rCounts = new int[n];
for (int i = 0; i < m; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
counts[b]++;
rCounts[a]++;
graph.get(a).add(new Path(b, c));
reverse.get(b).add(new Path(a, c));
}
ArrayDeque<Integer> idxes = new ArrayDeque<>();
idxes.add(0);
int[] value = new int[n];
while (idxes.size() > 0) {
int x = idxes.poll();
for (Path y : graph.get(x)) {
value[y.idx] = Math.max(value[y.idx], value[x] + y.value);
counts[y.idx]--;
if(counts[y.idx] == 0) {
idxes.add(y.idx);
}
}
}
idxes.add(n - 1);
int[] rValue = new int[n];
Arrays.fill(rValue, Integer.MAX_VALUE);
rValue[n - 1] = value[n - 1];
while (idxes.size() > 0) {
int x = idxes.poll();
for (Path y : reverse.get(x)) {
rValue[y.idx] = Math.min(rValue[y.idx], rValue[x] - y.value);
rCounts[y.idx]--;
if(rCounts[y.idx] == 0) {
idxes.add(y.idx);
}
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
if (rValue[i] > value[i]) {
ans++;
}
}
System.out.print(value[n - 1]);
System.out.println(" " + ans + "/" + n);
}
static class Path {
int idx;
int value;
public Path(int idx, int value) {
this.idx = idx;
this.value = value;
}
}
}
class Utilities {
static String arrayToLineString(Object[] arr) {
return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
}
static String arrayToLineString(int[] arr) {
return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
}
}
class Scanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
StringBuilder sb = new StringBuilder();
public Scanner() throws Exception {
}
public int nextInt() throws Exception {
return Integer.parseInt(next());
}
public long nextLong() throws Exception {
return Long.parseLong(next());
}
public double nextDouble() throws Exception {
return Double.parseDouble(next());
}
public int[] nextIntArray() throws Exception {
return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
}
public String next() throws Exception {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
}
tenten