제출 #1093682

#제출 시각아이디문제언어결과실행 시간메모리
1093682RiverFlow여행하는 상인 (APIO17_merchant)C++14
100 / 100
70 ms3504 KiB
#include <bits/stdc++.h>

#define nl "\n"
#define no "NO"
#define yes "YES"
#define fi first
#define se second
#define vec vector
#define task "main"
#define _mp make_pair
#define ii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define evoid(val) return void(std::cout << val)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
#define unq(x) sort(all(x)); x.resize(unique(all(x)) - x.begin())

using namespace std;

template<typename U, typename V> bool maxi(U &a, V b) {
    if (a < b) { a = b; return 1; } return 0;
}
template<typename U, typename V> bool mini(U &a, V b) {
    if (a > b or a == -1) { a = b; return 1; } return 0;
}

const int mod = (int)1e9 + 7;

void prepare(); void main_code();

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    if (fopen(task".inp", "r")) {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    const bool MULTITEST = 0; prepare();
    int num_test = 1; if (MULTITEST) cin >> num_test;
    while (num_test--) { main_code(); }
}

void prepare() {};

const int N = (int)107;
const int K = (int)1007;

int n, m, k;
int buy[N][K], sell[N][K];

long long d[N][N];
int prof[N][N];

long long dp[N];

const long long INF = -(long long)1e15;

long long d2[N][N];

bool check(int x) {
    FOR(i, 1, n) FOR(j, 1, n) {
        if (i == j or d[i][j] == -1)
            d2[i][j] = INF;
        else {
            d2[i][j] = 1LL * prof[i][j] - 1LL * d[i][j] * x;
        }
    }
    FOR(i, 1, n) {
        FOR(j, 1, n) if (j != i and d[i][j] != -1 and d[j][i] != -1) {
            maxi(d2[i][i], 1LL * prof[i][i] - (d[i][j] + d[j][i]) * x);
        }
    }
    FOR(k, 1, n)
    FOR(i, 1, n)
    FOR(j, 1, n) {
        maxi(d2[i][j], d2[i][k] + d2[k][j]);
    }
    FOR(i, 1, n) if (d2[i][i] >= 0)
        return 1;
    return 0;
}

void main_code() {
    int K;
    cin >> n >> m >> K;
    FOR(i, 1, n) {
        FOR(j, 1, K) {
            cin >> buy[i][j] >> sell[i][j];
        }
    }
    memset(d, -1, sizeof d);
    FOR(i, 1, n) d[i][i] = 0;
    for(int i = 1; i <= m; ++i) {
        int u, v, w;
        cin >> u >> v >> w;
        d[u][v] = w;
    }
    FOR(k, 1, n)
    FOR(i, 1, n) if (d[i][k] != -1)
    FOR(j, 1, n) if (d[k][j] != -1) {
        mini(d[i][j], d[i][k] + d[k][j]);
    }
    FOR(i, 1, n) FOR(j, 1, n) if (d[i][j] != -1) {
        FOR(k, 1, K) if (sell[j][k] != -1 and buy[i][k] != -1)
            prof[i][j] = max(prof[i][j],
                             sell[j][k] - buy[i][k]);
    }

    int lb = 1, rb = (int)1e9, ans = 0;
    while (lb <= rb) {
        int mid = (lb + rb) >> 1;
        if (check(mid)) {
            ans = mid;
            lb = mid + 1;
        } else {
            rb = mid - 1;
        }
    }
    cout << ans;
}


/*     Let the river flows naturally     */

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

merchant.cpp: In function 'int main()':
merchant.cpp:35:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   35 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
merchant.cpp:36:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   36 |         freopen(task".out", "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...