# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
57277 | IvanC | Travelling Merchant (APIO17_merchant) | C++17 | 147 ms | 4916 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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 110;
const int MAXM = 1010;
const ll INF = 1e17;
const ll LMAX = 1e10;
ll compra[MAXN][MAXM],venda[MAXN][MAXM];
ll matriz[MAXN][MAXN],lucro[MAXN][MAXN];
int chega[MAXN][MAXN],tem_lucro[MAXN][MAXN],existe[MAXN][MAXN];
ll shortest_path[MAXN][MAXN];
int N,M,K;
int FloydWarshall(ll X){
for(int i = 1;i<=N;i++){
for(int j = 1;j<=N;j++){
shortest_path[i][j] = INF;
existe[i][j] = 0;
}
}
for(int i = 1;i<=N;i++){
for(int j = 1;j<=N;j++){
if(!tem_lucro[i][j] || !chega[i][j]) continue;
shortest_path[i][j] = X*matriz[i][j] - lucro[i][j];
existe[i][j] = 1;
}
}
for(int k = 1;k<=N;k++){
for(int i = 1;i<=N;i++){
for(int j = 1;j<=N;j++){
if(!existe[i][k] || !existe[k][j]) continue;
existe[i][j] |= 1;
shortest_path[i][j] = min(shortest_path[i][j], shortest_path[i][k] + shortest_path[k][j] );
}
}
}
for(int i = 1;i<=N;i++){
if(shortest_path[i][i] <= 0) return 1;
}
return 0;
}
ll bruta_par(int u,int v){
if(u == v) return -1;
ll best = 0;
for(int i = 1;i<=K;i++){
if(compra[u][i] != -1 && venda[v][i] != -1) best = max(best, venda[v][i] - compra[u][i] );
}
return best;
}
void calcula_lucro(int v){
for(int i = 1;i<=N;i++){
lucro[v][i] = bruta_par(v,i);
if(lucro[v][i] >= 0) tem_lucro[v][i] = 1;
}
}
int main(){
scanf("%d %d %d",&N,&M,&K);
for(int i = 1;i<=N;i++){
for(int j = 1;j<=N;j++){
matriz[i][j] = INF;
}
}
for(int i = 1;i<=N;i++){
for(int j = 1;j<=K;j++){
scanf("%lld %lld",&compra[i][j],&venda[i][j]);
}
}
//printf("Custos\n");
for(int i = 1;i<=N;i++) calcula_lucro(i);
for(int i = 1;i<=M;i++){
int u,v;
ll w;
scanf("%d %d %lld",&u,&v,&w);
matriz[u][v] = w;
chega[u][v] = 1;
}
//printf("Leu\n");
for(int k = 1;k<=N;k++){
for(int i = 1;i<=N;i++){
for(int j = 1;j<=N;j++){
if(!chega[i][k] || !chega[k][j]) continue;
chega[i][j] |= 1;
matriz[i][j] = min(matriz[i][j],matriz[i][k] + matriz[k][j]);
}
}
}
//printf("Floyd\n");
//for(int i = 1;i<=N;i++){
// printf("%d :",i);
// for(int j = 1;j<=N;j++){
// printf(" (%lld,%lld)", chega[i][j] ? (matriz[i][j]) : (-1), lucro[i][j] );
// }
// printf("\n");
//}
ll resposta = 0,ini = 1,fim = LMAX,meio;
while(ini <= fim){
meio = ini + (fim - ini)/2;
if(FloydWarshall(meio)){
resposta = meio;
ini = meio + 1;
}
else{
fim = meio - 1;
}
}
printf("%lld\n",resposta);
return 0;
}
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... |