TalentnotDefined's blog

By TalentnotDefined, history, 15 months ago, In English

Can anyone please tell me mistake in my code for given problem.

Consider a network consisting of n computers and m connections. Each connection specifies how fast a computer can send data to another computer.

Kotivalo wants to download some data from a server. What is the maximum speed he can do this, using the connections in the network?

Input

The first input line has two integers n and m : the number of computers and connections. The computers are numbered 1,2,…,n . Computer 1 is the server and computer n is Kotivalo's computer.

After this, there are m lines describing the connections. Each line has three integers a , b and c : computer a can send data to computer b at speed c .

Output

Print one integer: the maximum speed Kotivalo can download data.

Constraints 1≤n≤500

1≤m≤1000

1≤a,b≤n

1≤c≤109

Example

Input: 4 5 1 2 3 2 4 2 1 3 4 3 4 5 4 1 3

Output: 6

Problem Link : https://cses.fi/problemset/task/1694/

My Code :

int solveRec(int i, int j, vector<vector<pi>> &graph, vector<vector<int>> &dp, int n)
{
    if (i == j)
        return dp[i][j] = 1e18;
    if (dp[i][j] != -1)
        return dp[i][j];
    int ans = 0;
    for (auto x : graph[i])
    {
        ans += min(x.second, solveRec(x.first, j, graph, dp, n));
    }
    return dp[i][j] = ans;
}

auto solve = []()
{
    int n, k;
    cin >> n >> k;
    vector<vector<pi>> graph(n + 1);
    for (int i = 0; i < k; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        graph[a].push_back({b, c});
    }
    vector<vector<int>> dp(n + 1, vector<int>(n + 1, -1));
    vector<int> vis(n + 1, 0);
    int ans = solveRec(1, n, graph, dp, n);
    cout << ans << endl;
};
  • Vote: I like it
  • -9
  • Vote: I do not like it

»
15 months ago, # |
  Vote: I like it 0 Vote: I do not like it