Submission #630112

#TimeUsernameProblemLanguageResultExecution timeMemory
630112Do_you_copyTravelling Merchant (APIO17_merchant)C++17
0 / 100
86 ms2144 KiB
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define pb push_back
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
using ll = long long;
using ull = unsigned ll;
using ld = long double;
using pii = pair <int, int>;
using pil = pair <int, ll>;
using pli = pair <ll, int>;
using pll = pair <ll, ll>;
mt19937 Rand(chrono::steady_clock::now().time_since_epoch().count());

ll min(const ll &a, const ll &b){
    return (a < b) ? a : b;
}

ll max(const ll &a, const ll &b){
    return (a > b) ? a : b;
}

//const ll Mod = 1000000007;
//const ll Mod2 = 999999999989;
//only use when required
const int maxN = 102;
const int maxM = 9901;
const int maxK = 1001;
const ll inf = 0x3f3f3f3f3f3f3f3f;
int n, m, K;
int buy[maxN][maxK];
int sell[maxN][maxK];
vector <vector <ll>> d, profit;
void Floyd_Warshall(vector <vector <ll>> &dist){
    for (int k = 1; k <= n; ++k){
        for (int i = 1; i <= n; ++i){
            for (int j = 1; j <= n; ++j){
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
            }
        }
    }
}

bool has_negative_cycle(int x){
    vector <vector <ll>> tem(n + 1, vector <ll> (n + 1));
    for (int i = 1; i <= n; ++i){
        for (int j = 1; j <= n; ++j){
            tem[i][j] = x * min(d[i][j], inf / x) - profit[i][j];
            if (x == 1) cerr << d[i][j] << "\n";
        }
    }
    Floyd_Warshall(tem);
    for (int i = 1; i <= n; ++i){
        if (tem[i][i] <= 0) return 1;
    }
    return 0;
}

void Init(){
    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];
    }
    d.resize(n + 1, vector <ll> (n + 1, inf));
    profit.resize(n + 1, vector <ll> (n + 1, 0));
    for (int i = 1; i <= n; ++i){
        for (int j = 1; j <= n; ++j){
            for (int k = 1; k <= n; ++k){
                if (buy[i][k] != -1 && sell[j][k] != -1){
                    profit[i][j] = max(profit[i][j], sell[j][k] - buy[i][k]);
                }
            }
        }
    }
    for (int i = 1; i <= m; ++i){
        int u, v, w;
        cin >> u >> v >> w;
        d[u][v] = min(d[u][v], w);
    }
    Floyd_Warshall(d);
    int l = 1, r = 0x3f3f3f3f;
    while (l <= r){
        int mid = (l + r) / 2;
        if (has_negative_cycle(mid)) l = mid + 1;
        else r = mid - 1;
    }
    cout << r;
}

#define debu
#define taskname "test"
signed main(){
    if (fopen(taskname".inp", "r")){
        freopen(taskname".inp", "r", stdin);
        #ifdef debug
        freopen(taskname".out", "w", stdout);
        #endif
    }
    faster;
    ll tt = 1;
    //cin >> tt;
    while (tt--){
        Init();
    }
    if (fopen("timeout.txt", "r")){
        ofstream timeout("timeout.txt");
        timeout << 1000 * double(clock()) / CLOCKS_PER_SEC;
        #ifndef debug
        cerr << "\nTime elapsed: " << 1000 * double(clock()) / CLOCKS_PER_SEC << "ms\n";
        #endif
    }
}

Compilation message (stderr)

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