-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathShortest Path Exactly K.java
More file actions
63 lines (63 loc) · 1.12 KB
/
Shortest Path Exactly K.java
File metadata and controls
63 lines (63 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.util.Scanner;
class Shortest_Path_Exact_K
{
int v;
int g[][];
static final int INF=100000;
Shortest_Path_Exact_K(int v)
{
this.v=v;
g=new int[v][v];
for(int i=0;i<v;i++)
{
for(int j=0;j<v;j++)
{
if(i==j)
g[i][j]=0;
else
g[i][j]=INF;
}
}
}
void addEdge(int src,int dest,int weight)
{
g[src][dest]=weight;
}
int path(int u,int v,int k)
{
if(k==0&&u==v)
return 0;
if(k==1&&g[u][v]!=INF)
return g[u][v];
if(k<=0)
return INF;
int res=INF;
for(int i=0;i<v;i++)
{
if(g[u][i]!=INF&&u!=i&&v!=i)
{
int rec_res=path(i,v,k-1);
if(g[u][i]!=INF)
res=Math.min(res,g[u][i]+rec_res);
}
}
return res;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int v=sc.nextInt();
int e=sc.nextInt();
Shortest_Path_Exact_K graph=new Shortest_Path_Exact_K(v);
for(int i=1;i<=e;i++)
{
int src=sc.nextInt();
int dest=sc.nextInt();
int weight=sc.nextInt();
graph.addEdge(src,dest,weight);
}
int src=sc.nextInt();
int dest=sc.nextInt();
int k=sc.nextInt();
System.out.println(graph.path(src,dest,k));
}
}