Submission details
Task:Internet connection
Sender:david.meichel
Submission time:2018-09-15 15:59:57 +0300
Language:Java
Status:READY
Result:
Test results
testverdicttime
#10.26 sdetails
#20.24 sdetails
#30.25 sdetails
#40.26 sdetails
#50.26 sdetails
#6--details
#7--details
#8--details
#9--details
#10--details

Code

import java.util.*;
public class Task2
{
public static void main(String[] args)
{
	Scanner s = new Scanner(System.in);
	int n = s.nextInt();
	int m = s.nextInt();
	int[][] am = new int [n+1][n+1];
	int flow = 0;
	for(int i = 0; i < m; i++)
	{
		int x = s.nextInt();
		int y = s.nextInt();
		am[x][y] = s.nextInt();
	} 
	
	List<Integer> visited = new ArrayList<Integer>();
	List<Integer> track = new ArrayList<Integer>();
	/*List<Integer> path = find_path(am, 1, n, visited, track);
	System.out.println(path);
	flow = flow_of_path(am, path);
	System.out.println(flow);
	switch_edges(am, path, flow);
	printam(am);
	path = find_path(am, 1, n, visited, track);
	*/
	while(find_path(am, 1, n, visited, track).size() != 0)
	{
		visited = new ArrayList<Integer>();
		track = new ArrayList<Integer>();
		List<Integer> path = find_path(am, 1, n, visited, track);
		int tmp_flow = flow_of_path(am, path);
		flow += flow_of_path(am, path);
		am = switch_edges(am,path, tmp_flow);		
		printam(am);
		visited = new ArrayList<Integer>();
		track = new ArrayList<Integer>();
	}
	System.out.println(flow);
}

public static int[][] switch_edges(int [][] am, List<Integer> path, int flow)
{
	for (int i = 0; i < path.size()-1; i++)
        {
			int x = path.get(i);
			int y = path.get(i+1);
			am[x][y] = am[x][y] - flow;
			am[y][x] += flow;
	}
	return am;
}

public static int flow_of_path(int [][] am, List<Integer> path)
{
	int flow = Integer.MAX_VALUE;
	for (int i = 0; i < path.size()-1; i++)
        {
			int x = path.get(i);
			int y = path.get(i+1);
			//System.out.print("x: " + x + " y: " + y);		
			if(am[x][y] < flow)
				flow = am[x][y];
	}
	return flow;
}
public static List<Integer> find_path(int[][] am, int source, int target, List<Integer> visited, List<Integer> track)
{
	visited.add(source);
	for(int i = 1; i < am[source].length; i++)
	{	
		if(am[source][i] != 0 && !visited.contains(i))
		{
			if(i == target)
			{
				track.add(0, target);
				track.add(0, source);
				return track;
			}
			else
			{
				List<Integer> track_from_i = find_path(am, i, target, visited, track);
				if(track_from_i.contains(target))
				{
					track.add(0, source);
					return track;
				}
				
			}	
		}	
 	}
	return (new ArrayList<Integer>());  
	
}

public static void printam(int[][] am)
{
	for(int i = 0; i < am.length; i++)
	{
		for(int j = 0; j < am[i].length; j++)
			System.out.print(am[i][j]);
		System.out.println("");
	}
}
}

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
00000000000
0059014205000
0190490000000
0019026700000
0001902881006244
...
Truncated

Test 2

Verdict:

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

correct output
110

user output
00000000000
0084000000022
040563340003310
004095000001
00040520018095
...
Truncated

Test 3

Verdict:

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

correct output
29

user output
00000000000
00000030000
02602000420000
0026030026791326
00026059007300
...
Truncated

Test 4

Verdict:

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

correct output
95

user output
00000000000
0062000008000
02701500015000
00270490007100
000270002462040
...
Truncated

Test 5

Verdict:

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

correct output
156

user output
00000000000
00440000022081
090440930097820
0090420008300
0009011000700
...
Truncated

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)