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;
long long inf = 1000000000000000ll;
int main()
{
int n, m, t;
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> t;
static long long buy[105][1005];
static long long sell[105][1005];
static long long dist[105][105];
static long long profit[105][105];
for(int i = 0;i < n;i++){
for(int j = 0;j < n;j++){
buy[i][j] = inf;
sell[i][j] = inf;
profit[i][j] = 0;
dist[i][j] = inf;
}
}
for(int i = 0;i < n;i++){
for(int k = 0;k < t;k++){
cin >> buy[i][k];
cin >> sell[i][k];
}
}
///Max Profit btw two nodes
for(int i = 0;i < n;i++){
for(int j = 0;j < n;j++){
for(int k = 0;k < t;k++){
if(buy[i][k] != -1 && sell[j][k] != -1)
profit[i][j] = max(profit[i][j],sell[j][k] - buy[i][k]);
}
}
}
///adjMat
for(int i = 0;i < m;i++){
int a, b, c;
cin >> a >> b >> c;
a--;
b--;
dist[a][b] = c;
}
///Floyd
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]);
}
}
}
///p/d >= v
///p - dv >= 0 for a cycle, where p is total profit, d is total distance
///monotonous in terms of v
static long long diff[105][105];
long long low = 0ll;
long long high = inf;
while(true){
if(low == high - 1) break;
long long v = (low + high) / 2ll;
for(int i =0 ;i < n;i++){
for(int j = 0;j < n;j++){
///new graph with edge weight = p - dv
diff[i][j] = profit[i][j] - v * min(inf/v,dist[i][j]);
}
}
///Floyd
for(int k = 0;k < n;k++){
for(int i = 0;i < n;i++){
for(int j = 0;j < n;j++){
diff[i][j] = max(diff[i][j], diff[i][k] + diff[k][j]);
}
}
}
bool can = false;
for(int i = 0;i < n;i++){
if(diff[i][i] >= 0){
can = true;
break;
}
}
if(can) low = v;
else high = v;
}
cout << low;
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... |