-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPossibleWinner.java
More file actions
102 lines (85 loc) · 2.77 KB
/
PossibleWinner.java
File metadata and controls
102 lines (85 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import java.util.*;
public class PossibleWinner {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//read input
int numTeam = in.nextInt();
int[] currentWins = new int[numTeam];
int[][] remainGames = new int[numTeam][numTeam];
for (int i = 0; i < numTeam; i++) {
currentWins[i] = in.nextInt();
}
for (int i = 0; i < numTeam; i++) {
for (int j = 0; j < numTeam; j++) {
remainGames[i][j] = in.nextInt();
}
}
if (isPossibleWinner(currentWins, remainGames))
System.out.println("yes");
else
System.out.println("no");
}
private static boolean isPossibleWinner(int[] currentWins,
int[][] remainGames) {
final int INF = Integer.MAX_VALUE;
int numTeam = currentWins.length;
int candidateInd = 0;
int maxWins = 0;
//calculate maximum wins of first team
for (int i = 0; i < numTeam; i++)
maxWins += remainGames[candidateInd][i];
maxWins += currentWins[candidateInd];
if (!preCheck(maxWins, currentWins))
return false;
//if pass pre-check
int[] enemyMaxWins = new int[numTeam];
for (int i = 0; i < numTeam; i++) {
if (i != candidateInd)
enemyMaxWins[i] = maxWins - currentWins[i];
else
enemyMaxWins[i] = -1; //if is processing itself
}
//calculate numVertices
int numMatches = (numTeam - 1) * (numTeam - 2) / 2;
//1 source, 1 sink, numEnemy choose 2, numEnemy
int numV = 2 + numTeam - 1 + numMatches;
int minGames = 0;
//construct edges
ArrayList<FlowEdge> edges = new ArrayList<FlowEdge>();
int matchTrack = 1;
for (int i = 0; i < numTeam; i++) {
if (i != candidateInd) {
for (int j = i + 1; j < numTeam; j++) {
int capacity = remainGames[i][j];
minGames += capacity;
FlowEdge fe = new FlowEdge(0, matchTrack, capacity);
edges.add(fe);
FlowEdge fe1 = new FlowEdge(matchTrack, numMatches + i, INF);
FlowEdge fe2 = new FlowEdge(matchTrack, numMatches + j, INF);
edges.add(fe1);
edges.add(fe2);
matchTrack++;
}
FlowEdge fe3 = new FlowEdge(numMatches + i, numV - 1, enemyMaxWins[i]);
edges.add(fe3);
}
}
//construct graph
FlowNetwork graph = new FlowNetwork(numV);
for (FlowEdge e : edges)
graph.addEdge(e);
//run MaxFLow
FordFulkerson maxFlow = new FordFulkerson(graph, 0, numV - 1);
double maxFlowValue = maxFlow.value();
if (maxFlowValue < minGames)
return false;
return true;
}
private static boolean preCheck(int maxWins, int[] currentWins) {
for (int i = 0; i < currentWins.length; i++) {
if (currentWins[i] > maxWins)
return false;
}
return true;
}
}