• 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
  • 相关阅读:
    .vue 文件中怎么引用全局的sass 样式
    git 本地仓库同时推送到多个远程仓库
    markdown文件生成html页面
    查看IPA安装包文件信息工具
    如何给iOS安装包IPA注入一个动态库文件?
    一年级语文上册读拼音写汉字,聪明的家长已收藏,偷偷给孩子练习
    关于直接下载第三方IPA应用分发平台之蒲公英的研究
    如何给iOS安装包IPA添加时间锁功能?
    关于几种获取iOS设备UDID典型方式的技术探讨
    针对五款IPA文件安装工具的深度评测
  • 原文地址:https://www.cnblogs.com/gaojunonly1/p/10590906.html
Copyright © 2020-2023  润新知