제출 #412743

#제출 시각아이디문제언어결과실행 시간메모리
412743fvogel499여행하는 상인 (APIO17_merchant)C++14
0 / 100
225 ms34524 KiB
/* File created on 05/27/2021 at 13:38:24. Link to problem: https://oj.uz/problem/view/APIO17_merchant Description: plotting a graph then running binary search and executing floyd warshall's algorithm Time complexity: O(N^3) Space complexity: O(N^2) Status: DEV Copyright: Ⓒ 2021 Francois Vogel */ #include <iostream> #include <cmath> #include <vector> #include <bitset> #include <queue> #include <cstring> #include <set> #include <unordered_set> #include <map> #include <unordered_map> #include <algorithm> using namespace std; #define mp pair<pii, pii> #define pii pair<int, int> #define f first #define s second #define int ll #define ll long long const int inf = 1e9; int n; vector<mp> edges; int maxM = -1; void bs(int start, int end) { if (start <= end) { int mid = (start+end)/2; int dist [n][n]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) dist[i][j] = inf; for (mp e : edges) dist[e.f.f][e.f.s] = min(dist[e.f.f][e.f.s], e.s.f*mid-e.s.s); for (int k = 0; k < n; k++) for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) dist[i][j] = min(dist[i][j], dist[i][k]+dist[k][j]); bool negativeCycle = false; for (int i = 0; i < n; i++) if (dist[i][i] <= 0) negativeCycle = true; if (negativeCycle) { maxM = max(maxM, mid); bs(mid+1, end); } else bs(start, mid-1); } } signed main() { cin.tie(0); // ios_base::sync_with_stdio(0); int nbEdges, nbItems; cin >> n >> nbEdges >> nbItems; int buy [n][nbItems]; int sell [n][nbItems]; for (int i = 0; i < n; i++) for (int j = 0; j < nbItems; j++) cin >> buy[i][j] >> sell[i][j]; int dist [n][n]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) dist[i][j] = inf; for (int i = 0; i < n; i++) dist[i][i] = 0; for (int i = 0; i < nbEdges; i++) { int u, v, t; cin >> u >> v >> t; u--; v--; dist[u][v] = min(dist[u][v], t); } for (int k = 0; k < n; k++) for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) dist[i][j] = min(dist[i][j], dist[i][k]+dist[k][j]); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < nbItems; k++) { if (dist[i][j] != inf and buy[i][k] != -1 and sell[j][k] != -1) { mp e; e.f = pii(i, j); e.s = pii(dist[i][j], sell[j][k]-buy[i][k]); edges.push_back(e); } } bs(0, inf); cout << maxM+1; int d = 0; d++; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...