Submission #768517

#TimeUsernameProblemLanguageResultExecution timeMemory
768517raysh07Travelling Merchant (APIO17_merchant)C++17
100 / 100
74 ms4432 KiB
#include <bits/stdc++.h> using namespace std; #define int long long #define INF (int)1e18 #define f first #define s second mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count()); int n, m, k; const int N = 101; const int K = 1e3 + 1; int buy[N][K]; int sell[N][K]; vector <pair<int, int>> adj[N]; int floyd[N][N]; int best[N][N]; bool check(int x){ vector<vector<int>> ok(n + 1, vector<int>(n + 1, -INF)); for (int i = 1; i <= n; i++){ for (int j = 1; j <= n; j++){ if (floyd[i][j] == INF) continue; ok[i][j] = max(ok[i][j], best[i][j] - x * floyd[i][j]); } } for (int k = 1; k <= n; k++){ for (int i = 1; i <= n; i++){ for (int j = 1; j <= n; j++){ ok[i][j] = max(ok[i][j], ok[i][k] + ok[k][j]); if (ok[i][j] > INF) ok[i][j] = INF; } } } for (int i = 1; i <= n; i++){ for (int j = 1; j <= n; j++){ if (i != j && ok[i][j] + ok[j][i] >= 0) return true; } } return false; } void Solve() { 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]; if (buy[i][j] == -1) buy[i][j] = INF; if (sell[i][j] == -1) sell[i][j] = 0; } } for (int i = 0; i < N; i++){ for (int j = 0; j < N; j++){ floyd[i][j] = INF; } } for (int i = 1; i <= m; i++) { int u, v, w; cin >> u >> v >> w; adj[u].push_back({v, w}); floyd[u][v] = w; } for (int k = 1; k <= n; k++){ for (int i = 1; i <= n; i++){ for (int j = 1; j <= n; j++){ floyd[i][j] = min(floyd[i][j], floyd[i][k] + floyd[k][j]); } } } for (int i = 1; i <= n; i++){ for (int j = 1; j <= n; j++){ for (int i1 = 1; i1 <= k; i1++){ best[i][j] = max(best[i][j], sell[j][i1] - buy[i][i1]); } } } int lo = 0, hi = 1e9; while (lo != hi){ int mid = (lo + hi + 1) / 2; if (check(mid)) lo = mid; else hi = mid - 1; } cout << lo << "\n"; } int32_t main() { auto begin = std::chrono::high_resolution_clock::now(); ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; // cin >> t; for(int i = 1; i <= t; i++) { //cout << "Case #" << i << ": "; Solve(); } auto end = std::chrono::high_resolution_clock::now(); auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin); cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\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...