CodeForces-437C(贪心)

链接:

https://vjudge.net/problem/CodeForces-437C

题意:

On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy.

The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as vi. The child spend vf1 + vf2 + ... + vfk energy for removing part i where f1, f2, ..., fk are the parts that are directly connected to the i-th and haven't been removed.

Help the child to find out, what is the minimum total energy he should spend to remove all n parts.

思路:

优先选值较大的点去拿,这样值较大的点就被累加的次数小.

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;

const int MAXN = 1e3+10;
struct Node
{
    int v;
    int pos;
    bool operator < (const Node& that) const
    {
        return this->v > that.v;
    }
}node[MAXN];
int Map[MAXN][MAXN];
int Val[MAXN];
int n, m;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    for (int i = 1;i <= n;i++)
    {
        cin >> node[i].v;
        Val[i] = node[i].v;
        node[i].pos = i;
    }
    int u, v;
    for (int i = 1;i <= m;i++)
    {
        cin >> u >> v;
        Map[u][v] = 1;
        Map[v][u] = 1;
    }
    sort(node+1, node+1+n);
    int res = 0;
    for (int i = 1;i <= n;i++)
    {
        for (int j = 1;j <= n;j++)
        {
            if (Map[node[i].pos][j])
            {
                res += Val[j];
                Map[node[i].pos][j] = Map[j][node[i].pos] = 0;
            }
        }
    }
    cout << res << endl;

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/YDDDD/p/11368350.html