码迷,mamicode.com
首页 > 其他好文 > 详细

D. The Child and Zoo

时间:2019-08-25 13:54:32      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:oid   结果   new   span   codeforce   sem   return   static   get   

 

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

标签:oid   结果   new   span   codeforce   sem   return   static   get   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!