D. Jzzhu and Cities

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

 

先对公路集合求最短路,再判断铁路集合是否可再次更新d[],如果可以就r--

 

 

 1 public class Main {
 2 
 3     public static void main(String[] args) {
 4         IO io = new IO();
 5         int n = io.nextInt(), m = io.nextInt(), k = io.nextInt();
 6         ArrayList<int[]>[] g = new ArrayList[100000 + 100];
 7         for (int i = 0; i < g.length; i++) g[i] = new ArrayList<>();
 8 
 9         for (int i = 0; i < m; i++) {
10             int x = io.nextInt(), y = io.nextInt(), c = io.nextInt();
11             g[x].add(new int[]{y, c});
12             g[y].add(new int[]{x, c});
13         }
14 
15         PriorityQueue<P>q=new PriorityQueue<>();
16         //w==0:公路集合最短路,w==1,铁路更新最短路
17         q.add(new P(0,0,1));
18         int[]d=new int[100000+100];
19         Arrays.fill(d,-1);
20 
21         for (int i = 0; i < k; i++) {
22             int y=io.nextInt(),c=io.nextInt();
23             q.add(new P(c,1,y));
24         }
25 
26         int r=k;
27         while (!q.isEmpty()){
28             P p=q.poll();
29             int c=p.c,w=p.w,x=p.x;
30             if (d[x]!=-1)continue;
31             if (w==1)r--;
32             d[x]=c;
33             for (int i = 0; i < g[x].size(); i++) {
34                 int y=g[x].get(i)[0],cc=g[x].get(i)[1];
35                 if (d[y]==-1)q.add(new P(c+cc,0,y));
36             }
37         }
38         io.println(r);
39     }
40 
41     static class P implements Comparable<P> {
42         int c, w, x;
43 
44         public P(int c, int w, int x) {
45             this.c = c;
46             this.w = w;
47             this.x = x;
48         }
49 
50         @Override
51         public int compareTo(P o) {
52             if (o.c != c) return c - o.c;
53             if (o.w != w) return w - o.w;
54             if (o.x != x) return x - o.x;
55             return 0;
56         }
57     }
58 
59     
60 }
原文地址:https://www.cnblogs.com/towerbird/p/11238490.html