Problem solution · Python

ABC416 E — Development

ABC416 E — Development: a Python solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to KATO-Hiro AtCoder Solutions.

Technique
Direct simulation
Source
KATO-Hiro AtCoder Solutions
Length
100 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For ABC416 E — Development, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 100 lines of Python from the credited upstream file abc416_e.py.
  • The implementation visibly relies on sequence storage, ordered lookup.
  • No explicit loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from KATO-Hiro AtCoder Solutions by KATO-Hiro and is used under the CC0-1.0 licence.

Full codeABC416 E — Development · PythonPython
Use this to learn the idea, then write your own version.
# -*- coding: utf-8 -*-  def warshall_floyd(dist):    """    Args:        dist (list[list[int]]): A 2D matrix where dist[i][j] represents the distance             from vertex i to vertex j in a graph. If there is no direct edge between             i and j, dist[i][j] should be set to a very large value (e.g., infinity).     Returns:        list[list[int]]: The updated 2D matrix where dist[i][j] contains the shortest             distance from vertex i to vertex j. The input matrix is modified in place.     Landau notation: O(n ** 3).    """     v_count = len(dist[0])     for k in range(v_count):        for i in range(v_count):            for j in range(v_count):                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])     return dist  def main():    import sys     input = sys.stdin.readline     n, m = map(int, input().split())    inf = 10**18    graph = [[inf for _ in range(n + 1)] for _ in range(n + 1)]     for i in range(n + 1):        graph[i][i] = 0     for _ in range(m):        ai, bi, ci = map(int, input().split())        ai -= 1        bi -= 1         graph[ai][bi] = min(graph[ai][bi], ci)        graph[bi][ai] = min(graph[bi][ai], ci)     k, t = map(int, input().split())    d = list(map(int, input().split()))     # 超頂点を導入    # 空港diから超頂点nへの辺をコストt、nから別の空港への辺をコスト0とする    for di in d:        di -= 1        graph[di][n] = t        graph[n][di] = 0     dist = warshall_floyd(graph)     q = int(input())     for _ in range(q):        query = list(map(int, input().split()))         if query[0] == 1:            xi, yi, ti = query[1:]            xi -= 1            yi -= 1             for i in range(n + 1):                for j in range(n + 1):                    dist1 = dist[i][xi] + ti + dist[yi][j]                    dist2 = dist[i][yi] + ti + dist[xi][j]                    dist[i][j] = min(dist[i][j], dist1, dist2)        elif query[0] == 2:            xi = query[1] - 1             for i in range(n + 1):                for j in range(n + 1):                    dist1 = dist[i][xi] + t + dist[n][j]                    dist2 = dist[i][n] + 0 + dist[xi][j]                    dist[i][j] = min(dist[i][j], dist1, dist2)        else:            ans = 0             for i in range(n):                for j in range(n):                    if i == j:                        continue                    if dist[i][j] == inf:                        continue                     ans += dist[i][j]             print(ans)  if __name__ == "__main__":    main() 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗