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;
typedef pair<int, int> pii;
typedef long long ll;
const int MAXN = 101, MAXM = 9901, MAXK = 1001;
// const int MAXN = 51, MAXK = 51;
const ll INF = 1e18;
int N, M, K;
vector<pii> g[MAXN];
ll buy[MAXN][MAXK];
ll sell[MAXN][MAXK];
ll d[MAXN][MAXN];
ll gain[MAXN][MAXN];
// ll dist[2][MAXN];
// priority_queue<pair<int, ll>, vector<pair<int, ll>>, greater<pair<int, ll>>> q;
// void dijkstra(int start, ll d[], vector<pii> g[]) {
// for (int i = 1; i <= N; i++) d[i] = INF;
// q.push({0, start});
// d[start] = 0;
// while (!q.empty()) {
// ll dd; int cur;
// tie(dd, cur) = q.top();
// q.pop();
// if (dd > d[cur]) continue;
// for (pii p : g[cur]) {
// int nx = p.first;
// ll cost = p.second;
// if (dd + cost < d[nx]) {
// d[nx] = dd + cost;
// q.push({d[nx], nx});
// }
// }
// }
// }
ll dp[MAXN][MAXM];
bool ok[MAXN][MAXM];
ll f(int cur, int sum) {
if (cur == 1 && sum == 0) return 0;
else if (sum <= 0) return -INF;
else if (d[cur][1] > sum) return -INF;
ll &ret = dp[cur][sum];
if (ok[cur][sum]) return ret;
ok[cur][sum] = true;
ret = -INF;
for (int i = 1; i <= N; i++) {
if (i != cur) {
ret = max(ret, f(i, sum - d[cur][i]) + gain[cur][i]);
}
}
return ret;
}
int main() {
scanf("%d%d%d", &N, &M, &K);
for (int i = 1; i <= N; i++) {
for (int k = 1; k <= K; k++) {
scanf("%lld%lld", &buy[i][k], &sell[i][k]);
}
}
for (int i = 1; i <= N; i++) {
for (int k = 1; k <= N; k++) {
d[i][k] = INF;
}
d[i][i] = 0;
}
for (int i = 1,a,b,c; i <= M; i++) {
scanf("%d%d%d", &a, &b, &c);
g[a].push_back({b, c});
d[a][b] = c;
}
for (int k = 1; k <= N; k++) {
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
for (int k = 1; k <= K; k++) {
if (buy[i][k] != -1 && sell[j][k] != -1) {
gain[i][j] = max(gain[i][j], -buy[i][k] + sell[j][k]);
}
}
}
}
ll ans = 0;
for (int sum = 1; sum <= N*N; sum++) {
ans = max(ans, f(1, sum)/ll(sum));
}
// for (int sum = 0; sum <= N*N; sum++) {
// for (int i = 1; i <= N; i++) {
// printf("sum %d i %d: %lld\n", sum, i, f(i, sum));
// }
// }
// ll ans = 0;
// for (int i = 1; i <= N; i++) {
// ll dist = d[1][i] + d[i][1];
// if (dist > 0)
// ans = max(ans, gain[1][i]/dist);
// }
printf("%lld\n", ans);
}
Compilation message (stderr)
merchant.cpp: In function 'int main()':
merchant.cpp:65:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &N, &M, &K);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
merchant.cpp:68:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld", &buy[i][k], &sell[i][k]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
merchant.cpp:80:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &a, &b, &c);
~~~~~^~~~~~~~~~~~~~~~~~~~~~| # | 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... |