Submission #1117034

#TimeUsernameProblemLanguageResultExecution timeMemory
1117034hyakupTravelling Merchant (APIO17_merchant)C++17
100 / 100
107 ms2404 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define bug(x) cout << #x << " " << x << endl; const int maxn = 1e2 + 10; const int maxk = 1e3 + 10; const ll inf = 2e9 + 10; ll tempo[maxn][maxn], lucro[maxn][maxn], dist[maxn], buy[maxn][maxk], sell[maxn][maxk]; bool marc[maxn]; vector<pair<int, ll>> adj[maxn]; void dijkstra( int source ){ set<pair<ll, int>> s; for( int i = 0; i < maxn; i++ ) tempo[source][i] = inf; tempo[source][source] = 0; s.insert({ 0, source }); while( !s.empty() ){ int cur = s.begin()->second; s.erase(s.begin()); for( auto [viz, d] : adj[cur] ) if( tempo[source][viz] > tempo[source][cur] + d ){ s.erase({ tempo[source][viz], viz }); tempo[source][viz] = tempo[source][cur] + d; s.insert({ tempo[source][viz], viz }); } } } void bellman_ford( int n ){ for( int i = 1; i <= n; i++ ) dist[i] = inf*inf; dist[1] = 0; for( int i = 1; i <= n + 1; i++ ){ // cout << "distancias " << endl; // for( int cur = 1; cur <= n; cur++ ) cout << dist[cur] << " "; // cout << endl; for( int cur = 1; cur <= n; cur++ ) for( auto [viz, d] : adj[cur] ) dist[viz] = min( dist[viz], dist[cur] + d ); } } bool dfs( int cur, int source ){ if( marc[cur] ) return (cur == source); marc[cur] = true; for( auto [viz, d] : adj[cur] ) if( dist[viz] == dist[cur] + d && dfs( viz, source ) ) return true; return false; } bool check_negative_cicle( int n, ll val ){ // bug(val); for( int i = 1; i <= n; i++ ){ adj[i].clear(); for( int j = 1; j <= n; j++ ) if( i != j && tempo[i][j] != inf ) adj[i].push_back({ j, tempo[i][j]*val - lucro[i][j] }); } bellman_ford( n ); for( int i = 1; i <= n; i++ ){ for( int j = 1; j <= n; j++ ) marc[j] = false; if( dfs( i, i ) ) return true; for( auto [viz, d] : adj[i] ) if( dist[viz] > dist[i] + d ) return true; } return false; } ll bs( int n ){ ll l = 0, r = inf;/////////////////////////////////////////////////////////////////////////////////// while( l < r ){ ll mid = ( l + r + 1 )/2; if( check_negative_cicle( n, mid ) ) l = mid; else r = mid - 1; } return l; } int main(){ int n, m, k; 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]; for( int i = 1; i <= m; i++ ){ int a, b, c; cin >> a >> b >> c; adj[a].push_back({ b, c }); } // get distances for( int i = 1; i <= n; i++ ){ dijkstra( i ); for( int j = 1; j <= n; j++ ) if( j != i ){ for( int item = 1; item <= k; item++ ) if( buy[i][item] != -1 && sell[j][item] != -1 ) lucro[i][j] = max( lucro[i][j], sell[j][item] - buy[i][item] ); } } // // for( int i = 1; i <= n;i++ ) for( int j = 1; j <= n; j++ ) if( i != j ){ // // cout << i << " " << j << ":" << endl; // bug(tempo[i][j]); // bug(lucro[i][j]); // } cout << bs( n ) << endl; }

Compilation message (stderr)

merchant.cpp: In function 'bool dfs(int, int)':
merchant.cpp:43:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   43 |     if( marc[cur] ) return (cur == source); marc[cur] = true;
      |     ^~
merchant.cpp:43:45: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   43 |     if( marc[cur] ) return (cur == source); marc[cur] = true;
      |                                             ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...