Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.util.*;
- public class Div2_257_D {
- static long INFINITY = 1000000000000l;
- static int nodes;
- static int edges;
- static int trains;
- static long distance[];
- static int previous[];
- static int sum[];
- static int type[];
- static Edge3 railways[];
- static ArrayList<Edge3>[] graph;
- public static void main(String[] args) throws IOException {
- br = new BufferedReader(new InputStreamReader(System.in));
- out = new PrintWriter(System.out);
- sc = new StringTokenizer("");
- nodes = nxtInt();
- edges = nxtInt();
- trains = nxtInt();
- graph = new ArrayList[nodes];
- for (int i = 0; i < nodes; i++) {
- graph[i] = new ArrayList<Edge3>();
- }// end for.
- for (int i = 0; i < edges; i++) {
- int from = nxtInt() - 1;
- int to = nxtInt() - 1;
- int w = nxtInt();
- graph[from].add(new Edge3(from, to, w, 0));
- graph[to].add(new Edge3(to, from, w, 0));
- }// end for.
- railways = new Edge3[trains];
- sum = new int[nodes];
- for (int i = 0; i < trains; i++) {
- int to = nxtInt() - 1;
- int w = nxtInt();
- graph[0].add(new Edge3(0, to, w, i + 1));
- graph[to].add(new Edge3(to, 0, w, i + 1));
- sum[to]++;
- railways[i] = new Edge3(0, to, w, i + 1);
- }// end for.
- dijkstra(0);
- int counter = 0;
- boolean closed[] = new boolean[trains];
- for (int i = 0; i < trains; i++) {
- int node = railways[i].to;
- int weight = railways[i].weight;
- int trainNum = railways[i].trainNumber;
- if (distance[node] < weight) {
- closed[i] = true;
- counter++;
- // System.out.println(weight+","+distance[node]);
- } else if (distance[node] == weight) {
- if (previous[node] != 0) {
- counter++;
- // System.out.println(weight+",,");
- } else {
- if (type[node] != trainNum) {
- counter++;
- // System.out.println(weight+" ,,,");
- }
- }
- }
- }// end for(i).
- // int counter = 0;
- // for (int i = 0; i < nodes; i++) {
- // if (previous[i] > 0) {
- // counter += sum[i];
- // } else {
- // if (type[i] > 0){
- // counter += (sum[i] - 1);
- // }else{
- // counter+=sum[i];
- // }
- // }
- // }// end for(i).
- System.out.println(counter);
- }// end method.
- public static void dijkstra(int source) {
- distance = new long[nodes];
- previous = new int[nodes];
- type = new int[nodes];
- //type=0 ->road.
- //type !=0 ->train number.
- for (int i = 0; i < nodes; i++) {
- distance[i] = INFINITY;
- }
- distance[source] = 0;
- PriorityQueue<Node3> pq = new PriorityQueue<Node3>();
- pq.add(new Node3(0,0));
- while (!pq.isEmpty()) {
- Node3 c = pq.poll();
- int node = c.node;
- for (int i = 0; i < graph[node].size(); i++) {
- Edge3 e = graph[node].get(i);
- int to = e.to;
- int w = e.weight;
- int t = e.trainNumber;
- if (distance[node] + w < distance[to]) {
- distance[to] = distance[node] + w;
- previous[to] = node;
- type[to] = t;
- Node3 toAdd = new Node3(to, distance[to]);
- pq.add(toAdd);
- } else if (distance[node] + w == distance[to]) {
- if (previous[to] < node) {
- previous[to] = node;
- type[to] = 0;
- }
- if (previous[to] == node) {
- type[to] = Math.min(type[to], t);
- }
- }
- }// end for.
- }// end while.
- }// end method.
- static BufferedReader br;
- static StringTokenizer sc;
- static PrintWriter out;
- static String nxtTok() throws IOException {
- while (!sc.hasMoreTokens()) {
- String s = br.readLine();
- if (s == null)
- return null;
- sc = new StringTokenizer(s.trim());
- }
- return sc.nextToken();
- }
- static int nxtInt() throws IOException {
- return Integer.parseInt(nxtTok());
- }
- static long nxtLng() throws IOException {
- return Long.parseLong(nxtTok());
- }
- static double nxtDbl() throws IOException {
- return Double.parseDouble(nxtTok());
- }
- static int[] nxtIntArr(int n) throws IOException {
- int[] a = new int[n];
- for (int i = 0; i < n; i++)
- a[i] = nxtInt();
- return a;
- }
- static long[] nxtLngArr(int n) throws IOException {
- long[] a = new long[n];
- for (int i = 0; i < n; i++)
- a[i] = nxtLng();
- return a;
- }
- static char[] nxtCharArr() throws IOException {
- return nxtTok().toCharArray();
- }
- }// end class
- class Edge3 {
- int from;
- int to;
- int weight;
- int trainNumber;
- // 0->road.
- // !=0 ->trainNumber.
- public Edge3(int x, int y, int w, int t) {
- from = x;
- to = y;
- weight = w;
- trainNumber = t;
- }
- }
- class Node3 implements Comparable<Node3> {
- int node;
- long key;
- public Node3(int a, long k) {
- node = a;
- key = k;
- }
- @Override
- public int compareTo(Node3 o) {
- return Long.valueOf(key).compareTo(o.key);
- }
- }// end class Node3.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement