Submission #1251954

#TimeUsernameProblemLanguageResultExecution timeMemory
1251954CodeLakVNTravelling Merchant (APIO17_merchant)C++20
0 / 100
81 ms1348 KiB
#include <bits/stdc++.h>
using namespace std;

#define task "main"
#define F first
#define S second
#define ii pair<int, int>
#define il pair<int, long long>
#define li pair<long long, int>
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)

template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b) {a = b; return true;}
        return false;
    }

template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b) {a = b; return true;}
        return false;
    }

template <class T>
    void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
        for(auto item: container) out << item << separator;
        out << finish;
    }

const int MAX_N = 102;
const int MAX_M = 9902;
const int MAX_K = 1002;

int nNode, nEdge, nItem;
vector<ii> adj[MAX_N];
int buy[MAX_K][MAX_N], sell[MAX_K][MAX_N];
int maxProfit[MAX_N][MAX_N]; // maxProfit[i][j]: maximum profit when buy an item at market i and sell it at market j

long long initDist[MAX_N][MAX_N], curDist[MAX_N][MAX_N];

void floyd(long long dist[MAX_N][MAX_N]) {
    FOR(k, 1, nNode) FOR(u, 1, nNode) FOR(v, 1, nNode) {
        minimize(dist[u][v], dist[u][k] + dist[k][v]);
    }
}

void findMaxProfit() {
    memset(maxProfit, 0, sizeof(maxProfit));
    FOR(i, 1, nNode) FOR(j, 1, nNode) if (i != j) FOR(item, 1, nItem)
        if (buy[item][i] != -1 && sell[item][j] != -1)
            maximize(maxProfit[i][j], -buy[item][i] + sell[item][j]);
}

bool valid(long long val) {
    memset(curDist, 0x3f, sizeof(curDist));
    FOR(u, 1, nNode) FOR(v, 1, nNode) curDist[u][v] = initDist[u][v] * val - maxProfit[u][v];
    floyd(curDist);
    FOR(u, 1, nNode) if (curDist[u][u] <= 0) return true;
    return false;
}

void solve() {
    cin >> nNode >> nEdge >> nItem;
    FOR(i, 1, nNode) FOR(j, 1, nItem)
        cin >> buy[j][i] >> sell[j][i];
    memset(initDist, 0x3f, sizeof(initDist));
    FOR(i, 1, nEdge) {
        int u, v, w;
        cin >> u >> v >> w;
        initDist[u][v] = w;
    }

    floyd(initDist);
    findMaxProfit();

    long long l = 0, r = (long long)1e9, res = 0;
    while (l <= r) {
        long long mid = (l + r) >> 1;
        if (valid(mid)) l = (res = mid) + 1;
        else r = mid - 1;
    }

    cout << res << "\n";
}

int32_t main() {
    if (fopen(task".inp", "r")) {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    bool multitest = 0;
    int numTest = 1;
    if (multitest) cin >> numTest;

    while (numTest--) {
        solve();
    }

    return 0;
}

/* Lak lu theo dieu nhac!!!! */

Compilation message (stderr)

merchant.cpp: In function 'int32_t main()':
merchant.cpp:89:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   89 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
merchant.cpp:90:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   90 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...