D. The Child and Zoo

Posted towerbird

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了D. The Child and Zoo相关的知识,希望对你有一定的参考价值。

 

http://codeforces.com/contest/437/problem/D

 

贪心,按照自身的费用从大到小拿,费用的相等先后顺序不影响结果

 

 

 1 import java.util.*;
 2 
 3 public class Main 
 4     static final int MAX = Integer.MAX_VALUE;
 5 
 6     public static void main(String[] args) 
 7         Scanner io = new Scanner(System.in);
 8         int n = io.nextInt(), m = io.nextInt();
 9 
10         PriorityQueue<P> q = new PriorityQueue<>();
11         int[] a = new int[1000 + 50];
12         for (int i = 1; i <= n; i++) q.add(new P(i, a[i] = io.nextInt()));
13 
14         ArrayList<Integer>[] c = new ArrayList[1000 + 50];
15         for (int i = 0; i < c.length; i++) c[i] = new ArrayList<>();
16         for (int i = 0, aa, bb; i < m; i++) 
17             aa = io.nextInt();
18             bb = io.nextInt();
19             c[aa].add(bb);
20             c[bb].add(aa);
21         
22 
23         int sum = 0, p;
24         int[] vis = new int[1000 + 50];
25         while (!q.isEmpty()) 
26             p = q.poll().n;
27             vis[p] = 1;
28             for (int i = 0, cc; i < c[p].size(); i++) 
29                 cc = c[p].get(i);
30                 if (vis[cc] == 0) sum += a[cc];
31             
32         
33         System.out.println(sum);
34     
35 
36     static class P implements Comparable<P> 
37         int n, m;
38 
39         @Override
40         public int compareTo(P o) 
41             return -m + o.m;
42         
43 
44         public P(int n, int m) 
45             this.n = n;
46             this.m = m;
47         
48     
49 

 

以上是关于D. The Child and Zoo的主要内容,如果未能解决你的问题,请参考以下文章

[线段树 区间取模] D. The Child and Sequence

Codeforces 437 D. The Child and Zoo 并查集

Codeforces Round #250 (Div. 1) - D. The Child and Sequence(线段树)

D. Vasya And The Matrix

D. The Fair Nut and the Best Path

D. Vasya And The Matrix(Educational Codeforces Round 48)