Submission #558774

#TimeUsernameProblemLanguageResultExecution timeMemory
558774kwongwengTravelling Merchant (APIO17_merchant)C++17
100 / 100
67 ms2128 KiB
/* Solution for APIO 2016 - Merchant Tags : binary search, shortest path (Floyd-Warshall) Complexity O(n^3 * log (MOD)) */ #include <bits/stdc++.h> using namespace std; #pragma GCC target ("avx2") #pragma GCC optimization ("Ofast") #pragma GCC optimization ("unroll-loops") typedef long long ll; typedef vector<int> vi; typedef pair<int, int> ii; typedef vector<ii> vii; typedef long double ld; typedef pair<ll, ll> pll; #define FOR(i, a, b) for(int i = a; i < b; i++) #define ROF(i, a, b) for(int i = a; i >= b; i--) #define ms memset #define pb push_back #define fi first #define se second ll MOD = 1000000007; ll MOD1 = 998244353; ll power(ll base, ll n){ if (n == 0) return 1; if (n == 1) return base; ll halfn = power(base, n/2); if (n % 2 == 0) return (halfn * halfn) % MOD; return (((halfn * halfn) % MOD) * base) % MOD; } ll inverse(ll n){ return power(n, MOD-2); } ll add(ll a, ll b){ return (a+b) % MOD; } ll mul(ll a, ll b){ a %= MOD; return (a*b) % MOD; } ll gcd(ll a, ll b){ if (a == 1) return 1; if (a == 0) return b; return gcd(b%a, a); } ld pi = 3.141592653589793238; void solve(){ int n, m, k; cin >> n >> m >> k; ll b[n+1][k], s[n+1][k]; FOR(i,1,n+1){ FOR(j,0,k){ cin >> b[i][j] >> s[i][j]; //b[i][j] = buying price for jth item in ith market //s[i][j] = selling price } } ll mat[n+1][n+1]; ms(mat,-1,sizeof(mat)); FOR(i,0,m){ int u, v, w; cin >> u >> v >> w; mat[u][v] = w; } FOR(i,1,n+1){ FOR(j,1,n+1){ if (mat[i][j]==-1) mat[i][j] = MOD; } } // O(n^3) Floyd-Warshall to find shortest distance between any 2 markets FOR(i,1,n+1){ FOR(j,1,n+1){ FOR(l,1,n+1){ mat[j][l] = min(mat[j][l], mat[j][i]+mat[i][l]); } } } ll profit[n+1][n+1]; FOR(i,1,n+1){ FOR(j,1,n+1){ ll mx = 0; // not buying/selling item FOR(l,0,k){ if (b[i][l] != -1 && s[j][l] != -1) mx = max(mx, s[j][l] - b[i][l]); } profit[i][j] = mx; // max profit from buying an item in market i and selling it in market j } } ll L = 0, R = MOD; while (R-L>1){ ll M = (L+R)/2; bool sol = false; // does there exist a nonnegative positive cycle? ll dist[n+1][n+1]; FOR(i,1,n+1){ FOR(j,1,n+1){ if (mat[i][j] == MOD) dist[i][j] = -MOD; else dist[i][j] = profit[i][j] - M*mat[i][j]; } } FOR(i,1,n+1){ FOR(j,1,n+1){ FOR(l,1,n+1){ dist[j][l] = max(dist[j][l], dist[j][i] + dist[i][l]); } } } FOR(i,1,n+1){ if (dist[i][i] >= 0){ sol = true; break; } } if (sol) L=M; else R=M; } cout << L << '\n'; return; } int main() { cout << fixed << setprecision(8); ios::sync_with_stdio(false); if (fopen("input.txt", "r")) { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); } int TC = 1; //cin >> TC; FOR(i, 1, TC+1){ //cout << "Case #" << i << ": "; solve(); } }

Compilation message (stderr)

merchant.cpp:10: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
   10 | #pragma GCC optimization ("Ofast")
      | 
merchant.cpp:11: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
   11 | #pragma GCC optimization ("unroll-loops")
      | 
merchant.cpp: In function 'int main()':
merchant.cpp:133:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  133 |   freopen("input.txt", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
merchant.cpp:134:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  134 |   freopen("output.txt", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...