• The Child and Toy


    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 + ... + vfkenergy for removing part i where f1, f2, ..., fkare 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 nparts.

    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.

    Examples

    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根绳子组成。每根绳子连接两个部分,但每对部分最多由一根绳子连接。要把玩具拆开,孩子必须把它的所有零件都拆下来。孩子可以一次移除一个部件,每个移除都消耗一个能量。让我们将第一部分的能量值定义为vi.。孩子花费vf1 + vf2 + ... + vfk能量移除第一部分,其中f1, f2, ..., fk是直接连接到第i部分但尚未移除的部分。帮助孩子找出,他应该花费的最小总能量是多少,以去除所有的N个部分。

    思路:仔细一看比较简单。一根绳的两端哪个端点的能量比较小,则以这个端点裁剪。最后累加裁剪所有绳的最小能量。

     1 #include <cstdio>
     2 #include <fstream>
     3 #include <algorithm>
     4 #include <cmath>
     5 #include <deque>
     6 #include <vector>
     7 #include <queue>
     8 #include <string>
     9 #include <cstring>
    10 #include <map>
    11 #include <stack>
    12 #include <set>
    13 #include <sstream>
    14 #include <iostream>
    15 #define mod 1000000007
    16 #define ll long long
    17 using namespace std;
    18 
    19 int n,m;
    20 int sz[2005];
    21 int main()
    22 {
    23     cin>>n>>m;
    24     for(int i=1;i<=n;i++)
    25     {
    26         cin>>sz[i];
    27     }
    28     int a,b;
    29     int ans=0;
    30     for(int i=0;i<m;i++)
    31     {
    32         cin>>a>>b;
    33         ans+=min(sz[a],sz[b]);
    34         //cout<<ans<<endl;
    35     }
    36     cout<<ans<<endl;
    37 }
  • 相关阅读:
    HDU 2546:饭卡(01背包)
    HPU 第三次积分赛:阶乘之和(水题)
    拓扑排序练习题
    HDU 2647:Reward(拓扑排序+队列)
    HDU 3342:Legal or Not(拓扑排序)
    HDU 2094:产生冠军(拓扑排序)
    POJ 2585:Window Pains(拓扑排序)
    51Nod 1002:数塔取数问题(DP)
    cogs696 longest prefix
    poj3764 The xor-longest Path
  • 原文地址:https://www.cnblogs.com/mzchuan/p/11240645.html
Copyright © 2020-2023  润新知