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 long long INF = 1LL << 60;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N, M, K;
cin >> N >> M >> K;
vector<vector<int>> B(N, vector<int>(K)), S(N, vector<int>(K));
for (int i = 0; i < N; i++) {
for (int j = 0; j < K; j++) {
cin >> B[i][j] >> S[i][j];
}
}
vector<vector<pair<int, int>>> G(N);
for (int i = 0; i < M; i++) {
int u, v, w;
cin >> u >> v >> w;
u--, v--;
G[u].push_back({v, w});
}
vector<vector<int>> profit(N, vector<int>(N));
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
if (i != j)
for (int k = 0; k < K; k++)
if (S[j][k] != -1 && B[i][k] != -1)
profit[i][j] = max(profit[i][j], S[j][k] - B[i][k]);
vector<vector<long long>> dist(N, vector<long long>(N, INF));
for (int u = 0; u < N; u++) {
dist[u][u] = 0;
for (auto [v, t] : G[u]) {
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]);
int l = 0, r = 1 << 30;
while (r > l + 1) {
int mid = (l + r) / 2;
vector<vector<long long>> cost(N, vector<long long>(N));
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (dist[i][j] == INF || i == j) {
cost[i][j] = INF;
} else {
cost[i][j] = 1LL * dist[i][j] * mid - profit[i][j];
}
}
}
for (int k = 0; k < N; k++)
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++) {
cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
cost[i][j] = max(cost[i][j], -INF);
cost[i][j] = min(cost[i][j], INF);
}
bool good = false;
for (int i = 0; i < N; i++) {
if (cost[i][i] <= 0) {
good = true;
}
}
if (good) {
l = mid;
} else {
r = mid;
}
}
cout << l << "\n";
}
# | 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... |