Rumor CodeForces - 893C
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?ci?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?ci?(0?≤?ci?≤?109) — the amount of gold?i-th character asks to start spreading the rumor.
Then?m?lines follow, each containing a pair of numbers (xi,?yi) which represent that characters?xi?and?yi?are friends (1?≤?xi,?yi?≤?n,?xi?≠?yi). 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.
Examples
Input
5 2 2 5 3 4 8 1 4 4 5Output
10Input
10 0 1 2 3 4 5 6 7 8 9 10Output
55Input
10 5 1 6 2 7 3 8 4 9 5 10 1 2 3 4 5 6 7 8 9 10Output
15Note
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.
#include <iostream> using namespace std; #include<string> #include<vector> #pragma warning (disable:4996) long long gold[1000000]; vector<int> rodes[1000000]; bool book[1000000]; long long ans, min_cur; void bfs(long long a); long long min(long long a, long long b) {return a < b ? a : b; } int main() {int n, m;cin >> n >> m;for (int cnt = 1; cnt <= n; cnt++)cin >> gold[cnt];for (int cnt = 0; cnt < m; cnt++) {int a, b;cin >> a >> b;rodes[a].push_back(b);rodes[b].push_back(a);}for (int cnt = 1; cnt <= n; cnt++) {if (!book[cnt]) {min_cur = gold[cnt];bfs(cnt);ans += min_cur;}}cout << ans; } void bfs(long long cnt) {if (!book[cnt]) {book[cnt] = true;min_cur = min(min_cur, gold[cnt]);for (int i : rodes[cnt]) {bfs(i);}} }這道題,是一個雙向圖,然后,可以通過數(shù)學關系得到,總的要花費的金額等于無鏈接的人加上有鏈接的人中花錢最少的(鏈接指的是朋友關系),也就是說,通過深度搜索(是dfs,不過我懶得改了)得到一條鏈接中的最小值,并給它做上標記,避免下次又訪問到它,這就是大體 的思路
注意下,在主函數(shù)里寫循環(huán)是因為深度搜索如果寫找最小值的話,min的值會一直鎖定為0,如果要寫成一體的,最好要加個條件判斷
然后這里算總花費的時候注意下要用long long ,因為每一個gold的值最大可能到2的九次方,那他們加起來就可以超過int 的范圍,而int最多也就10次方出頭而已
總結
以上是生活随笔為你收集整理的Rumor CodeForces - 893C的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第三周学习笔记
- 下一篇: java 学习笔记2022.1.26