C. Rumor

Codeforces
IDCF893C
Time2000ms
Memory256MB
Difficulty
dfs and similargraphsgreedy
English · Original
Chinese · Translation
Formal · Original
Vova promised himself that he would never play computer games... But recently Firestorm — a well-known game developing company — published their newest game, World of Farcraft, and it became really popular. Of course, Vova started playing it. Now he tries to solve a quest. The task is to come to a settlement named Overcity and spread a rumor in it. Vova knows that there are _n_ characters in Overcity. Some characters are friends to each other, and they share information they got. Also Vova knows that he can bribe each character so he or she starts spreading the rumor; _i_\-th character wants _c__i_ gold in exchange for spreading the rumor. When a character hears the rumor, he tells it to all his friends, and they start spreading the rumor to their friends (for free), and so on. The quest is finished when all _n_ characters know the rumor. What is the minimum amount of gold Vova needs to spend in order to finish the quest? Take a look at the notes if you think you haven't understood the problem completely. ## Input The first line contains two integer numbers _n_ and _m_ (1 ≤ _n_ ≤ 105, 0 ≤ _m_ ≤ 105) — the number of characters in Overcity and the number of pairs of friends. The second line contains _n_ integer numbers _c__i_ (0 ≤ _c__i_ ≤ 109) — the amount of gold _i_\-th character asks to start spreading the rumor. Then _m_ lines follow, each containing a pair of numbers (_x__i_, _y__i_) which represent that characters _x__i_ and _y__i_ are friends (1 ≤ _x__i_, _y__i_ ≤ _n_, _x__i_ ≠ _y__i_). It is guaranteed that each pair is listed at most once. ## Output Print one number — the minimum amount of gold Vova has to spend in order to finish the quest. [samples] ## Note In the first example the best decision is to bribe the first character (he will spread the rumor to fourth character, and the fourth one will spread it to fifth). Also Vova has to bribe the second and the third characters, so they know the rumor. In the second example Vova has to bribe everyone. In the third example the optimal decision is to bribe the first, the third, the fifth, the seventh and the ninth characters.
Vova 向自己保证,他再也不会玩电脑游戏了……但最近,知名游戏开发公司 Firestorm 发布了他们的最新游戏《World of Farcraft》,这款游戏变得非常流行。当然,Vova 开始玩了起来。 现在他试图完成一个任务:前往名为 Overcity 的定居点,并在其中传播一则谣言。 Vova 知道 Overcity 中有 #cf_span[n] 个角色。有些角色彼此是朋友,他们会分享自己得到的信息。同时,Vova 知道他可以贿赂每个角色,使其开始传播谣言;第 #cf_span[i] 个角色要求 #cf_span[ci] 金币作为交换条件。当一个角色听到谣言后,他会告诉所有朋友,朋友们又会免费地将谣言传播给他们的朋友,依此类推。 当所有 #cf_span[n] 个角色都得知谣言时,任务完成。Vova 最少需要花费多少金币才能完成这个任务? 如果你觉得自己还没有完全理解题目,请参考注释部分。 第一行包含两个整数 #cf_span[n] 和 #cf_span[m](#cf_span[1 ≤ n ≤ 10^5, 0 ≤ m ≤ 10^5])——分别表示 Overcity 中的角色数量和朋友对的数量。 第二行包含 #cf_span[n] 个整数 #cf_span[ci](#cf_span[0 ≤ ci ≤ 10^9])——表示第 #cf_span[i] 个角色为开始传播谣言所要求的金币数量。 接下来 #cf_span[m] 行,每行包含一对数字 (#cf_span[xi, yi]),表示角色 #cf_span[xi] 和 #cf_span[yi] 是朋友(#cf_span[1 ≤ xi, yi ≤ n], #cf_span[xi ≠ yi])。保证每对朋友关系最多只出现一次。 请输出一个数字——Vova 为完成任务所需的最少金币数量。 在第一个示例中,最佳策略是贿赂第一个角色(他会将谣言传播给第四个角色,第四个角色再传播给第五个)。此外,Vova 还必须贿赂第二个和第三个角色,使他们得知谣言。 在第二个示例中,Vova 必须贿赂所有人。 在第三个示例中,最优策略是贿赂第一个、第三个、第五个、第七个和第九个角色。 ## Input 第一行包含两个整数 #cf_span[n] 和 #cf_span[m](#cf_span[1 ≤ n ≤ 10^5, 0 ≤ m ≤ 10^5])——分别表示 Overcity 中的角色数量和朋友对的数量。第二行包含 #cf_span[n] 个整数 #cf_span[ci](#cf_span[0 ≤ ci ≤ 10^9])——表示第 #cf_span[i] 个角色为开始传播谣言所要求的金币数量。接下来 #cf_span[m] 行,每行包含一对数字 (#cf_span[xi, yi]),表示角色 #cf_span[xi] 和 #cf_span[yi] 是朋友(#cf_span[1 ≤ xi, yi ≤ n], #cf_span[xi ≠ yi])。保证每对朋友关系最多只出现一次。 ## Output 请输出一个数字——Vova 为完成任务所需的最少金币数量。 [samples] ## Note 在第一个示例中,最佳策略是贿赂第一个角色(他会将谣言传播给第四个角色,第四个角色再传播给第五个)。此外,Vova 还必须贿赂第二个和第三个角色,使他们得知谣言。 在第二个示例中,Vova 必须贿赂所有人。 在第三个示例中,最优策略是贿赂第一个、第三个、第五个、第七个和第九个角色。
**Definitions** Let $ n \in \mathbb{Z}^+ $ be the number of characters, $ m \in \mathbb{Z}_{\geq 0} $ the number of friendship pairs. Let $ c = (c_1, c_2, \dots, c_n) \in \mathbb{R}_{\geq 0}^n $ be the cost vector, where $ c_i $ is the gold required to bribe character $ i $. Let $ G = (V, E) $ be an undirected graph with $ V = \{1, 2, \dots, n\} $ and $ E \subseteq \binom{V}{2} $, $ |E| = m $, representing friendships. **Constraints** 1. $ 1 \leq n \leq 10^5 $ 2. $ 0 \leq m \leq 10^5 $ 3. $ 0 \leq c_i \leq 10^9 $ for all $ i \in V $ 4. Each edge $ \{x_i, y_i\} \in E $ is unique and $ x_i \ne y_i $ **Objective** Find the minimum total cost to ensure the rumor spreads to all characters, where: - Bribing a character $ i $ costs $ c_i $ and causes the rumor to propagate along all edges in the connected component containing $ i $. - In each connected component, only one character needs to be bribed (the one with minimal cost in that component). Let $ \mathcal{C} = \{C_1, C_2, \dots, C_k\} $ be the set of connected components of $ G $. For each component $ C_j $, let $ \min_{i \in C_j} c_i $ be the minimum bribe cost in $ C_j $. Then the minimum gold required is: $$ \sum_{j=1}^{k} \min_{i \in C_j} c_i $$
Samples
Input #1
5 2
2 5 3 4 8
1 4
4 5
Output #1
10
Input #2
10 0
1 2 3 4 5 6 7 8 9 10
Output #2
55
Input #3
10 5
1 6 2 7 3 8 4 9 5 10
1 2
3 4
5 6
7 8
9 10
Output #3
15
API Response (JSON)
{
  "problem": {
    "name": "C. Rumor",
    "description": {
      "content": "Vova promised himself that he would never play computer games... But recently Firestorm — a well-known game developing company — published their newest game, World of Farcraft, and it became really po",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF893C"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Vova promised himself that he would never play computer games... But recently Firestorm — a well-known game developing company — published their newest game, World of Farcraft, and it became really po...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "Vova 向自己保证,他再也不会玩电脑游戏了……但最近,知名游戏开发公司 Firestorm 发布了他们的最新游戏《World of Farcraft》,这款游戏变得非常流行。当然,Vova 开始玩了起来。\n\n现在他试图完成一个任务:前往名为 Overcity 的定居点,并在其中传播一则谣言。\n\nVova 知道 Overcity 中有 #cf_span[n] 个角色。有些角色彼此是朋友,他们会分享...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n \\in \\mathbb{Z}^+ $ be the number of characters, $ m \\in \\mathbb{Z}_{\\geq 0} $ the number of friendship pairs.  \nLet $ c = (c_1, c_2, \\dots, c_n) \\in \\mathbb{R}_{\\geq 0}^n $ b...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments