Submission #492514

#TimeUsernameProblemLanguageResultExecution timeMemory
492514MahfelTravelling Merchant (APIO17_merchant)C++17
100 / 100
99 ms4344 KiB
#include<bits/stdc++.h> using namespace std; typedef long long ll; const ll MXN = 120 , MXM = 10000 , MXK = 1020 , Inf = 1e10; ll n,m,k; ll profit[MXN][MXN] , dis[MXN][MXN]; ll buy[MXN][MXK] , sell[MXN][MXK]; ll gr[MXN][MXN]; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cin >> n >> m >> k; for(int i = 1 ; i <= n ; i++) { for(int j = 1 ; j <= k ; j++) { cin >> buy[i][j] >> sell[i][j]; // if(buy[i][j] == -1) buy[i][j] = Inf; // if(sell[i][j] == -1) sell[i][j] = -Inf; } } for(int i = 1 ; i <= n ; i++) { for(int j = 1 ; j <= n ; j++) { dis[i][j] = Inf; } dis[i][i] = 0; } for(int i = 0 ; i < m ; i++) { ll v,u,w; cin >> v >> u >> w; dis[v][u] = w; } for(int k = 1 ; k <= n ; k++) { for(int i = 1 ; i <= n ; i++) { for(int j = 1 ; j <= n ; j++) { dis[i][j] = min(dis[i][j] , dis[i][k] + dis[k][j]); } } } for(int i = 1 ; i <= n ; i++) { for(int j = 1 ; j <= n ; j++) { for(int x = 1 ; x <= k ; x++) { if(sell[j][x] != -1 && buy[i][x] != -1) profit[i][j] = max(profit[i][j] , sell[j][x] - buy[i][x]); } } } ll l = 0 , r = 1000000000 + 200; while(r-l > 1) { ll m = (l+r)/2; for(int i = 1 ; i <= n ; i++) { for(int j = 1 ; j <= n ; j++) { gr[i][j] = dis[i][j]*m - profit[i][j]; } gr[i][i] = Inf; } for(int k = 1 ; k <= n ; k++) { for(int i = 1 ; i <= n ; i++) { for(int j = 1 ; j <= n ; j++) { if(gr[i][k] < Inf) gr[i][j] = min(gr[i][j] , gr[i][k] + gr[k][j]); // if(gr[i][k] + gr[k][j] < gr[i][j]-EPS) { // gr[i][j] = gr[i][k] + gr[k][j]; // } // if(gr[i][j] < -Inf) gr[i][j] = -Inf; } } } bool ok = 0; for(int i = 1 ; i <= n ; i++) { if(gr[i][i] <= 0) { ok = 1; break; } } if(ok) { l = m; } else { r = m; } } cout << l << "\n"; // printf("%.0lf\n" , l); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...