제출 #1117159

#제출 시각아이디문제언어결과실행 시간메모리
1117159gustavo_d여행하는 상인 (APIO17_merchant)C++17
12 / 100
399 ms17944 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int MAXN = 100;
const int MAXK = 1000;
const double EPS = 0.0001;
pair<ll, ll> path[MAXN][MAXN][MAXN+1];
ll buy[MAXN][MAXK], sell[MAXN][MAXK];
ll dist[MAXN][MAXN];
vector<pair<int, ll>> adj[MAXN];

double val(pair<ll, ll> p) {
    // cout << "divisão" << p.first << ' ' << p.second << endl;
    return (double)p.first / (double)p.second;
}
void Dijkstra(int src) {
    priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
    pq.push({0, src});
    dist[src][src] = 0;
    while (!pq.empty()) {
        int v = pq.top().second; ll d = pq.top().first;
        pq.pop();
        if (dist[src][v] < d) continue;
        for (auto [viz, w] : adj[v]) {
            if (d + w >= dist[src][viz]) continue;
            dist[src][viz] = d + w;
            pq.push({dist[src][viz], viz});
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);

    int n, m, k; cin >> n >> m >> k;
    bool sub2 = n <= 50 and k <= 50;
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            dist[i][j] = 1e18;
        }
    }
    for (int i=0; i<n; i++) {
        for (int p=0; p<k; p++) {
            cin >> buy[i][p] >> sell[i][p];
        }
    }
    for (int i=0; i<m; i++) {
        int u, v; ll w; cin >> u >> v >> w;
        u--; v--;
        if (w != 1) sub2 = false;
        adj[u].push_back({v, w});
        // adj[v].push_back({u, w});
    }

    for (int i=0; i<n; i++) Dijkstra(i);

    int prof[n][n];
    for (int u=0; u<n; u++) {
        for (int v=0; v<n; v++) {
            // cout << dist[u][v] << ' ';
            ll mx_profit = 0;
            for (int p=0; p<k; p++) {
                if (buy[u][p] == -1 or sell[v][p] == -1) continue;
                mx_profit = max(mx_profit, sell[v][p]-buy[u][p]);
            }
            // cout << u << '>' << v << ':' << mx_profit << ' ' << dist[u][v] << endl;
            path[u][v][0] = {mx_profit, dist[u][v]};
            prof[u][v] = mx_profit;
            if (u == v) {
                path[u][v][0] = {0, 1e18};
            }
        }
        // cout << endl;
    }

    if (sub2) {
        int ans = 0;
        for (int src=0; src<n; src++) {
            vector<vector<int>> dp(n, vector<int> (n+1, -1e9));
            dp[src][0] = 0;
            for (int t=1; t<=n; t++) {
                for (int act=0; act<n; act++) {
                    dp[act][t] = -1e9;
                    for (int i=0; i<n; i++) {
                        if (dist[i][act] <= t) {
                            dp[act][t] = max(
                                dp[act][t],
                                dp[i][t-dist[i][act]] + prof[i][act]
                            );
                        }
                    }
                    if (act == src) {
                        ans = max(ans, dp[act][t] / t);
                    }
                }
            }
        }
        cout << ans << '\n';
        return 0;
    }

    for (int i=0; i<=n; i++) {
        for (int mid=1; mid<=n; mid++) {
            for (int u=0; u<n; u++) {
                for (int v=0; v<n; v++) {
                    path[u][v][mid] = path[u][v][mid-1];
                    if (path[u][mid-1][mid-1].second != (ll)1e18 and path[mid-1][v][mid-1].second != (ll)1e18) {
                        pair<ll, ll> new_path = {
                            path[u][mid-1][mid-1].first + path[mid-1][v][mid-1].first,
                            path[u][mid-1][mid-1].second + path[mid-1][v][mid-1].second
                        };
                        if (val(new_path) > val(path[u][v][mid]) + EPS) {
                            path[u][v][mid] = new_path;
                        }
                    }

                    if (mid == n) path[u][v][0] = path[u][v][n];
                }
            }
        }
    }

    ll ans = 0;
    for (int i=0; i<n; i++) {
        // if (path[i][i][n].first / path[i][i][n].second > ans) {
        //     cout << "ciclo em i=" << i << ':' << path[i][i][n].first << ' ' << path[i][i][n].second << endl;
        // }
        ans = max(ans, path[i][i][n].first / path[i][i][n].second);
    }
    cout << ans << '\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...