The Child and Toy

source

Description

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 ≤ n; xi ≠ 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.

Example
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.

题意:
一个玩具有n个点,m条边,每个点都有一个权值,现在要拆除玩具,将每个点拿出来,每拆除一个点消耗的能量为与这个点直接关联的所有点的权值和,问拆除所有点所需最小能量.

题解:每次都应该选择权值最大的节点拆,到最后,其实就是每一条连边的两个节点中节点权值较小值的总和。

解法一:每次选择权值最大的节点拆

#include
#include
#include
#include
#include
using namespace std;
vector graph[1010];
struct Node{

    int index;
    int weight;
    bool operator<(const Node &t)
    {
        return weight>t.weight;
    }
} arr[1010];
int w[1010];
int main()
{
    int n,m,temp,a,b,sum=0;;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&temp);
        arr[i].weight=temp;
        arr[i].index=i;
        w[i]=temp;

    }
    for(int i=1;i<=m;i++)
    {

        scanf("%d%d",&a,&b);
        graph[a].push_back(b);
        graph[b].push_back(a);
    }
    sort(arr+1,arr+n+1);
    for(int i=1;i<=n;i++)
    {
        int t=arr[i].index;
        for(vector::iterator it=graph[t].begin();it!=graph[t].end();it++)
        {
            sum+=w[*it];
            vector::iterator e=graph[*it].begin();
            for(;e!=graph[*it].end();e++)
            {
                if(*e==t) break;
            }
            graph[*it].erase(e);
        }
    }
    printf("%d",sum);

}

解法二:最小值为 每一条连边的两个节点中节点权值较小值的总和。

#include
#include
using namespace std;
int main()
{
    int n,m,temp,a,b,sum=0;
    int w[1010];
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",w+i);
    }
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&a,&b);
        sum+=min(w[a],w[b]);
    }
    printf("%d",sum);
}

你可能感兴趣的:(The Child and Toy)