Problem:
Score : 400 points

Problem Statement

You are given an undirected connected weighted graph with N vertices and M edges that contains neither self-loops nor double edges.

The i-th (1≤i≤M) edge connects vertex a_i and vertex b_i with a distance of c_i.

Here, a self-loop is an edge where a_i = b_i (1≤i≤M), and double edges are two edges where (a_i,b_i)=(a_j,b_j) or (a_i,b_i)=(b_j,a_j) (1≤i<j≤M).

A connected graph is a graph where there is a path between every pair of different vertices.

Find the number of the edges that are not contained in any shortest path between any pair of different vertices.

Constraints

2≤N≤100

N-1≤M≤min(N(N-1)/2,1000)

1≤a_i,b_i≤N

1≤c_i≤1000

c_i is an integer.

The given graph contains neither self-loops nor double edges.

The given graph is connected.

Input

The input is given from Standard Input in the following format:

N M
a_1 b_1 c_1
a_2 b_2 c_2
:
a_M b_M c_M

Output

Print the number of the edges in the graph that are not contained in any shortest path between any pair of different vertices.

Sample Input 1

3 3
1 2 1
1 3 1
2 3 3

Sample Output 1

1

In the given graph, the shortest paths between all pairs of different vertices are as follows:

The shortest path from vertex 1 to vertex 2 is: vertex 1 → vertex 2, with the length of 1.

The shortest path from vertex 1 to vertex 3 is: vertex 1 → vertex 3, with the length of 1.

The shortest path from vertex 2 to vertex 1 is: vertex 2 → vertex 1, with the length of 1.

The shortest path from vertex 2 to vertex 3 is: vertex 2 → vertex 1 → vertex 3, with the length of 2.

The shortest path from vertex 3 to vertex 1 is: vertex 3 → vertex 1, with the length of 1.

The shortest path from vertex 3 to vertex 2 is: vertex 3 → vertex 1 → vertex 2, with the length of 2.

Thus, the only edge that is not contained in any shortest path, is the edge of length 3 connecting vertex 2 and vertex 3, hence the output should be 1.

Sample Input 2

3 2
1 2 1
2 3 1

Sample Output 2

0

Every edge is contained in some shortest path between some pair of different vertices.

Solution in Java (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.*;
import java.math.*;
import java.util.*;
public class Main { 
 
	public static void main(String[] args) { 
		FastScanner I = new FastScanner(); //Input
		OutPut O = new OutPut(); //Output
		int N = I.nextInt();
		int M = I.nextInt();
		int ans = M; //Assuming all edges to be invalid
		long[][] shortest = new long[N+1][N+1];
		int[][] weights = new int[N+1][N+1]; //weights[i][j] = weight of edge between 
		//vertex i and j (bidirectional)
		ArrayList<int[]>[][] used = new ArrayList[N+1][N+1]; //Each matrix cell
		//stores edges used for shortest path between i and j to be achieved
		for (int i = 1; i <= N; i++) {
			for (int j = 1; j <= N; j++) {
				used[i][j] = new ArrayList<int[]>();
			}
		}
		boolean[][] vis = new boolean[N+1][N+1]; //When checking off distinct edges,
		//I will use this as a marker to make sure a certain edge is a new edge that is within
		//the shortest path between two vertices
		for (int i = 1; i <= N; i++) {
			for (int j = 1; j <= N; j++) {
				shortest[i][j]=Integer.MAX_VALUE;
			}
		}
		for (int i = 0; i < M; i++) {
			int a = I.nextInt();
			int b = I.nextInt();
			int w = I.nextInt();
			weights[a][b] = w;
			weights[b][a] = w;
		    shortest[a][b]=w; //Assuming one edge path is shortest in the beginning of algorithm
		    shortest[b][a]=w;
		    int[] pair = {a,b};
		    used[a][b].add(pair);
		    used[b][a].add(pair);
		}
		for (int i = 1; i <= N; i++) shortest[i][i]=0; //Distance from vertex i to itself is 0
		for (int bet = 1; bet<=N; bet++) {
			for (int start = 1; start<=N; start++) {
				for (int end = 1; end<=N; end++) {
					if (shortest[start][bet]+shortest[bet][end]<shortest[start][end]) {
						//Strictly less because = length still counts as A shortest both
						//therefore all edges would be valid
						used[start][end]=new ArrayList<int[]>();
						used[end][start]=new ArrayList<int[]>();
						for (int chk = 0; chk < used[start][bet].size(); chk++) {
							used[start][end].add(used[start][bet].get(chk));
							used[end][start].add(used[start][bet].get(chk));
						}
						for (int chk = 0; chk < used[bet][end].size(); chk++) {
							used[start][end].add(used[bet][end].get(chk));
							used[end][start].add(used[bet][end].get(chk));
						}
						shortest[start][end]=shortest[start][bet]+shortest[bet][end];
					}
				}
			}
		}
		for (int i = 1; i <= N; i++) {
			for (int j = 1; j <= N; j++) {
				for (int chk = 0; chk<used[i][j].size(); chk++) {
					int[] pair = used[i][j].get(chk);
					int a = pair[0];
					int b = pair[1];
					if (!vis[a][b]) {
						vis[a][b]=true;
						vis[b][a]=true;
						ans--; //A new edge that contributes
					}
				}
			}
		}
		O.pln(ans);
	}
	public static ArrayList<String> perms(int N,int limit){
		if (N==1) {
			ArrayList<String> ret = new ArrayList<String>();
			for (int i = 0; i < limit; i++) {
				 ret.add(Integer.toString(i));
			}
			return ret;
		}
		ArrayList<String> prev = perms(N-1,limit);
		ArrayList<String> ret = new ArrayList<String>();
		for (int i = 0; i < prev.size(); i++) {
			String cur = prev.get(i);
			boolean[] vis = new boolean[10];
			for (int j = 0; j < cur.length(); j++) {
				int dig = Integer.parseInt(cur.substring(j,j+1));
				vis[dig]=true;
			}
			if (!vis[1]) {
				for (int next = 0; next < limit; next++) {
					if (!vis[next]) {
						String add = cur;
						add+=Integer.toString(next);
						ret.add(add);
					}
				}
			}else ret.add(cur); //Just use previous end on 1 sequence	
		}
		return ret;
	}
	public static long ceil(long num, long den) {long ans = num/den; if (num%den!=0) 
	ans++; return ans;}
	public static long GCD(long a, long b) {
		if (a==0||b==0) return Math.max(a,b);
		return GCD(Math.min(a, b),Math.max(a, b)%Math.min(a, b));
	}
	public static long FastExp(long base, long exp, long mod) {
		long ans=1;
		while (exp>0) {
			if (exp%2==1) ans*=base;
			exp/=2;
			base*=base;
			base%=mod;
			ans%=mod;
		}
		return ans;
	}
	public static long ModInv(long num,long mod) {return FastExp(num,mod-2,mod);}
	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}
		int nextInt() {return Integer.parseInt(next());}
		long nextLong() {return Long.parseLong(next());};
	}
	static class OutPut{
		PrintWriter w = new PrintWriter(System.out);
		void pln(double x) {w.println(x);w.flush();}
		void pln(int x) {w.println(x);w.flush();}
		void pln(long x) {w.println(x);w.flush();}
		void pln(String x) {w.println(x);w.flush();}
		void pln(char x) {w.println(x);w.flush();}
		void pln(StringBuilder x) {w.println(x);w.flush();}
		void p(int x) {w.print(x);w.flush();}
		void p(long x) {w.print(x);w.flush();}
		void p(String x) {w.print(x);w.flush();}
		void p(char x) {w.print(x);w.flush();}
		void p(StringBuilder x) {w.print(x);w.flush();}
	}
}