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 }

猜你喜欢

转载自www.cnblogs.com/towerbird/p/11407654.html
zoo