Codeforces 437C The Child and Toy(贪心)

时间:2022-03-06 03:20:48

题目连接:Codeforces 437C  The Child and Toy

贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm> using namespace std; const int MAX_N = 1000 + 10;
int G[MAX_N][MAX_N]; struct Value
{
int val;
int i;
};
int cmp(Value a, Value b)
{
return a.val > b.val;
}
Value value[MAX_N];
int value1[MAX_N]; int main()
{
memset(G, 0, sizeof(G));
int n, m;
scanf("%d%d", &n, &m);
for(int i = 1;i <= n; i++)
{
scanf("%d", &value[i].val);
value1[i] = value[i].val;
value[i].i = i;
}
int u, v;
for(int i = 0; i < m; i++)
{
scanf("%d%d", &u, &v);
G[u][v] = G[v][u] = 1;
}
sort(value + 1, value + n + 1, cmp);
int sum = 0;
int b;
for(int i = 1; i <= n; i++)
{
b = value[i].i;
for(int j = 1; j <= n; j++)
{
if(G[b][j])
{
sum += value1[j];
G[b][j] = G[j][b] = 0;
}
}
}
printf("%d\n", sum);
return 0;
}