题解:AtCoder AT_awc0101_a City with Power Shortage

【题目来源】

AtCoder:A - City with Power Shortage

【题目描述】

Takahashi manages the power grid of a region consisting of \(N\) cities. Each city is numbered from \(1\) to \(N\), and city \(i\) has a required power amount (demand) \(S_i\) needed to function properly.

There are \(M\) power transmission lines in this region, each connecting two cities bidirectionally. The \(j\)-th transmission line connects city \(U_j\) and city \(V_j\) and has a transmission capacity of \(W_j\). Each transmission line can supply power equal to its transmission capacity to each of the two cities it connects.

The power amount \(T_i\) that can be supplied to city \(i\) is defined as the sum of the transmission capacities of all transmission lines that have city \(i\) as an endpoint. That is, if the transmission capacities of the transmission lines with city \(i\) as an endpoint are \(W_{j_1}, W_{j_2}, \ldots, W_{j_k}\), then \(T_i = W_{j_1} + W_{j_2} + \cdots + W_{j_k}\). If there are no transmission lines with city \(i\) as an endpoint, then \(T_i = 0\).

City \(i\) is called a "power-deficient city" if \(T_i < S_i\) holds.

Find the number of power-deficient cities.

高橋管理着一个由 \(N\) 个城市组成的地区的电网。每个城市编号从 \(1\)\(N\),城市 \(i\) 正常运作所需的电量(需求量)为 \(S_i\)

该地区有 \(M\) 条输电线路,每条线路双向连接两个城市。第 \(j\) 条输电线路连接城市 \(U_j\) 和城市 \(V_j\),传输容量为 \(W_j\)。每条输电线路可以为其连接的两个城市分别提供等于其传输容量的电量。

能够供给城市 \(i\) 的电量 \(T_i\) 定义为所有以城市 \(i\) 为端点的输电线路的传输容量之和。也就是说,如果以城市 \(i\) 为端点的输电线路的传输容量为 \(W_{j_1}, W_{j_2}, \ldots, W_{j_k}\),则 \(T_i = W_{j_1} + W_{j_2} + \cdots + W_{j_k}\)。如果没有以城市 \(i\) 为端点的输电线路,则 \(T_i = 0\)

如果 \(T_i < S_i\) 成立,则称城市 \(i\) 为"电力不足城市"。

求电力不足城市的数量。

【输入】

\(N\) \(M\)
\(S_1\) \(S_2\) \(\ldots\) \(S_N\)
\(U_1\) \(V_1\) \(W_1\)
\(U_2\) \(V_2\) \(W_2\)
\(\vdots\)
\(U_M\) \(V_M\) \(W_M\)

  • The first line contains \(N\), the number of cities, and \(M\), the number of transmission lines, separated by a space.
  • The second line contains the demand of each city \(S_1, S_2, \ldots, S_N\), separated by spaces.
  • The following \(M\) lines provide information about the transmission lines.
  • The \((2 + j)\)-th line contains the cities \(U_j\) and \(V_j\) connected by the \(j\)-th transmission line, and the transmission capacity \(W_j\), separated by spaces.

【输出】

Output the number of power-deficient cities in one line.

【输入样例】

4 3
10 3 5 8
1 2 4
2 3 6
3 4 2

【输出样例】

2

【核心思想】

  1. 问题分析:给定 \(N\) 个城市,每个城市有电力需求量 \(S_i\),以及 \(M\) 条双向输电线路,每条线路连接城市 \(U_j\)\(V_j\) 且容量为 \(W_j\)。每条线路可为其两端城市分别提供 \(W_j\) 的电量。需要计算每个城市的实际可供电量 \(T_i\)(即所有与该城市相连的线路容量之和),并统计满足 \(T_i < S_i\) 的"电力不足城市"数量。

  2. 算法选择

    • 直接模拟(累加统计):每条线路的容量 \(W_j\) 同时贡献给其两个端点城市的可供电量,因此对每条线路执行两次累加操作即可
    • 离线处理:先读取所有数据,再统一计算和统计,无需复杂数据结构
  3. 关键步骤

    • 初始化:读取 \(N\)(城市数量)、\(M\)(线路数量),以及需求量数组 \(S[1..N]\)
    • 累加可供电量(处理 \(M\) 条线路):
      • 对于每条线路 \((U_j, V_j, W_j)\)
        • T[U_j] += W_j:线路为城市 \(U_j\) 提供 \(W_j\) 电量
        • T[V_j] += W_j:线路为城市 \(V_j\) 提供 \(W_j\) 电量
    • 统计电力不足城市
      • 遍历 \(i\)\(1\)\(N\)
        • T[i] < S[i],则 ans++
    • 输出答案 \(ans\)
  4. 时间/空间复杂度

    • 时间复杂度:\(O(N + M)\),读取需求量 \(O(N)\),处理 \(M\) 条线路 \(O(M)\),统计答案 \(O(N)\)
    • 空间复杂度:\(O(N)\),需求量数组 \(S[1..N]\) 和可供电量数组 \(T[1..N]\)
  5. 直接模拟的核心思想

    • 双向贡献:每条无向边 \((u, v)\) 的容量 \(W_j\) 同时计入两个端点的可供电量,无需图遍历,直接两次累加即可
    • 问题转化:将"图上的供需判断"转化为"数组的累加与比较",避开图论算法的复杂度
    • 离线统计:先完成所有累加操作,再统一与需求量比较,逻辑清晰且高效
    • 适用于边权直接贡献给端点、无需考虑路径或网络流的简单图统计问题

【算法标签】

模拟

【代码详解】

#include <bits/stdc++.h>
using namespace std;
#define int long long           // 将 int 定义为 long long,防止累加溢出
const int N = 200005;           // 最大城市数量
int n, m, ans;                  // n: 城市数, m: 输电线路数, ans: 电力不足城市数量
int s[N];                       // s[i]: 城市i的电力需求量
int t[N];                       // t[i]: 城市i的实际可供电量(所有相连线路容量之和)

signed main()                   // 使用 signed 替代 int,因为 #define int long long
{
    cin >> n >> m;              // 读入城市数和输电线路数

    // 读入每个城市的电力需求量
    for (int i = 1; i <= n; i++)
        cin >> s[i];

    // 读入m条输电线路,累加每个城市的可供电量
    while (m--)
    {
        int u, v, w;
        cin >> u >> v >> w;
        t[u] += w;              // 线路为城市u提供w电量
        t[v] += w;              // 线路为城市v提供w电量
    }

    // 统计电力不足的城市数量
    for (int i = 1; i <= n; i++)
    {
        if (t[i] < s[i])        // 可供电量 < 需求量,电力不足
            ans++;
    }

    // 输出电力不足城市的数量
    cout << ans << endl;

    return 0;
}

【运行结果】

4 3
10 3 5 8
1 2 4
2 3 6
3 4 2
2
posted @ 2026-06-30 20:45  团爸讲算法  阅读(7)  评论(0)    收藏  举报