Submission details
Task:Internet connection
Sender:david.meichel
Submission time:2018-09-18 18:44:32 +0300
Language:Java
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.24 sdetails
#2ACCEPTED0.23 sdetails
#3ACCEPTED0.23 sdetails
#4ACCEPTED0.23 sdetails
#5ACCEPTED0.22 sdetails
#6ACCEPTED0.35 sdetails
#7ACCEPTED0.47 sdetails
#8ACCEPTED0.31 sdetails
#9ACCEPTED0.31 sdetails
#10ACCEPTED0.32 sdetails

Code

import java.util.*;
public class Task2_new
{
	static long tmp_flow = 0;
	static ArrayList<ArrayList<Integer>> neighbors;
public static void main(String[] args)
{		
	long startTime = System.nanoTime();
	Scanner s = new Scanner(System.in);
	int n = s.nextInt();
	int m = s.nextInt();
	long flow = 0;
	initialize_neighbors(n);
	long[][] am = read_input(n, m, s);
	int [] visited = new int[n+1];
	List<Integer> track = new ArrayList<Integer>();
	List<Integer> path = find_path(am, 1, n, visited, track);
	visited = new int [n+1];
	track = new ArrayList<Integer>();
	while(path != null && path.size() != 0)
	{
		flow += tmp_flow;
		am = switch_edges(am,path, tmp_flow);	
		tmp_flow = 0;		
		path = find_path(am, 1, n, visited, track);
		visited = new int[n+1];
		track = new ArrayList<Integer>();
	}
	System.out.println(flow);
	long endTime   = System.nanoTime();	
	long totalTime = endTime - startTime;
	double seconds = (double)totalTime / 1000000000.0;
	//System.out.println("time: " + seconds);
}

public  static void initialize_neighbors(int n)
{
	neighbors = new ArrayList<ArrayList<Integer>>();
	for(int i = 0; i< n; i++)
	{
		neighbors.add(new ArrayList<Integer>());
	}
}

public static long[][] read_input(int n, int m, Scanner s)
{
	long[][] am = new long [n+1][n+1];
	int x;
	int y;
	for(int i = 0; i < m; i++)
	{
		x = s.nextInt();
		y = s.nextInt();
		am[x][y] = s.nextInt();
		neighbors.get(x-1).add(y);
		neighbors.get(y-1).add(x);
	} 
	return am;
}

public static long[][] switch_edges(long [][] am, List<Integer> path, long 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 long flow_of_path(long[][] am, List<Integer> path)
{
	long flow = Long.MAX_VALUE;
	for (int i = 0; i < path.size()-1; i++)
        {
			int x = path.get(i);
			int y = path.get(i+1);		
			if(am[x][y] < flow)
				flow = am[x][y];
	}
	return flow;
}
public static List<Integer> find_path(long[][] am, int source, int target, int[] visited, List<Integer> track)
{
	visited[source] = 1;
	for(int i = 0; i < neighbors.get(source-1).size(); i++)
	{        	 
		int j = neighbors.get(source-1).get(i);	
		if(am[source][j] != 0 && visited[j] == 0)
		{
			if(j == target)
			{
				track.add(0, target);
				track.add(0, source);
				tmp_flow = am[source][target];
				return track;
			}
			else
			{
				List<Integer> track_from_i = find_path(am, j, target, visited, track);
				if(track_from_i != null && track_from_i.size() > 0)
				{
					track.add(0, source);
					new_flow(am[source][track.get(1)]);
					return track;
				}
				
			}	
		}	
 	}
	return null;  
	
}

public static void new_flow(long new_flow)
{	
	if(new_flow < tmp_flow)
		tmp_flow = new_flow;
}

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: ACCEPTED

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

correct output
73

user output
73

Test 2

Verdict: ACCEPTED

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

correct output
110

user output
110

Test 3

Verdict: ACCEPTED

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

correct output
29

user output
29

Test 4

Verdict: ACCEPTED

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

correct output
95

user output
95

Test 5

Verdict: ACCEPTED

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

correct output
156

user output
156

Test 6

Verdict: ACCEPTED

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

correct output
4397669179

user output
4397669179

Test 7

Verdict: ACCEPTED

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

correct output
5298558023

user output
5298558023

Test 8

Verdict: ACCEPTED

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

correct output
5466229311

user output
5466229311

Test 9

Verdict: ACCEPTED

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

correct output
4893925638

user output
4893925638

Test 10

Verdict: ACCEPTED

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

correct output
6956499595

user output
6956499595