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
  • 相关阅读:
    全站之路一路坑(3)——使用百度站长工具提交站点地图
    全站之路一路坑(2)——在Apache下部署django博客
    全栈一路坑之使用django创建博客
    Django添加模型无法数据迁移解决方法
    一款自动汇报工作的微信机器人
    微信js接口自定义分享内容
    C Primer
    皇家每羊历险记(四)——角色移动
    【转载】Spring Boot 使用SSL-HTTPS
    解决https负载报错:unable to find valid certification path to requested target
  • 原文地址:https://www.cnblogs.com/gaojunonly1/p/10590906.html
Copyright © 2011-2022 走看看