Codeforces Round #250 (Div. 2) C. The Child and Toy details,

Source: Internet
Author: User

Codeforces Round #250 (Div. 2) C. The Child and Toy details,
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 consistsNParts andMRopes. 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 partIAsVI. The child spendVF1 worker + workerVF2 cores + cores... cores + CoresVFKEnergy for removing partIWhereF1, bytes,F2, middle..., middle ,...,FKAre the parts that are directly connected toI-Th and haven'tbeen removed.

Help the child to find out, what is the minimum total energy he shocould spend to remove allNParts.

Input

The first line contains two integersNAndM(1 digit ≤ DigitNMemory ≤ memory 1000; 0 memory ≤ memoryMLimit ≤00002000). The second line containsNIntegers:V1, bytes,V2, middle..., middle ,...,VN(0 bytes ≤ bytesVILimit ≤ limit 105). Then followedMLines, each line contains two integersXIAndYI, Representing a rope from partXITo partYI(1 digit ≤ DigitXI, Bytes,YILimit ≤ limitN;XI  =YI).

Consider all the parts are numbered from 1N.

Output

Output the minimum total energy the child shoshould spend to remove allNParts 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 seconds + limit 10 minutes + limit 10 minutes + limit 0 minutes = Limit 40, which is the minimum.

In the second sample, the child will spend 400 no matter in what order he will remove the parts.


After doing this, I feel it is necessary to write a question, which is a pioneering idea.

The question is to give you a connectivity diagram, with each vertex having a weight. In the end, I asked you to separate all vertices. The cost of cutting a path is to take any point at both ends of the path, locate all vertices connected to it, and add their weights. Minimum cost.

Declaration: select one of the two ends as the "target point" and find the sum of the weights of the points connected to the "target point.

It seems that the description is complex, and you cannot start with it. You can consider some simple situations. Complex graphs are composed of simple basic units.

First consider k points constitute a ring, then the cost is obvious cut K-1, and the minimum cost of each cut is the smaller values of the two weights.

If a vertex is connected to multiple vertices, and the weight of Node 1 is greater than that of other vertices, for example:

In this figure, if 2 is selected as the target point, the sum value must be added with the 1 value connected to 2, and the 3 and 4 values are similar, with the 1 value added.

Select 1 as the target point, and add the weights of 2, 3, and 4 to sum. The result will be smaller. Therefore, if the value of 1 is greater than its remainder, the sum of the values of other points is used.


Assume that the weight of Node 1 is smaller than the weight of the remaining points, and v [1] = 1, the optimal result is to take the remaining points as the target point, respectively, add v [1] to sum each time.


Assume that the weights of the remaining nodes are greater than those of the first node and smaller than that of the first node. The optimal solution is to subtract the weights greater than the first node as the target point, sum is a number larger than v [1.

The rest is the same as Case 1.


Based on the above three cases, we can find that the optimal value is the sum of smaller weights at both ends of all edge links of a token.


#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;}


Zookeeper


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.