Official

A - 電力不足の都市 / City with Power Shortage Editorial by admin Editorial for printing

Qwen3-Coder-480B

Overview

Given the required power demand for each city and the power that can be supplied through transmission lines, find the number of cities where the power supply is insufficient.

Analysis

In this problem, for each city, we need to calculate the total transmission capacity of all transmission lines connected to that city (the available supply power \(T_i\)), and determine whether it falls below the demand \(S_i\).

A straightforward approach is to iterate over each transmission line and add its capacity to both endpoint cities. Even if we simply add to an array each time, since the number of cities \(N\) and the number of transmission lines \(M\) are at most \(2 \times 10^5\), we need at most \(2 \times 10^5\) addition operations in total. This can be processed sufficiently fast, but performing list operations for each addition may cause slowdowns. Especially in languages where list operations are expensive, such as Python, optimization including standard input processing is necessary.

In this problem, we can cumulatively compute the total power supplied to each city while reading the information of each transmission line. That is, we simply add the transmission capacity to the array values corresponding to the two endpoint cities of each transmission line. After that, comparing the available supply power \(T_i\) with the demand \(S_i\) for each city gives us the answer.

Algorithm

  1. Read the demand \(S_i\) for each city.
  2. For each transmission line, add the transmission capacity \(W_j\) to both connected cities \(U_j\) and \(V_j\) (add to \(T_{U_j}\) and \(T_{V_j}\)).
  3. For each city \(i\), increment the count if \(T_i < S_i\).
  4. Output the count.

Complexity

  • Time complexity: \(O(N + M)\)
  • Space complexity: \(O(N + M)\)

※ Since updating both endpoint cities for each transmission line can be done in \(O(1)\), the total is \(O(M)\). Scanning all cities at the end takes \(O(N)\).

Implementation Notes

  • Since the input is very large, use sys.stdin.read for fast input reading.

  • City numbers are given as 1-indexed, so convert them to 0-indexed internally (\(U_j - 1\), \(V_j - 1\)).

  • The array \(T\) holding the available supply power should be initialized to zero at the start.

    Source Code

import sys
input = sys.stdin.read

def main():
    data = input().split()
    idx = 0
    
    N = int(data[idx])
    idx += 1
    M = int(data[idx])
    idx += 1
    
    S = [int(data[idx + i]) for i in range(N)]
    idx += N
    
    T = [0] * N
    for _ in range(M):
        U = int(data[idx]) - 1
        idx += 1
        V = int(data[idx]) - 1
        idx += 1
        W = int(data[idx])
        idx += 1
        T[U] += W
        T[V] += W
    
    count = 0
    for i in range(N):
        if T[i] < S[i]:
            count += 1
    
    print(count)

if __name__ == "__main__":
    main()

This editorial was generated by qwen3-coder-480b.

posted:
last update: