Codeforces Round #250 (Div. 2) C. The Child and Toy 详解

output

standard output

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.

做了这道题,感觉很有必要写一篇题解,算是开拓思路。

题目大意就是给你一张联通图,每个点有一个权值。问你最后要把所有点都分开,剪断一条路的代价是 取这条路两端的任意一点, 找到与所有与它相连的点,并把它们的权值加起来。问最小代价。

声明:选取两端的其中一个作为“目标点”,找与“目标点”相连的点的权值之和。

看起来描述很复杂,感觉也无从下手,可以考虑一些简单的情况,复杂的图都是由简单的基本单元构成。

首先考虑k个点构成环,那么代价就很明显的剪断k-1条路,且每次剪断的最小代价就两个权值的较小值。

如果是一个点连多个点的情况,且1号节点的权值大于其余点的权值 ,比如:

这一张图,若选取2为目标点,那么sum就要加上与2相连的1的权值,3和4也是类似,都是加上1的权值。

那么考虑选取1为目标点,sum就加上2,3,4的权值,结果会更小。所以,若1号的权值比其余点大,那么取其他点的权值之和。

假设1号节点的权值比其余各点的权值都小,上图的v[1] = 1,那么最优结果是分别取其余点为目标点,每次sum加上v[1]。

假设其余节点的权值有的比1号大有的比一号小,最优的情况是把比1号权值大的分别当做目标点减去,sum加的就是v[1]*比v[1]大的个数。

剩下的就是和情况1相同了。

综合上述三种情况,可以发现最优值就是一张图里所有的边链接的两端较小的权值之和。

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <queue>
#include <algorithm>
#define mem(f) memset(f,0,sizeof(f))
#define M 100005
#define mod 1000000007
#define lson o<<1, l, m
#define rson o<<1|1, m+1, r
using namespace std;
typedef __int64 LL;
const int MAX = 0x3f3f3f3f;
const int maxn = 2111111;

int n, m, a, b, v[1111], d[1111];

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

    cout << ans << endl;
    return 0;
}



时间: 2024-10-13 01:03:01

Codeforces Round #250 (Div. 2) C. The Child and Toy 详解的相关文章

codeforces --- Round #250 (Div. 2) A. The Child and Homework

<传送门> 这题就是一个坑,尼玛wa了一大片啊. 自己被hack了,比赛结束后改了又wa两次才过. [题目大意] 其实就是一个猜题小技巧(联系自己初中考试的时候怎么猜题的,这题就好理解多了).这位同学是这样来选答案的:1.如果有一些选项长度至少比其他所有的描述短两倍,或至少超过所有其他的描述的两倍,那么孩子认为这个选择很可能是正确的.2.如果正好满足以上其中一种条件(重点),这个同学就会选择它,否则就选C.给你一个选择题,让你选择出这个同学将会选择的答案. [题目分析] 首先,这个题目就是一个

codeforces --- Round #250 (Div. 2) B. The Child and Set

<传送门> [题目大意] 给你一个sum和一个limit,现在要你在1~limit中找到一些数来使得这些数的和等于sum,如果能找到的话就输出找到的数的个数和这些数,未找到输出"-1". 比赛的时候被hack了. [题目分析] 这题需要将所有的数的lowbit先求出来,然后按照大小排序,然后从后往前判断,如果这个数小于sum那么这个数就是可以构成sum的数,选进去,完了以后判断sum的值是否为0就可以了.做题的时候没将题目理解透彻. #include<bits/std

Codeforces Round #250 (Div. 2)B. The Child and Set 暴力

B. The Child and Set At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks. Fortunately, Picks remembers something about h

Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间求和+点修改+区间取模

D. The Child and Sequence At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite sequence of Picks. Fortunately, Picks remembers how to

Codeforces Round #250 (Div. 1) B. The Child and Zoo(排序+并查集)(常规好题)

B. The Child and Zoo time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Of course our child likes walking in a zoo. The zoo has n areas, that are numbered from 1 to n. The i-th area contains 

Codeforces Round #250 (Div. 1) D. The Child and Sequence (线段树)

题目链接:http://codeforces.com/problemset/problem/438/D 给你n个数,m个操作,1操作是查询l到r之间的和,2操作是将l到r之间大于等于x的数xor于x,3操作是将下标为k的数变为x. 注意成段更新的时候,遇到一个区间的最大值还小于x的话就停止更新. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5

Codeforces Round #250 (Div. 2) C、The Child and Toy

注意此题,每一个部分都有一个能量值v[i],他移除第i部分所需的能量是v[f[1]]+v[f[2]]+...+v[f[k]],其中f[1],f[2],...,f[k]是与i直接相连(且还未被移除)的部分的编号. 注意题目移除的都是与第i部分直接相连的部分的能量值, 将本题目简化得,只考虑两个点1和2,1和2相连,1的能量值是10,2的能量值是20, 移除绳子时,要保持能量最小,可以移除部分2,这样移除的能量就是与2相连的部分1的能量即是10: 故每次相连两部分都移除能量值大的即可 #includ

Codeforces Round #250 (Div. 2)——The Child and Set

题目链接 题意: 给定goal和limit,求1-limit中的若干个数,每一个数最多出现一次,且这些数的lowbit()值之和等于goal,假设存在这种一些数,输出个数和每一个数:否则-1 分析: 先考虑一下比較普通的情况,给一些数,和一个goal,问时候能达到.(最好还是设这些数已经从大到小排序) 考虑能否够贪心,对于当前的数x: 1.之后的数的和能等于x,那么假设x<=goal,显然必须选x: 2.之后的数的和能等于x-1,那么同上(这个情况就是二进制的情况) 3.之后的数的和不包含上述两

Codeforces Round #250 (Div. 1)

这几次CF都挺惨.. A 没条边权设为两端点的最小点权,最后加起来. 数组开小,WA一次 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath> 8 #include<queue> 9 #