0

I am solving this problem https://www.acmicpc.net/problem/1238#

You can change the language by clicking the button enter image description here

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
logan shin
  • 43
  • 2
  • 6
  • 3
    Can you post a quick summary of the problem statement please? I'm not seeing the translate button, and Google translate doesn't provide something clear enough for me. – IVlad Feb 27 '17 at 12:31
  • since this question is taged as 'C', what is this 'C++' statement doing in the code: `using namespace std;` which will cause a c compiler to raise an ERROR message. – user3629249 Mar 01 '17 at 04:36
  • The posted code does not compile. That is because it contains C++ statements in (per the tag) C code. – user3629249 Mar 01 '17 at 04:45
  • The posted code could be greatly faster by making the working variables all `size_t` rather than `int` and using the function: `getchar_unlocked()` and putchar_unlocked() in appropriate algorithms rather than the (time and memory expensive calls to `scanf()` and `printf()` – user3629249 Mar 01 '17 at 04:53
  • for ease of readability and understaning: 1) follow the axiom: *only one statement per line and (at most) one variable declaration per statement.* 2) variable names should indicate `usage` or `content` (or better, both) Single character variable names offer no meaning and (except for subscript names) should not be used. – user3629249 Mar 01 '17 at 05:00
  • the algorithm in the `solve()` function seems to be looking for the longest route rather than the shortest route. Probably not what you actually want to calculate. – user3629249 Mar 01 '17 at 05:03
  • in C, array indexes begin with 0 and end in (number of elements in array -1) However the posted code is referencing the first element past the end of the array. – user3629249 Mar 01 '17 at 05:11

1 Answers1

0

The complexity of the Floyd-Warshall algorithm is O(n3). It will be TL.

The solution to this task is to doing SSSP (single source shortest path). It can do effectively with Dijkstra's Algorithm. You can do Dijkstra's Algorithm on farm X then doing it again with edges reversed. It has O(m logn) complexity if you write it with priority queues or set. Google for this, there are many information, for example 1, 2.

After this your idea is right, you should take maximum of sum D[i][X] + D[i][X] for each farm i. Though you have written 2 instead of x, but it is probably a typo, or to test code.

Dmitrii Kurylev
  • 407
  • 1
  • 8
  • 17