首页 > 其他 > 详细

D. The Child and Zoo

时间:2019-08-25 16:36:03      阅读:121      评论:0      收藏:0      [点我收藏+]

 

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

原文:https://www.cnblogs.com/towerbird/p/11407654.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!