Submission details
Task:Internet connection
Sender:pierog
Submission time:2020-09-19 15:19:40 +0300
Language:Java
Status:READY
Result:
Test results
testverdicttime
#10.09 sdetails
#20.09 sdetails
#30.09 sdetails
#40.09 sdetails
#50.09 sdetails
#6--details
#7--details
#8--details
#9--details
#10--details
#110.09 sdetails
#120.09 sdetails
#130.09 sdetails
#140.12 sdetails
#15ACCEPTED0.09 sdetails
#160.09 sdetails
#170.09 sdetails
#18ACCEPTED0.10 sdetails
#19ACCEPTED0.10 sdetails
#200.09 sdetails
#21--details
#22--details

Code

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static java.util.stream.Collectors.toList;

public class InternetConnection {

    public static class Node {
        int target;
        long speed;

        public Node(int target, long speed) {
            this.target = target;
            this.speed = speed;
        }
    }

    public static class Input {
        int n;
        int m;

        Map<Integer, List<Node>> nodes;

        public Input(int n, int m, Map<Integer, List<Node>> nodes) {
            this.n = n;
            this.m = m;
            this.nodes = nodes;
        }
    }

    private static Input getInput() {
        try (BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) )) {
            var line = reader.readLine();
            var split = line.split(" ");
            int n = Integer.parseInt(split[0]);
            int m = Integer.parseInt(split[1]);
            Map<Integer, List<Node>> nodes = new HashMap<>();
            for (int i = 0; i < m; i++) {
                line = reader.readLine();
                split = line.split(" ");
                int a = Integer.parseInt(split[0]);
                int b = Integer.parseInt(split[1]);
                long c = Long.parseLong(split[2]);
                if (!nodes.containsKey(a)) {
                    nodes.put(a, new ArrayList<>());
                }
                nodes.get(a).add(new Node(b, c));
            }
            return new Input(n, m, nodes);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        var input = getInput();

        System.out.println(new InternetConnection(input).find());
    }

    private final Input input;

    public InternetConnection(Input input) {
        this.input = input;
    }

    public long find() {
        var paths = findAllPaths();
        long sum = 0L;

        for (var path : paths) {
            var minSpeed = findMinSpeed(path);
            sum += minSpeed;
            for (var node : path) {
                node.speed -= minSpeed;
            }
        }

        return sum;
    }

    private long getSpeed(Node from, Node to) {
        for (var node : this.input.nodes.get(from.target)) {
            if (node.target == to.target) {
                return node.speed;
            }
        }
        return -1;
    }

    private long findMinSpeed(List<Node> path) {
        long min = Long.MAX_VALUE;

        for (int i = 0; i < path.size() - 1; i++) {
            var speed = getSpeed(path.get(i), path.get(i + 1));
            if (speed < min) {
                min = speed;
            }
        }

        return min;
    }

    private Set<List<Node>> findAllPaths() {
        var start = this.input.nodes.get(1);
        Set<List<Node>> results = new HashSet<>();
        start.forEach(node -> this.findPath(new ArrayList<>(List.of(node)), results));
        return results;
    }

    private void findPath(List<Node> currentPath, Set<List<Node>> results) {
        var last = currentPath.get(currentPath.size() - 1);
        if (last.target == this.input.n) {
            results.add(currentPath);
            return;
        }

        for (Node n : this.input.nodes.get(last.target)) {
            var newPath = new ArrayList<>(currentPath);
            newPath.add(n);
            findPath(newPath, results);
        }
    }
}

Test details

Test 1

Verdict:

input
10 20
5 6 19
4 5 47
3 5 7
4 9 62
...

correct output
73

user output
137

Test 2

Verdict:

input
10 20
2 4 63
7 9 54
6 7 16
2 3 9
...

correct output
110

user output
-9223372036854775660

Test 3

Verdict:

input
10 20
5 6 90
2 3 46
7 8 80
6 7 60
...

correct output
29

user output
156

Test 4

Verdict:

input
10 20
3 4 76
5 7 8
3 8 71
4 7 24
...

correct output
95

user output
80

Test 5

Verdict:

input
10 20
1 8 22
6 7 40
4 5 20
8 10 77
...

correct output
156

user output
-9223372036854775713

Test 6

Verdict:

input
100 1000
63 85 864540192
22 91 974117435
64 66 953124912
85 88 6080960
...

correct output
4397669179

user output
(empty)

Test 7

Verdict:

input
100 1000
36 93 760720873
12 75 175717522
78 79 340128710
80 83 181753465
...

correct output
5298558023

user output
(empty)

Test 8

Verdict:

input
100 1000
20 60 909693891
55 91 570199535
21 41 118646902
37 82 824735480
...

correct output
5466229311

user output
(empty)

Test 9

Verdict:

input
100 1000
26 44 753330451
62 67 821574279
70 95 219303983
7 44 980013084
...

correct output
4893925638

user output
(empty)

Test 10

Verdict:

input
100 1000
15 89 501388091
50 71 396801720
15 92 324349822
29 85 184420157
...

correct output
6956499595

user output
(empty)

Test 11

Verdict:

input
2 1
1 2 1

correct output
1

user output
9223372036854775807

Test 12

Verdict:

input
2 1
2 1 1

correct output
0

user output
(empty)

Error:
Exception in thread "main" java.lang.NullPointerException
	at InternetConnection.findAllPa...

Test 13

Verdict:

input
2 2
1 2 1
2 1 1

correct output
1

user output
9223372036854775807

Test 14

Verdict:

input
100 1000
1 2 539540023
2 3 244306651
3 4 253259012
3 5 630461598
...

correct output
0

user output
(empty)

Error:
Exception in thread "main" java.lang.NullPointerException
	at InternetConnection.findPath(...

Test 15

Verdict: ACCEPTED

input
4 5
1 2 2
1 3 5
2 4 3
3 2 2
...

correct output
4

user output
4

Test 16

Verdict:

input
2 0

correct output
0

user output
(empty)

Error:
Exception in thread "main" java.lang.NullPointerException
	at InternetConnection.findAllPa...

Test 17

Verdict:

input
100 0

correct output
0

user output
(empty)

Error:
Exception in thread "main" java.lang.NullPointerException
	at InternetConnection.findAllPa...

Test 18

Verdict: ACCEPTED

input
100 196
1 2 1000000000
2 100 1000000000
1 3 1000000000
3 100 1000000000
...

correct output
98000000000

user output
98000000000

Test 19

Verdict: ACCEPTED

input
100 99
1 2 1000000000
2 3 1000000000
3 4 1000000000
4 5 1000000000
...

correct output
1000000000

user output
1000000000

Test 20

Verdict:

input
2 2
2 1 1
1 2 1

correct output
1

user output
9223372036854775807

Test 21

Verdict:

input
4 6
1 2 1000000000
1 3 1000000000
2 3 1
2 4 1000000000
...

correct output
2000000000

user output
(empty)

Test 22

Verdict:

input
4 6
1 2 1000000000
1 3 1000000000
2 4 1000000000
2 3 1
...

correct output
2000000000

user output
(empty)