图论+贪心--The Child and Toy[CodeForces--437C]

12 篇文章 0 订阅
6 篇文章 0 订阅

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.

Input

The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v1, v2, ..., vn (0 ≤ vi ≤ 105). Then followed m lines, each line contains two integers xi and yi, representing a rope from part xi to part yi (1 ≤ xi, yi ≤ nxi ≠ yi).

Consider all the parts are numbered from 1 to n.

Output

Output the minimum total energy the child should spend to remove all n parts of the toy.

Sample test(s)

input

4 3
10 20 30 40
1 4
1 2
2 3

output

40

input

4 4
100 100 100 100
1 2
2 3
2 4
3 4

output

400

input

7 10
40 10 20 10 20 80 40
1 5
4 7
4 5
5 2
5 7
6 4
1 6
1 3
4 3
1 4

output

160

Note

One of the optimal sequence of actions in the first sample is:

  • First, remove part 3, cost of the action is 20.

  • Then, remove part 2, cost of the action is 10.

  • Next, remove part 4, cost of the action is 10.

  • At last, remove part 1, cost of the action is 0.

So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum.

In the second sample, the child will spend 400 no matter in what order he will remove the parts.

翻译

在儿童节这一天,我们的小朋友收到了来自Delayyy的礼物:一个玩具。只是,小朋友实在是太淘气了,他迫不及待的要拆掉这个玩具。
这个玩具由n个部分和m条绳子组成,每条绳子连接着两个部分,而且不论什么两个部分至多由一条绳子直接相连。为了拆掉这个玩具,小朋友必须移除它的全部n个部分。每次他仅仅能移除一个部分,而且移除须要能量。每个部分都有一个能量值v[i],他移除第i部分所需的能量是v[f[1]]+v[f[2]]+...+v[f[k]],当中f[1],f[2],...,f[k]是与i直接相连(且还未被移除)的部分的编号。
请帮助他找到移除n个部分所需的最小总能量。
Input
第一行包括两个整数n,m(1<=n<=10000;0<=m<=2000)。第二行包括n个整数v[1],v[2],...,v[n](0<=v[i]<=10^5)。接下来有m个行,每一行有两个整数x[i]和y[i],表示一条连接x[i]和y[i]的绳子(1<=x[i],y[i]<=n; x[i]不等于y[i])。
Output

输出所需的最小能量。



发现,删点其实就是删边的过程,
于是我们可以把题面进行改变,变成删边。
然后对于删每一条边,再用删点的贪心思想去做。
我们发现对于删除一条边两端的点,肯定是删权值大的点最优,
即删这条边对答案的贡献为两端点的点权中的最小值。
然后就for循环删边即可,每次答案加上两端点的点权中的最小值即可。

这里搬来大佬的解释:

  • 对于一条边,它最后一定会被删去(如果有边没删掉则意味着有点没删掉);

  • 对于一条边,它对最终答案的最优贡献为连接的两个点中较小的那个;

  • 对于一条边,它贡献最优时先删较大的点;

  • 如果所有的边都达到最优贡献,则删点的顺序为从大到小删点;

  • 如果从大到小删点,删一个点时权值比它大的点已经删完了,所以不会出现删点时达不到最优贡献的情况;

  • 故从大到小删点这种方法可行,对每一条边取连接的两个点中较小的那个即可

一道比较有意思的思维题 

#include <bits/stdc++.h>
using namespace std;

int n,m,temp,sum;
int p[1005];

int main()
{
    cin >> n >> m;
    for(int i = 1; i <= n; i ++) cin >> p[i];
    
    for(int i = 1; i <= m; i ++)
    {
        int a,b;
        cin >> a >> b;
        sum += min(p[a], p[b]);
    }
    cout << sum;
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Silver_Bullet14

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值