标签:style class code http tar color
题目连接:Codeforces 437C The Child and Toy
题目大意:孩子有一个玩具,有n个部件组成,m条绳子组成,每条绳子连接两个部件。小孩比较顽皮,要将玩具拆成不可分割的部件,每次剪断一条绳子的代价是该绳子连接的两个部件的权值中较小的值。问说最小的总代价是多少。
解题思路:以为每条边都是要被剪断的,所以将节点按照代价值从大到小排序,每次拿掉权值大的点,与该点连接并且还未剪断的边均用另外点的权值。
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 1005;
const int M = 2005;
struct state {
int id;
int val;
}p[N];
int n, m, v[N], val[N];
vector<int> g[N];
inline bool cmp (const state& a, const state& b) {
return a.val > b.val;
}
void init () {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &val[i]);
p[i].id = i;
p[i].val = val[i];
}
sort(p + 1, p + n + 1, cmp);
int a, b;
for (int i = 0; i < m; i++) {
scanf("%d%d", &a, &b);
g[a].push_back(b);
g[b].push_back(a);
}
}
int main () {
init();
int ans = 0;
memset(v, 0, sizeof(v));
for (int i = 1; i <= n; i++) {
int u = p[i].id;
v[u] = 1;
for (int j = 0; j < g[u].size(); j++) {
int vi = g[u][j];
if (v[vi])
continue;
ans += val[vi];
}
}
printf("%d\n", ans);
return 0;
}
Codeforces 437C The Child and Toy(贪心),布布扣,bubuko.com
Codeforces 437C The Child and Toy(贪心)
标签:style class code http tar color
原文地址:http://blog.csdn.net/keshuai19940722/article/details/30773185