제출 #558758

#제출 시각아이디문제언어결과실행 시간메모리
558758kwongwengTravelling Merchant (APIO17_merchant)C++17
0 / 100
70 ms2020 KiB
#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*MOD;
            if (i==j) mat[i][j] = 0;
        }
    }
    // 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 cycle?
        vector<ll> dist(n+1);
        FOR(rep,0,n-1){ // repeat n-1 times
            FOR(i,1,n+1){
                FOR(j,1,n+1){
                    if (i==j) continue;
                    ll w = M*mat[i][j] - profit[i][j];
                    dist[j] = min(dist[j], dist[i]+w);
                }
            }
            /*
            if (dist[1] < 0){
                sol = true; break;
            }
            */
        }
        /*
        if (sol){
            L = M; continue;
        }
        */
        FOR(i,1,n+1){
            FOR(j,1,n+1){
                if (i==j) continue;
                ll w = M*mat[i][j] - profit[i][j];
                if (dist[i] + w < dist[j]){
                    sol = true; break;
                }
            }
        }
        FOR(i,2,n+1){
            if (dist[i] + mat[i][1]*M <= 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();
    }
}

컴파일 시 표준 에러 (stderr) 메시지

merchant.cpp:5: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    5 | #pragma GCC optimization ("Ofast")
      | 
merchant.cpp:6: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    6 | #pragma GCC optimization ("unroll-loops")
      | 
merchant.cpp: In function 'int main()':
merchant.cpp:144:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  144 |   freopen("input.txt", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
merchant.cpp:145:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  145 |   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...