Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.io.PrintWriter;
  5. import java.util.*;
  6.  
  7. public class Div2_257_D {
  8.  
  9. static long INFINITY = 1000000000000l;
  10. static int nodes;
  11. static int edges;
  12. static int trains;
  13. static long distance[];
  14. static int previous[];
  15. static int sum[];
  16. static int type[];
  17. static Edge3 railways[];
  18. static ArrayList<Edge3>[] graph;
  19.  
  20. public static void main(String[] args) throws IOException {
  21. br = new BufferedReader(new InputStreamReader(System.in));
  22. out = new PrintWriter(System.out);
  23. sc = new StringTokenizer("");
  24.  
  25. nodes = nxtInt();
  26. edges = nxtInt();
  27. trains = nxtInt();
  28.  
  29. graph = new ArrayList[nodes];
  30. for (int i = 0; i < nodes; i++) {
  31. graph[i] = new ArrayList<Edge3>();
  32. }// end for.
  33.  
  34. for (int i = 0; i < edges; i++) {
  35. int from = nxtInt() - 1;
  36. int to = nxtInt() - 1;
  37. int w = nxtInt();
  38. graph[from].add(new Edge3(from, to, w, 0));
  39. graph[to].add(new Edge3(to, from, w, 0));
  40. }// end for.
  41.  
  42. railways = new Edge3[trains];
  43. sum = new int[nodes];
  44. for (int i = 0; i < trains; i++) {
  45. int to = nxtInt() - 1;
  46. int w = nxtInt();
  47. graph[0].add(new Edge3(0, to, w, i + 1));
  48. graph[to].add(new Edge3(to, 0, w, i + 1));
  49. sum[to]++;
  50.  
  51. railways[i] = new Edge3(0, to, w, i + 1);
  52. }// end for.
  53.  
  54. dijkstra(0);
  55.  
  56. int counter = 0;
  57. boolean closed[] = new boolean[trains];
  58. for (int i = 0; i < trains; i++) {
  59. int node = railways[i].to;
  60. int weight = railways[i].weight;
  61. int trainNum = railways[i].trainNumber;
  62. if (distance[node] < weight) {
  63. closed[i] = true;
  64. counter++;
  65. // System.out.println(weight+","+distance[node]);
  66. } else if (distance[node] == weight) {
  67. if (previous[node] != 0) {
  68. counter++;
  69. // System.out.println(weight+",,");
  70. } else {
  71. if (type[node] != trainNum) {
  72. counter++;
  73. // System.out.println(weight+" ,,,");
  74. }
  75. }
  76. }
  77. }// end for(i).
  78.  
  79.  
  80. // int counter = 0;
  81. // for (int i = 0; i < nodes; i++) {
  82. // if (previous[i] > 0) {
  83. // counter += sum[i];
  84. // } else {
  85. // if (type[i] > 0){
  86. // counter += (sum[i] - 1);
  87. // }else{
  88. // counter+=sum[i];
  89. // }
  90. // }
  91. // }// end for(i).
  92.  
  93.  
  94. System.out.println(counter);
  95.  
  96. }// end method.
  97.  
  98. public static void dijkstra(int source) {
  99. distance = new long[nodes];
  100. previous = new int[nodes];
  101. type = new int[nodes];
  102. //type=0 ->road.
  103. //type !=0 ->train number.
  104. for (int i = 0; i < nodes; i++) {
  105. distance[i] = INFINITY;
  106. }
  107. distance[source] = 0;
  108.  
  109. PriorityQueue<Node3> pq = new PriorityQueue<Node3>();
  110.  
  111. pq.add(new Node3(0,0));
  112.  
  113. while (!pq.isEmpty()) {
  114. Node3 c = pq.poll();
  115. int node = c.node;
  116. for (int i = 0; i < graph[node].size(); i++) {
  117. Edge3 e = graph[node].get(i);
  118. int to = e.to;
  119. int w = e.weight;
  120. int t = e.trainNumber;
  121.  
  122. if (distance[node] + w < distance[to]) {
  123. distance[to] = distance[node] + w;
  124. previous[to] = node;
  125. type[to] = t;
  126. Node3 toAdd = new Node3(to, distance[to]);
  127. pq.add(toAdd);
  128. } else if (distance[node] + w == distance[to]) {
  129. if (previous[to] < node) {
  130. previous[to] = node;
  131. type[to] = 0;
  132. }
  133. if (previous[to] == node) {
  134. type[to] = Math.min(type[to], t);
  135. }
  136. }
  137.  
  138. }// end for.
  139. }// end while.
  140.  
  141. }// end method.
  142.  
  143. static BufferedReader br;
  144. static StringTokenizer sc;
  145. static PrintWriter out;
  146.  
  147. static String nxtTok() throws IOException {
  148. while (!sc.hasMoreTokens()) {
  149. String s = br.readLine();
  150. if (s == null)
  151. return null;
  152. sc = new StringTokenizer(s.trim());
  153. }
  154. return sc.nextToken();
  155. }
  156.  
  157. static int nxtInt() throws IOException {
  158. return Integer.parseInt(nxtTok());
  159. }
  160.  
  161. static long nxtLng() throws IOException {
  162. return Long.parseLong(nxtTok());
  163. }
  164.  
  165. static double nxtDbl() throws IOException {
  166. return Double.parseDouble(nxtTok());
  167. }
  168.  
  169. static int[] nxtIntArr(int n) throws IOException {
  170. int[] a = new int[n];
  171. for (int i = 0; i < n; i++)
  172. a[i] = nxtInt();
  173. return a;
  174. }
  175.  
  176. static long[] nxtLngArr(int n) throws IOException {
  177. long[] a = new long[n];
  178. for (int i = 0; i < n; i++)
  179. a[i] = nxtLng();
  180. return a;
  181. }
  182.  
  183. static char[] nxtCharArr() throws IOException {
  184. return nxtTok().toCharArray();
  185. }
  186.  
  187. }// end class
  188.  
  189. class Edge3 {
  190. int from;
  191. int to;
  192. int weight;
  193. int trainNumber;
  194.  
  195. // 0->road.
  196. // !=0 ->trainNumber.
  197.  
  198. public Edge3(int x, int y, int w, int t) {
  199. from = x;
  200. to = y;
  201. weight = w;
  202. trainNumber = t;
  203. }
  204. }
  205.  
  206. class Node3 implements Comparable<Node3> {
  207. int node;
  208. long key;
  209.  
  210. public Node3(int a, long k) {
  211. node = a;
  212. key = k;
  213.  
  214. }
  215.  
  216. @Override
  217. public int compareTo(Node3 o) {
  218. return Long.valueOf(key).compareTo(o.key);
  219.  
  220. }
  221.  
  222. }// end class Node3.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement