CF 893C DFS
来源:互联网 发布:什么软件可以看轻小说 编辑:程序博客网 时间:2024/06/11 20:00
题目链接:http://codeforces.com/problemset/problem/893/C
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.
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.
Print one number — the minimum amount of gold Vova has to spend in order to finish the quest.
5 22 5 3 4 81 44 5
10
10 01 2 3 4 5 6 7 8 9 10
55
10 51 6 2 7 3 8 4 9 5 101 23 45 67 89 10
15
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.
题目意思:给你n个点,m条边,其中每个点有权值 c [ i ] ,每条边对应 a [ i ] <-> a [ j ],花费为 min ( c [ i ],c [ j ] ),点相连的集合所花费的为其中权值最小的一个。求走完所有点的最小花费。
题解:DFS,遍历每个点及与它相连的点,求出这个联通块的最小花费即可,最后再统计一下单独的点,加上单独的点的权值即为最后答案。
AC代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#define INF 1e9+10
using namespace std;
const int N = 1e5+10;
long long c[N];
long long tmp;
vector <long long> maze[N];
bool vis[N];
int n,m;
void dfs(long long pos)
{
vis[pos]=true;
vector <long long>::iterator it;
long long ans=0;
for(it=maze[pos].begin();it!=maze[pos].end();it++)
{
if(!vis[*it])
{
tmp=min(tmp,c[*it]);
dfs(*it);
}
}
return ;
}
int main()
{
cin >> n >> m;
memset(vis,false,sizeof(vis));
memset(maze,0,sizeof(maze));
for(int i=1;i<=n;i++) cin >> c[i];
for(int i=1;i<=m;i++)
{
int u,v;
cin >> u >> v;
maze[u].push_back(v);
maze[v].push_back(u);
}
long long ans=0;
for(long long i=1;i<=n;i++)
{
if(!vis[i])
{
tmp=c[i];
dfs(i);
ans+=tmp;
}
}
cout << ans << endl;
return 0;
}
- CF 893C DFS
- cf 580 c(邻接表+dfs)
- CF#321-C - Kefa and Park-DFS
- CF 616C The Labyrinth(dfs)
- CF 782C 贪心,DFS染色,水题
- CF-29C - Mail Stamps(DFS+离散化)
- CF 383C Propagating tree(dfs序+线段树)
- CF#291 C Watto and Mechanism(Trie+dfs)
- cf C. Kefa and Park (树的dfs)
- cf-edu#5-C The Labyrinth-dfs记忆化搜搜
- CF Round #616 (A,B,C 字符串,贪心,DFS)
- CF 505 B (dfs)
- CF 638 dfs
- CF 717E[dfs]
- <cf>Solitaire(DFS or DP)
- CF D. Giving Awards(dfs)
- Cf 120 F 【vector+dfs】
- cf 337 D(dfs)
- Tablayout的应用
- spring整合jdbc
- 写一个功能类似字符串title()方法的函数
- SparkCore1
- VS2017+QT5.10编译QT静态库
- CF 893C DFS
- linux内核的I2C子系统详解5——i2c_driver的注册、i2c_client的来源
- comic life中文版下载 | Comic Life(漫画制作工具)官方破解版V3.5.6下载 | comic life破解版
- (转)被AI改变的风投模式:数据驱动使效率提高10倍
- 兄弟连学python(4)——内部函数
- 学习安卓第一周
- Java集合之三List接口及其实现类ArrayList、LinkedList、Vector
- js原生ajax与jQuery的ajax
- 根据Request获取客户端IP