Submission #409309

#TimeUsernameProblemLanguageResultExecution timeMemory
409309tranxuanbachTravelling Merchant (APIO17_merchant)C++17
0 / 100
139 ms1228 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define endl '\n'
#define fi first
#define se second
#define For(i, l, r) for (int i = l; i < r; i++)
#define ForE(i, l, r) for (int i = l; i <= r; i++)
#define FordE(i, l, r) for (int i = l; i >= r; i--)
#define Fora(v, a) for (auto v: a)
#define bend(a) a.begin(), a.end()
#define isz(a) ((signed)a.size())

typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef vector <int> vi;
typedef vector <pii> vpii;
typedef vector <vi> vvi;

const int N = 1e2 + 5, K = 1e3 + 5, inf = 1e9 + 7;
const ll infll = (ld)1e18 + 7;

int n, m, k;
int b[N][K], s[N][K];
ll adj[N][N];

ll dist[N][N];

void floyd_warshall(){
    ForE(k, 1, n){
        ForE(i, 1, n){
            ForE(j, 1, n){
                if (dist[i][j] > dist[i][k] + dist[k][j]){
                    dist[i][j] = dist[i][k] + dist[k][j];
                }
            }
        }
    }
}

ll profit[N][N];

signed main(){
    // ios_base::sync_with_stdio(0);
    // cin.tie(0); cout.tie(0);
    // freopen("KEK.inp", "r", stdin);
    // freopen("KEK.out", "w", stdout);
    cin >> n >> m >> k;
    ForE(i, 1, n){
        ForE(j, 1, k){
            cin >> b[i][j] >> s[i][j];
        }
    }
    ForE(i, 1, n){
        ForE(j, 1, n){
            adj[i][j] = infll;
        }
    }
    ForE(i, 1, m){
        int u, v, w; cin >> u >> v >> w;
        adj[u][v] = w;
    }
    ForE(i, 1, n){
        ForE(j, 1, n){
            dist[i][j] = adj[i][j];
        }
    }
    floyd_warshall();
    ForE(i, 1, n){
        ForE(j, 1, n){
            adj[i][j] = dist[i][j];
        }
    }
    ForE(i, 1, n){
        ForE(j, 1, n){
            ForE(kk, 1, k){
                if (b[i][kk] != -1 and s[j][kk] != -1){
                    profit[i][j] += max(0, s[j][kk] - b[i][kk]);
                }
            }
        }
    }
    int lo = 0, hi = inf;
    while (lo < hi){
        int mid = (lo + hi + 1) >> 1;
        ForE(i, 1, n){
            ForE(j, 1, n){
                dist[i][j] = (ll)mid * min(adj[i][j], infll / mid) - profit[i][j];
            }
        }
        floyd_warshall();
        bool ck = 0;
        ForE(i, 1, n){
            if (dist[i][i] <= 0){
                ck = 1;
            }
        }
        if (ck){
            lo = mid;
        }
        else{
            hi = mid - 1;
        }
    }
    cout << lo << endl;
}

/*
==================================================+
INPUT:                                            |
--------------------------------------------------|

--------------------------------------------------|
==================================================+
OUTPUT:                                           |
--------------------------------------------------|

--------------------------------------------------|
==================================================+
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...