I am solving this problem https://www.acmicpc.net/problem/1238#
You can change the language by clicking the button
The idea I came up with is to find the sum of the shortest distance from the Kth to the second and the second to the Kth
so here is my whole source code
#include <stdio.h>
#define INF 999999
#define min(x,y) ((x)>(y)?(y):(x))
using namespace std;
int ans = 0;
int n,m,x;
int d[1001][1001];
void Floyd_Warshal(){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
if(i==j) d[i][j]=0;
}
}
for(int k=1; k<=n; k++){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
}
void solve(){
for(int i=1; i<=n; i++){
if(i==2) continue;
if(d[i][2] + d[2][i]>ans) ans = d[i][2] + d[2][i];
//printf("%d = %d+%d \n",ans,d[i][2],d[2][i]);
}
}
int main(){
scanf("%d %d %d",&n,&m,&x);
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++) d[i][j] = INF;
}
for(int i=0; i<m; i++){
int u,v,t;
scanf("%d %d %d",&u,&v,&t);
d[u][v] = t;
}
Floyd_Warshal();
solve();
printf("%d\n",ans);
return 0;
}
I think Floyd_warshal() function is fine.
But, I guess I take a wrong approach(The idea suggested above) to solve the problem so I just wanna ask that my idea is right approach to solve the problem or not.