제출 #768504

#제출 시각아이디문제언어결과실행 시간메모리
768504raysh07여행하는 상인 (APIO17_merchant)C++17
0 / 100
1084 ms198648 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)1e18
#define f first
#define s second

mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

int n, m, k;
const int N = 101;
const int K = 1e3 + 1;
int buy[N][K];
int sell[N][K];
int dp[N][K]; 
int holy[K];
int curr, ans;
vector <pair<int, int>> adj[N], inv[N];

bool check(int x){
    ans = x;
    for (curr = 1; curr <= n; curr++){
        //loop over cycle start 
        
        for (int i = 0; i < N; i++){
            for (int j = 0; j < K; j++){
                dp[i][j] = -INF;
            }
        }
        
        dp[curr][0] = 0;
        for (int i = 1; i <= k; i++){
            if (buy[curr][i] != -1)
            dp[curr][i] = -buy[curr][i];
        }
        
        priority_queue <pair<int, pair<int, int>>> pq;
        for (int i = 0; i <= k; i++){
            pq.push({dp[curr][i], {curr, i}});
        }
        
        while (!pq.empty()){
            auto pi = pq.top();
            pq.pop();
            
            int ds = pi.first;
            int u = pi.second.first;
            int ok = pi.second.second;
            
            if (ds != dp[u][ok]) continue;
            
            if (ok != 0){
                if (dp[u][0] < ds + sell[u][ok] && sell[u][ok] != -1){
                    dp[u][0] = ds + sell[u][ok];
                    pq.push({dp[u][0], {u, 0}});
                }
            } else {
                for (int i = 1; i <= k; i++){
                    if (dp[u][i] < ds - buy[u][i] && buy[u][i] != -1){
                        dp[u][i] = ds - buy[u][i];
                        pq.push({dp[u][i], {u, i}});
                    }
                }
            }
            
            for (auto [v, w] : adj[u]){
                if (dp[v][ok] < dp[u][ok] - x * w){
                    dp[v][ok] = dp[u][ok] - x * w;
                    pq.push({dp[v][ok], {v, ok}});
                }
            }
        }
        
        for (int i = 0; i <= k; i++){
            holy[i] = -INF;
        }
        
        for (auto [v, w] : inv[curr]){
            for (int i = 0; i <= k; i++){
                holy[i] = max(holy[i], dp[v][i] - x * w);
            }
        }
        
        for (int i = 1; i <= k; i++){
            if (sell[curr][i] != -1)
            holy[0] = max(holy[0], holy[i] + sell[curr][i]);
        }
        
        if (holy[0] >= 0) return true;
    }
    return false;
}

void Solve() 
{
    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 u, v, w; cin >> u >> v >> w;
        
        adj[u].push_back({v, w});
        inv[v].push_back({u, w});
    }
    
    int lo = 0, hi = 1e10;
    while (lo != hi){
        int mid = (lo + hi + 1) / 2;
        
        if (check(mid)) lo = mid;
        else hi = mid - 1;
    }
    
    assert(lo != 1e10);
    
    cout << lo << "\n";
}

int32_t main() 
{
    auto begin = std::chrono::high_resolution_clock::now();
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
  //  cin >> t;
    for(int i = 1; i <= t; i++) 
    {
        //cout << "Case #" << i << ": ";
        Solve();
    }
    auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; 
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...