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>
#define long long long
using namespace std;
const int N = 100;
const int T = 1000;
int n, m, t;
long a[N][T], b[N][T];
long d[N][N], f[N][N];
long e[N][N];
bool valid(int x) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) {
e[i][j] = numeric_limits<long>::min();
} else {
if (d[i][j] < numeric_limits<long>::max()) {
e[i][j] = f[i][j] - d[i][j] * x;
} else {
e[i][j] = numeric_limits<long>::min();
}
}
}
}
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
if (e[i][i] >= 0) {
return 1;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (e[i][k] > numeric_limits<int>::min() &&
e[k][j] > numeric_limits<int>::min()) {
e[i][j] = max(e[i][j], e[i][k] + e[k][j]);
}
}
}
}
return 0;
}
void solve() {
cin >> n >> m >> t;
for (int i = 0; i < n; i++) {
for (int j = 0; j < t; j++) {
cin >> a[i][j] >> b[i][j];
}
}
for (int i = 0; i < n; i++) {
fill(d[i], d[i] + n, numeric_limits<long>::max());
d[i][i] = 0;
}
for (int i = 0; i < m; i++) {
int u, v;
long w;
cin >> u >> v >> w;
u = u - 1, v = v - 1;
d[u][v] = min(d[u][v], w);
}
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (d[i][k] < numeric_limits<long>::max() &&
d[k][j] < numeric_limits<long>::max()) {
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
}
// f[i][j] 表示 i 直接到 j 最大收益是多少
// 如果不连通,为 min
// 否则,最小值是 0
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (d[i][j] == numeric_limits<long>::max()) {
f[i][j] = numeric_limits<long>::min();
}
for (int k = 0; k < t; k++) {
if (b[j][k] < 0 || a[i][k] < 0) {
continue;
}
f[i][j] = max(f[i][j], b[j][k] - a[i][k]);
}
}
}
int l = 0, r = (int) 1E9;
while (l <= r) {
int c = (l + r) / 2;
if (valid(c)) {
l = c + 1;
} else {
r = c - 1;
}
}
cout << r << "\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(nullptr);
solve();
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... |