More actions
imported>skywave No edit summary |
imported>skywave No edit summary |
||
| Line 1: | Line 1: | ||
[http://183.106.113.109/30stair/virtual/virtual.php] | e[http://183.106.113.109/30stair/virtual/virtual.php] | ||
== 조영준 == | |||
=== 설명 === | |||
* 기본적인 아이디어는 '''노드를 하나씩 제거'''하는 것 | |||
** 루프가 없고 한 방향으로만 흘러가기 때문에 가능 | |||
* 들어오는 edge가 없는 node를 제거하고 그 node에서 나가는 edge와 연결된 node들의 최대 흐를 수 있는 양을 확인 후 변경 | |||
** 현재 node에서 흐를 수 있는 양보다 크면 그 양을 갱신하고 parent를 이전 노드로 지정 | |||
** 같아도 변경하지 않음. | |||
** 최대로 흐를 수 있는 양이 같더라도 먼저 계산한 것이 더 최단거리임 | |||
* path는 도착지점부터 parent 노드를 쫒아가면 됨. | |||
=== 겪었던 문제 === | |||
* 처음에는 하나씩 지워나간다고 생각해서 검사할 node를 int 형으로 기록했으나, 가장 초기에는 지워야 하는 node가 시작점 하나만 있는 것이 아닌 것을 알게 되어 stack으로 변경. | |||
=== 코드 === | |||
* Python2 | |||
* [http://183.106.113.109/displaysrc.php?id=1178830] | |||
points = int(raw_input()) | |||
point_current, point_end = [int(i) for i in raw_input().split()] | |||
graph = [[-1] * (points + 1) for i in range(points + 1)] | |||
point_in = [0] * (points + 1) | |||
point_parent = [0] * (points + 1) | |||
point_parent[point_current] = -1 | |||
point_flow = [0] * (points + 1) | |||
point_flow[point_current] = 99999 | |||
while True: | |||
try: | |||
input_data = [int(i) for i in raw_input().split()] | |||
if not input_data: | |||
break | |||
graph[input_data[0]][input_data[1]] = input_data[2] | |||
point_in[input_data[1]] += 1 | |||
except EOFError: | |||
break | |||
stack = [point_current] | |||
for i in range(1, points + 1): | |||
if point_in[i] == 0 and i != point_current: | |||
stack.append(i) | |||
while stack: | |||
point_current = stack.pop() | |||
for i in range(points + 1): | |||
flow = min(graph[point_current][i], point_flow[point_current]) | |||
if flow != -1: | |||
graph[point_current][i] = -1 | |||
if flow > point_flow[i]: | |||
point_flow[i] = flow | |||
point_parent[i] = point_current | |||
point_in[i] -= 1 | |||
if point_in[i] == 0: | |||
stack.append(i) | |||
print point_flow[point_end] | |||
path = [] | |||
point_current = point_end | |||
while point_current != -1: | |||
path.append(point_current) | |||
point_current = point_parent[point_current] | |||
path.reverse() | |||
for i in path: | |||
print i, | |||
Revision as of 10:08, 16 February 2014
e[1]
조영준
설명
- 기본적인 아이디어는 노드를 하나씩 제거하는 것
- 루프가 없고 한 방향으로만 흘러가기 때문에 가능
- 들어오는 edge가 없는 node를 제거하고 그 node에서 나가는 edge와 연결된 node들의 최대 흐를 수 있는 양을 확인 후 변경
- 현재 node에서 흐를 수 있는 양보다 크면 그 양을 갱신하고 parent를 이전 노드로 지정
- 같아도 변경하지 않음.
- 최대로 흐를 수 있는 양이 같더라도 먼저 계산한 것이 더 최단거리임
- path는 도착지점부터 parent 노드를 쫒아가면 됨.
겪었던 문제
- 처음에는 하나씩 지워나간다고 생각해서 검사할 node를 int 형으로 기록했으나, 가장 초기에는 지워야 하는 node가 시작점 하나만 있는 것이 아닌 것을 알게 되어 stack으로 변경.
코드
- Python2
- [2]
points = int(raw_input()) point_current, point_end = [int(i) for i in raw_input().split()] graph = [[-1] * (points + 1) for i in range(points + 1)] point_in = [0] * (points + 1) point_parent = [0] * (points + 1) point_parent[point_current] = -1 point_flow = [0] * (points + 1) point_flow[point_current] = 99999 while True: try: input_data = [int(i) for i in raw_input().split()] if not input_data: break graph[input_data[0]][input_data[1]] = input_data[2] point_in[input_data[1]] += 1 except EOFError: break stack = [point_current] for i in range(1, points + 1): if point_in[i] == 0 and i != point_current: stack.append(i) while stack: point_current = stack.pop() for i in range(points + 1): flow = min(graph[point_current][i], point_flow[point_current]) if flow != -1: graph[point_current][i] = -1 if flow > point_flow[i]: point_flow[i] = flow point_parent[i] = point_current point_in[i] -= 1 if point_in[i] == 0: stack.append(i) print point_flow[point_end] path = [] point_current = point_end while point_current != -1: path.append(point_current) point_current = point_parent[point_current] path.reverse() for i in path: print i,