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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
TEST_DATA = ''' 4 1 1000 1 2 3 4 2 500 1 2 3 70 2 3 ''' IS_PRINT_DETAIL = False PRINT_SPACE = 9 INF = int(1e10)
sink = 0 totalPeople = 0 edges = [] capacity = [] flow = [] cost = [] distance = [] parents = [] inQueue = []
balance = []
def init_data(totalPeople): arraySize = totalPeople * 2 + 2
global edges, capacity, flow, cost, distance, parents, inQueue edges = [[] for _ in range(arraySize)] capacity = [[0] * arraySize for _ in range(arraySize)] flow = [[0] * arraySize for _ in range(arraySize)] cost = [[0] * arraySize for _ in range(arraySize)] distance = [0] * arraySize parents = [0] * arraySize inQueue = [False] * arraySize
def process_input(inputData): global sink, edges, capacity, flow, cost, distance, parents, inQueue global totalPeople, balance
inputDataSplit = inputData.strip().split('\n') totalPeople = int(inputDataSplit[0]) sink = totalPeople * 2 + 1 balance = [0] * (totalPeople + 1) init_data(totalPeople)
for i in range(1, totalPeople + 1): capacity[0][i] = INF
for i in range(1, totalPeople + 1): for j in range(totalPeople + 1, totalPeople * 2 + 1): cost[i][j] = 1
pay = [0] * (totalPeople + 1) get = [0] * (totalPeople + 1) for line in inputDataSplit[1:]: lst = line.split(' ') p1 = int(lst[0]) money = int(lst[1]) splitMoney = int(money / (len(lst) - 2)) people = list(map(int, lst[2:]))
pay[p1] += money for p in people: get[p] += splitMoney
for i in range(len(balance)): balance[i] = pay[i] - get[i]
for i in range(1, totalPeople + 1): if balance[i] < 0: capacity[0][i] = -balance[i]
if IS_PRINT_DETAIL: print(f'{"pay:":>{PRINT_SPACE}}', pay) print(f'{"get:":>{PRINT_SPACE}}', get) print(f'{"balance:":>{PRINT_SPACE}}', balance) print()
for i in range(1, totalPeople + 1): edges[0].append(i)
if balance[i] >= 0: edges[i + totalPeople].append(sink) capacity[i + totalPeople][sink] = balance[i] continue
for j in range(totalPeople + 1, totalPeople * 2 + 1): if i + totalPeople != j: edges[i].append(j) capacity[i][j] = -balance[i]
if IS_PRINT_DETAIL: print(f'{"capacity:":>{PRINT_SPACE}}') for i in capacity: print(' ' * PRINT_SPACE, i)
print(f'{"edges:":>{PRINT_SPACE}}') for i in edges: print(' ' * PRINT_SPACE, i)
def SPFA(): global sink, edges, capacity, flow, cost, distance, parents, inQueue
distance = [INF] * len(distance) distance[0] = 0
inQueue = [False] * len(inQueue)
que = [0] inQueue[0] = True
while que: u = que.pop(0) inQueue[u] = False
for v in edges[u]: if capacity[u][v] > flow[u][v] and distance[u] + cost[u][v] < distance[v]: distance[v] = distance[u] + cost[u][v] parents[v] = u if not inQueue[v]: que.append(v) inQueue[v] = True if flow[v][u] > 0 and distance[u] + (-cost[v][u] < distance[v]): distance[v] = distance[u] + (-cost[v][u]) parents[v] = u if not inQueue[v]: que.append(v) inQueue[v] = True
return distance[sink] != INF
def augment(u, v, bottleNeck): global parents, capacity, flow
if v == 0: return bottleNeck bottleNeck = augment(parents[u], u, min(capacity[u][v] - flow[u][v], bottleNeck)) flow[u][v] += bottleNeck flow[v][u] -= bottleNeck return bottleNeck
def MCMF(): global edges, capacity, flow, cost, distance, parents, inQueue, sink global totalPeople, balance
times = 0 while SPFA(): augment(parents[sink], sink, INF) times += 1
for i, bal in enumerate(balance[1:]): pay, get = 0, 0 if bal < 0: pay = -bal elif bal > 0: get = bal print(f'{i + 1:2} 號應付 {pay:4} 元,應得 {get:4} 元')
print('\n最少轉帳次數:', times) for i in range(totalPeople + 1, totalPeople * 2 + 1): for j in range(1, totalPeople + 1): if flow[i][j] < 0: print(f'{j:2} 號需給 {i - totalPeople:2} 號 {-flow[i][j]:4} 元')
def main(): process_input(TEST_DATA) MCMF()
if __name__ == '__main__': main()
|