Submission #1324238

#TimeUsernameProblemLanguageResultExecution timeMemory
1324238sh_qaxxorov_571Travelling Merchant (APIO17_merchant)C++20
100 / 100
109 ms2240 KiB
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long ll;
const ll INF = 1e16;

int n, m, k;
ll buy[105][1005], sell[105][1005];
ll dist[105][105], max_profit[105][105];

bool check(ll x) {
    vector<vector<ll>> g(n + 1, vector<ll>(n + 1, -INF));
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            if (i != j && dist[i][j] != INF)
                g[i][j] = max_profit[i][j] - x * dist[i][j];

    // Floyd-Warshall musbat siklni tekshirish uchun
    for (int p = 1; p <= n; p++)
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                g[i][j] = max(g[i][j], g[i][p] + g[p][j]);

    for (int i = 1; i <= n; i++)
        if (g[i][i] >= 0) return true;
    return false;
}

int main() {
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= k; j++) cin >> buy[i][j] >> sell[i][j];
    }
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++) dist[i][j] = (i == j ? 0 : INF);

    for (int i = 0; i < m; i++) {
        int u, v; ll t; cin >> u >> v >> t;
        dist[u][v] = min(dist[u][v], t);
    }

    // 1. Eng qisqa yo'llar
    for (int p = 1; p <= n; p++)
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                dist[i][j] = min(dist[i][j], dist[i][p] + dist[p][j]);

    // 2. Maksimal foyda matritsasi
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            max_profit[i][j] = 0;
            if (i == j || dist[i][j] == INF) continue;
            for (int p = 1; p <= k; p++) {
                if (buy[i][p] != -1 && sell[j][p] != -1)
                    max_profit[i][j] = max(max_profit[i][j], sell[j][p] - buy[i][p]);
            }
        }
    }

    // 3. Binar qidiruv
    ll low = 0, high = 1e9, ans = 0;
    while (low <= high) {
        ll mid = (low + high) / 2;
        if (check(mid)) { ans = mid; low = mid + 1; }
        else high = mid - 1;
    }
    cout << ans << endl;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...