Submission #913987

#TimeUsernameProblemLanguageResultExecution timeMemory
913987tvladm2009Travelling Merchant (APIO17_merchant)C++17
0 / 100
55 ms3416 KiB
#include <iostream>
#include <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <deque>
#include <random>
 
using namespace std;
template<typename T1, typename T2> inline void chkmin(T1 &a, T2 b) {if (a > b) a = b;}
template<typename T1, typename T2> inline void chkmax(T1 &a, T2 b) {if (a < b) a = b;}
#define files(FILENAME) read(FILENAME); write(FILENAME)
#define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin)
#define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout)
#define all(c) (c).begin(), (c).end()
#define sz(c) (int)(c).size()
#define left left228
#define right right228
#define y1 y1228
#define mp make_pair
#define pb push_back
#define y2 y2228
#define rank rank228
using ll = long long;
using ld = long double; 
const string FILENAME = "input";
const int NMAX = 100 + 7;
const int KMAX = 1228;
const ll inf = 1e18;
 
int n, m, k;
int buy[NMAX][KMAX], sell[NMAX][KMAX];
ll dist[NMAX][NMAX], profit[NMAX][NMAX];
ll dp[NMAX][NMAX];
 
bool check(ll e) { 
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (i == j) {
                dp[i][j] = 0;
            } else if ((ll) dist[i][j] * e <= inf) {
                dp[i][j] = profit[i][j] - (ll) dist[i][j] * e;
            } else {
                dp[i][j] = -inf;
            }
        }
    }
    for (int p = 1; p <= n; p++) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                dp[i][j] = max(dp[i][j], dp[i][p] + dp[p][j]);
                if (dp[i][j] > inf) {
                    return true;
                }
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (i != j && dp[i][j] + dp[j][i] >= 0) {
                return true;
            }
        }
    }
    return false;
}
 
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	//read(FILENAME);
	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++) {
            if (i == j) {
                dist[i][j] = 0;
            } else {
                dist[i][j] = inf;
            }
        }
    }
    for (int i = 1; i <= m; i++) {
        int x, y, c;
        cin >> x >> y >> c;
        dist[x][y] = c;
    }
    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]);
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            for (int item = 1; item <= k; item++) {
                if (sell[j][item] != -1 && buy[i][item] != -1) {
                    profit[i][j] = max(profit[i][j], (ll) sell[j][item] - buy[i][item]);
                }
            }
        }
    }
    int ans = 0;
    for (int jump = (1 << 29); jump > 0; jump >>= 1) {
        if (check(ans + jump)) {
            ans += jump;
        }
    }
    cout << ans << '\n';
    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...