Submission #406365

#TimeUsernameProblemLanguageResultExecution timeMemory
406365aryan12Travelling Merchant (APIO17_merchant)C++17
100 / 100
98 ms4296 KiB
#include <bits/stdc++.h> using namespace std; #define int long long mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count()); const int N = 110, K = 1010; int nodes, edges, items; int buy[N][K], sell[N][K]; int value[N][N], dist[N][N]; bool Check(long long x) { long long efficiency[N][N]; for(int i = 0; i < N; i++) { for(int j = 0; j < N; j++) { efficiency[i][j] = -1e16; } } for(int i = 1; i <= nodes; i++) { for(int j = 1; j <= nodes; j++) { if(dist[i][j] != 1e16) { efficiency[i][j] = max(efficiency[i][j], value[i][j] - dist[i][j] * x); } } } for(int k = 1; k <= nodes; k++) { for(int i = 1; i <= nodes; i++) { for(int j = 1; j <= nodes; j++) { efficiency[i][j] = max(efficiency[i][j], efficiency[i][k] + efficiency[k][j]); } } } /*cout << "Outputing the efficiency\n"; for(int i = 1; i <= nodes; i++) { for(int j = 1; j <= nodes; j++) { cout << efficiency[i][j] << " "; } cout << "\n"; }*/ for(int i = 1; i <= nodes; i++) { if(efficiency[i][i] >= 0) return true; } return false; } void Solve() { for(int i = 0; i < N; i++) { for(int j = 0; j < N; j++) { dist[i][j] = 1e16; value[i][j] = 0; } } cin >> nodes >> edges >> items; for(int i = 1; i <= nodes; i++) { for(int j = 1; j <= items; j++) { cin >> buy[i][j] >> sell[i][j]; } } for(int i = 1; i <= edges; i++) { int v, u, w; cin >> v >> u >> w; dist[v][u] = w; } for(int k = 1; k <= nodes; k++) { for(int i = 1; i <= nodes; i++) { for(int j = 1; j <= nodes; j++) { dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } } } for(int item_number = 1; item_number <= items; item_number++) { for(int buy_node = 1; buy_node <= nodes; buy_node++) { for(int sell_node = 1; sell_node <= nodes; sell_node++) { if(buy[buy_node][item_number] != -1 && sell[sell_node][item_number] != -1 && dist[buy_node][sell_node] != 1e15) { value[buy_node][sell_node] = max(value[buy_node][sell_node], sell[sell_node][item_number] - buy[buy_node][item_number]); } } } } /*cout << "Minimum distance to travel from a pair of indices (i, j) are as follows: \n"; for(int i = 1; i <= nodes; i++) { for(int j = 1; j <= nodes; j++) { cout << dist[i][j] << " "; } cout << "\n"; } cout << "Maximum value which can be gained when travelling from i to j is as follows: \n"; for(int i = 1; i <= nodes; i++) { for(int j = 1; j <= nodes; j++) { cout << value[i][j] << " "; } cout << "\n"; }*/ int ans = 0; int left = 0, right = 1e10; while(left <= right) { int mid = (left + right) >> 1; if(Check(mid)) { ans = mid; left = mid + 1; } else { right = mid - 1; } } cout << ans << "\n"; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); Solve(); 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...