이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
int N, M, K;
int B[105][1005];
int S[105][1005];
int dist[105][105];
int profit[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 j = 1; j <= N; j++) {
dist[i][j] = 1e9 + 100;
}
}
for (int i = 0; i < M; i++) {
int u, v, t;
cin >> u >> v >> t;
dist[u][v] = t;
}
}
void FloydWarshall() {
for (int k = 1; k <= N; k++) {
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
if (dist[i][j] > dist[i][k] + dist[k][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}
for (int k = 1; k <= K; k++) {
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
if (S[j][k] == -1 || B[i][k] == -1) continue;
profit[i][j] = max(profit[i][j], S[j][k] - B[i][k]);
}
}
}
}
bool Check(int r) {
// profit / dist >= r -> profit - r * dist >= 0
long long chk[105][105];
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
if (i == j) {
chk[i][j] = -inf;
} else {
chk[i][j] = max(-inf, 1ll * profit[i][j] - 1ll * r * dist[i][j]);
}
}
}
for (int rep = 0; rep < 2; rep++) {
for (int k = 1; k <= N; k++) {
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
if (chk[i][j] < chk[i][k] + chk[k][j]) {
chk[i][j] = min(inf, chk[i][k] + chk[k][j]);
}
if (chk[j][j] >= 0) {
return true;
}
}
}
}
}
return false;
}
int main() {
Read();
FloydWarshall();
int lo = 0, hi = 1e9 + 100;
while (lo < hi) {
int mid = (lo + hi + 1) / 2;
if (Check(mid)) {
lo = mid;
} else {
hi = mid -1;
}
}
cout << lo << "\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... |