# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
210713 | anonymous | Travelling Merchant (APIO17_merchant) | C++14 | 121 ms | 3452 KiB |
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<iostream>
#define MAXN 105
#define MAXK 1005
#define LL long long
using namespace std;
int N, M, K, ui, vi, wi;
int buy[MAXN][MAXK], sell[MAXN][MAXK];
LL dist[MAXN][MAXN], best[MAXN][MAXN];
LL adj[MAXN][MAXN];
bool can(LL k) {
for (int i=1; i<=N; i++) {
for (int j=1; j<=N; j++) {
adj[i][j] = best[i][j] - k*dist[i][j];
}
} //is there a non-negative cycle?
for (int m=1; m<=N; m++) {
for (int i=1; i<=N; i++) {
for (int j=1; j<=N; j++) {
adj[i][j]=max(adj[i][j], adj[i][m] + adj[m][j]);
}
}
}
for (int i=1; i<=N; i++) {
for (int j=1; j<=N; j++) {
if (i != j && adj[i][j] + best[j][i] - k*dist[j][i] >= 0) {return(true);}
}
}
return(false);
}
int main() {
//freopen("merchin.txt","r",stdin);
scanf("%d %d %d",&N,&M,&K);
for (int i=1; i<=N; i++) {
for (int j=1; j<=K; j++) {
scanf("%d %d", &buy[i][j], &sell[i][j]);
}
}
for (int i=1; i<=N; i++) {
for (int j=1; j<=N; j++) {
if (i != j) {dist[i][j] = 1LL << 31;}
}
}
for (int i=0; i<M; i++) {
scanf("%d %d %d",&ui,&vi,&wi);
dist[ui][vi]=wi;
}
for (int k=1; k<=N; k++) {
for (int i=1; i<=N; i++) {
for (int j=1; j<=N; j++) {
dist[i][j]=min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
for (int i=1; i<=N; i++) {
for (int j=1; j<=N; j++) {
for (int k=1; k<=K; k++) {
if (sell[j][k] == -1 || buy[i][k] == -1) {continue;}
best[i][j] = max(best[i][j], (LL) sell[j][k]-buy[i][k]);
}
}
}
int lo = 0, hi = (int) 1e9 + 5;
while (lo + 1 != hi) {
int mid = (lo + hi) >> 1;
if (can(mid)) {lo = mid;}
else {hi = mid;}
}
printf("%d\n", lo);
}
Compilation message (stderr)
# | 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... |