zoukankan      html  css  js  c++  java
  • codeforces437C

    The Child and Toy

     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.

    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.

    sol:容易发现删去一个点等于删掉所有与这个点相连的边,考虑每条边的贡献,比如边<a,b>,肯定取a,b中花费小的更优

    Ps:代码极短

    #include <bits/stdc++.h>
    using namespace std;
    typedef int ll;
    inline ll read()
    {
        ll s=0;
        bool f=0;
        char ch=' ';
        while(!isdigit(ch))
        {
            f|=(ch=='-'); ch=getchar();
        }
        while(isdigit(ch))
        {
            s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
        }
        return (f)?(-s):(s);
    }
    #define R(x) x=read()
    inline void write(ll x)
    {
        if(x<0)
        {
            putchar('-'); x=-x;
        }
        if(x<10)
        {
            putchar(x+'0'); return;
        }
        write(x/10);
        putchar((x%10)+'0');
        return;
    }
    #define W(x) write(x),putchar(' ')
    #define Wl(x) write(x),putchar('
    ')
    const int N=1005;
    int n,m,Cost[N];
    #define Pic Picture
    int main()
    {
        int i,ans=0;
        R(n); R(m);
        for(i=1;i<=n;i++) R(Cost[i]);
        for(i=1;i<=m;i++) ans+=min(Cost[read()],Cost[read()]);
        Wl(ans);
        return 0;
    }
    /*
    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
    */
    View Code
  • 相关阅读:
    Windows Live Writer 语法高亮
    二十一、Java基础--------IO流之综合案例分析
    二十、Java基础--------IO流之其他对象
    十九、Java基础--------IO流之字节流
    十八、Java基础--------IO流体系以及字符流
    十七、Java基础---------集合框架之Map
    十六、Java基础---------集合框架之Set
    十五、Java基础---------集合框架体系以及List
    十四、Java基础---------String、StringBuffer、StringBuilder基本应用
    十三、Java基础---------多线程总结
  • 原文地址:https://www.cnblogs.com/gaojunonly1/p/10590906.html
Copyright © 2011-2022 走看看