This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
const ll inf = 1e9L + 5, big = 1e18 + 5;
const int nmax = 105;
void fw(int n, ll dist[nmax][nmax]) {
for(int h = 0; h < n; h++) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++)
dist[i][j] = max(-big, min(dist[i][j], dist[i][h] + dist[h][j]));
}
}
}
ll dist[nmax][nmax], cost[nmax][nmax], alter[nmax][nmax];
ll buy[nmax][nmax], sell[nmax][nmax];
bool check(int n, int coef) {
//cerr << "Alter with coef " << coef << '\n';
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++)
alter[i][j] = min(big, -(cost[i][j] - dist[i][j] * coef));
}
fw(n, alter);
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) { // am dat rage si m-am uitat la modelcode, wtf??
if(alter[i][j] + alter[j][i] <= 0)
return 1;
}
}
return 0;
}
signed main() {
int n, m, k;
cin >> n >> m >> k;
for(int i = 0; i < n; i++) {
for(int j = 0; 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] = -inf;
}
}
for(int i = 0; i< n; i++)
for(int j = 0; j < n; j++)
dist[i][j] = inf;
for(int i = 0, x, y, c; i < m; i++)
cin >> x >> y >> c, --x, --y, dist[x][y] = min(dist[x][y], c);
fw(n, dist);
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cost[i][j] = 0;
for(int h = 0; h < k; h++)
cost[i][j] = max(cost[i][j], sell[j][h] - buy[i][h]);
}
}
int l = 0, r = inf, mid;
while(r - l > 1) {
mid = l + r >> 1;
if(check(n, mid))
l = mid;
else
r = mid;
}
cout << l << '\n';
}
Compilation message (stderr)
merchant.cpp: In function 'int main()':
merchant.cpp:64:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
64 | mid = l + r >> 1;
| ~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |