Codeforces Round #250 (Div. 2) C. The Child and Toy 詳解

來源:互聯網
上載者:User

標籤:cf

outputstandard output

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 spendvf1?+?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 alln parts.

Input

The first line contains two integers n andm (1?≤?n?≤?1000;0?≤?m?≤?2000). The second line contains n integers: v1,?v2,?...,?vn (0?≤?vi?≤?105). Then followedm lines, each line contains two integers xi andyi, representing a rope from partxi to partyi (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.

Sample test(s)Input
4 310 20 30 401 41 22 3
Output
40
Input
4 4100 100 100 1001 22 32 43 4
Output
400
Input
7 1040 10 20 10 20 80 401 54 74 55 25 76 41 61 34 31 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.


做了這道題,感覺很有必要寫一篇題解,算是開拓思路。

題目大意就是給你一張聯通圖,每個點有一個權值。問你最後要把所有點都分開,剪斷一條路的代價是 取這條路兩端的任意一點, 找到與所有與它相連的點,並把它們的權值加起來。問最小代價。

聲明:選取兩端的其中一個作為“目標點”,找與“目標點”相連的點的權值之和。

看起來描述很複雜,感覺也無從下手,可以考慮一些簡單的情況,複雜的圖都是由簡單的基本單元構成。

首先考慮k個點構成環,那麼代價就很明顯的剪斷k-1條路,且每次剪斷的最小代價就兩個權值的較小值。

如果是一個點連多個點的情況,且1號節點的權值大於其餘點的權值 ,比如:

這一張圖,若選取2為目標點,那麼sum就要加上與2相連的1的權值,3和4也是類似,都是加上1的權值。

那麼考慮選取1為目標點,sum就加上2,3,4的權值,結果會更小。所以,若1號的權值比其餘點大,那麼取其他點的權值之和。


假設1號節點的權值比其餘各點的權值都小,的v[1] = 1,那麼最優結果是分別取其餘點為目標點,每次sum加上v[1]。


假設其餘節點的權值有的比1號大有的比一號小,最優的情況是把比1號權值大的分別當做目標點減去,sum加的就是v[1]*比v[1]大的個數。

剩下的就是和情況1相同了。


綜合上述三種情況,可以發現最優值就是一張圖裡所有的邊連結的兩端較小的權值之和。


#include <stdio.h>#include <string.h>#include <math.h>#include <iostream>#include <queue>#include <algorithm>#define mem(f) memset(f,0,sizeof(f))#define M 100005#define mod 1000000007#define lson o<<1, l, m#define rson o<<1|1, m+1, rusing namespace std;typedef __int64 LL;const int MAX = 0x3f3f3f3f;const int maxn = 2111111;int n, m, a, b, v[1111], d[1111];int main(){    cin >> n >> m;    for(int i = 1; i <= n; i++)  cin >> v[i];    int ans = 0;    for(int i = 0; i < m; i++) {        cin >> a >> b;        ans += min(v[a], v[b]);    }    cout << ans << endl;    return 0;}




Codeforces Round #250 (Div. 2) C. The Child and Toy 詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.