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;
}

转载于:https://www.cnblogs.com/YDDDD/p/11368350.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值