• 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
  • 相关阅读:
    开发导致的内存泄露问题,运维小伙伴儿这样排查不背锅
    JVM垃圾回收器、内存分配与回收策略
    笔试编程(二) | 7种常见的排序算法解析(附实现代码)
    HBase高级特性、rowkey设计以及热点问题处理
    Geotools创建Feature的两种方式
    geotools实现追加数据到数据库
    Java连接mysql数据库经典代码
    leaflet加载高德地图和Geoserver的WMS服务
    geotools学习之连接数据库并获取数据
    LeaFlet中切片图层使用自定义坐标系
  • 原文地址:https://www.cnblogs.com/gaojunonly1/p/10590906.html
Copyright © 2020-2023  润新知