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;
const int inf = 1e9;
int N, M, K;
int B[105][1005], S[105][1005];
int dst[105][105];
int prv[105][105];
void Read() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> N >> M >> K;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= K; j++) {
cin >> B[i][j] >> S[i][j];
}
}
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
dst[i][j] = inf;
prv[i][j] = -1;
}
}
for (int i = 0; i < M; i++) {
int u, v, t;
cin >> u >> v >> t;
dst[u][v] = t;
prv[u][v] = u;
}
}
void FloydWarshall() {
for (int k = 1; k <= N; k++) {
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
if (dst[i][j] > dst[i][k] + dst[k][j]) {
dst[i][j] = dst[i][k] + dst[k][j];
prv[i][j] = prv[k][j];
}
}
}
}
}
long long OptimalCost(vector<int> a) {
int n = a.size();
long long dp[n + 1][K + 1];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= K; j++) {
dp[i][j] = -1e18;
}
}
dp[0][0] = 0;
for (int i = 0; i < n; i++) {
for (int j = 1; j <= K; j++) {
if (S[a[i]][j] == -1) continue;
dp[i][0] = max(dp[i][0], dp[i][j] + S[a[i]][j]);
}
for (int j = 1; j <= K; j++) {
if (B[a[i]][j] == -1) continue;
dp[i + 1][j] = max(dp[i + 1][j], dp[i][0] - B[a[i]][j]);
}
for (int j = 0; j <= K; j++) {
dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
}
}
return dp[n][0];
}
long long LoopCost(int n) {
if (dst[n][n] == inf) return 0;
vector<int> ls;
int cur = n;
ls.emplace_back(n);
do {
ls.emplace_back(prv[n][cur]);
cur = prv[n][cur];
} while (cur != n);
reverse(begin(ls), end(ls));
return OptimalCost(ls) / dst[n][n];
}
int main() {
Read();
FloydWarshall();
long long ans = 0;
for (int i = 1; i <= N; i++) {
ans = max(ans, LoopCost(i));
}
cout << ans << "\n";
return 0;
}
# | 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... |