Submission #202451

# Submission time Handle Problem Language Result Execution time Memory
202451 2020-02-16T07:33:24 Z darkkcyan Travelling Merchant (APIO17_merchant) C++17
21 / 100
1000 ms 2168 KB
// vim: foldmethod=marker
// Sat Feb 15 23:04:31 MSK 2020: start 
// Sat Feb 15 23:15:57 MSK 2020: done coding
// Sat Feb 15 23:26:12 MSK 2020: ok samples.
#include <bits/stdc++.h>
using namespace std;

#define llong long long 
#define len(x) ((int)x.size())
#define rep(i,n) for (int i = -1; ++ i < n; )
#define rep1(i,n) for (int i = 0; i ++ < n; )
#define rand __rand
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());  // or mt19937_64
template<class T = int> T rand(T range = numeric_limits<T>::max()) { return (T)(rng() % range); }

/*{{{*/
#define CONCAT_(x, y) x##y
#define CONCAT(x, y) CONCAT_(x, y)
#ifdef LOCAL_DEBUG   
int debug = 1;
#define DB(...) stringstream CONCAT(ss, __LINE__); CONCAT(ss, __LINE__) << __VA_ARGS__; debug_block CONCAT(dbbl, __LINE__)(CONCAT(ss, __LINE__).str())
#else
int debug = 0;
#define DB(...)
#endif
int __db_level = 0;
#define clog if (debug) cerr << string(__db_level * 2, ' ')
struct debug_block {
    string name;
    debug_block(const string& name_): name(name_) { clog << "{ " << name << endl; ++__db_level; }
    ~debug_block() { --__db_level; clog << "} " << name << endl; }
};
#define deb(...) "[" << #__VA_ARGS__ << "] = [" << __VA_ARGS__ << "]"
#define debln(...)  clog << "[" << #__VA_ARGS__ << "] = [" << __VA_ARGS__ << "]" << endl
#define _ << ", " <<
template<class U, class V>
ostream& operator<<(ostream& out, const pair<U, V>& p) { return out << "(" << p.first _ p.second << ")"; }
template<class A, class B>
ostream& operator<<(ostream& out, const tuple<A, B>& t) { return out << "(" << get<0>(t) _ get<1>(t) << ")"; }
template<class A, class B, class C>
ostream& operator<<(ostream& out, const tuple<A, B, C>& t) { return out << "(" << get<0>(t) _ get<1>(t) _ get<2>(t) << ")"; }
template<class T> ostream& operator<<(ostream& out, const vector<T>& container) { 
    out << "{";
    if (len(container)) out << container[0];
    rep1(i, len(container) - 1) out _ container[i];
    return out << "}";
}
template<class x> vector<typename x::value_type> $v(const x& a) { return vector<typename x::value_type>(a.begin(), a.end()); }
#define ptrtype(x) typename iterator_traits<x>::value_type
template<class u> vector<ptrtype(u)> $v(u a, u b) { return vector<ptrtype(u)>(a, b); }
/*}}}*/
// ACTUAL SOLUTION BELOW ////////////////////////////////////////////////////////////

const int maxn = 111;
const int maxk = 1010;
const llong inf = (llong)1e12;
int n, m, k;
llong buy_cost[maxn][maxk], sell_cost[maxn][maxk];
llong dist[maxn][maxn];
llong profit[maxn][maxn];

llong cur_ans[maxn][2];
bool in_queue[maxn][2];
int cnt_vis[maxn][2];
int dfs_state[maxn][2];

inline llong edge_dist(int u, int v, llong ans) {
    return dist[u][v] * min(LLONG_MAX / max(dist[u][v], 1ll), ans);
}

bool detect_zero_cycle(int u, bool holding, llong ans) {
    dfs_state[u][holding] = 1;
    rep1(v, n) {
        if (dfs_state[v][!holding] == 2) continue;
        llong new_dist = cur_ans[u][holding] - edge_dist(u, v, ans);
        if (holding) new_dist += profit[u][v];
        if (new_dist != cur_ans[v][!holding]) continue;
        if (dfs_state[v][!holding] == 1) return true;
        if (detect_zero_cycle(v, !holding, ans)) return true;
    }
    dfs_state[u][holding] = 2;
    return false;
}

bool check(llong ans) {
    DB("check " << ans);  
    rep1(i, n) {
        cur_ans[i][0] = cur_ans[i][1] = -inf;
        cnt_vis[i][0] = cnt_vis[i][1] = 0;
        in_queue[i][0] = in_queue[i][1] = false;
        dfs_state[i][0] = dfs_state[i][1] = 0;
    }

    queue<pair<int, int>> qu;
    rep1(i, n) {
        if (cnt_vis[i][0]) continue;
        for (qu.emplace(i, 0), cur_ans[i][0] = 0, in_queue[i][0] = 1; len(qu); qu.pop()) {
            auto [u, holding] = qu.front();
            ++cnt_vis[u][holding];
            in_queue[u][holding] = false;

            if (cnt_vis[u][holding] > n * 2) return true;
            rep1(v, n) {
                llong new_dist = cur_ans[u][holding] - edge_dist(u, v, ans);
                if (holding) new_dist += profit[u][v];
                if (new_dist <= cur_ans[v][!holding]) continue;
                cur_ans[v][!holding] = new_dist;
                if (!in_queue[v][!holding]) {
                    qu.emplace(v, !holding);
                    in_queue[v][!holding] = true;
                }
            }
        }

        if (detect_zero_cycle(i, 0, ans)) return true;
    }
    return false;
}

int main(void) {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> m >> k;
    rep1(i, n) {
        dist[i][i] = 0;
        for (int f = i + 1; f <= n; ++f)
            dist[i][f] = dist[f][i] = inf;
    }

    rep1(i, n) rep(f, k) {
        cin >> buy_cost[i][f] >> sell_cost[i][f];
    }

    rep(i, m) {
        int u, v; llong d;
        cin >> u >> v >> d;
        dist[u][v] = min(dist[u][v], d);
    }

    rep1(mid, n) rep1(u, n) rep1(v, n) {
        dist[u][v] = min(dist[u][mid] + dist[mid][v], dist[u][v]);
    }

    memset(profit, 0, sizeof(profit));
    rep(thing, k) {
        rep1(u, n) {
            if (buy_cost[u][thing] == -1) continue;
            rep1(v, n) {
                if (sell_cost[v][thing] == -1) continue;
                profit[u][v] = max(profit[u][v], sell_cost[v][thing] - buy_cost[u][thing]);
            }
        }
    }

    rep1(i, n) profit[i][i] = -inf;
    
    llong low = 0, high = 1;
    while (check(high)) high <<= 1;

    while (low < high) {
        llong mid = low + (high - low + 1) / 2;
        if (check(mid)) low = mid;
        else high = mid - 1;
    }
    cout << low << endl;

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 131 ms 2168 KB Output is correct
2 Correct 7 ms 1272 KB Output is correct
3 Execution timed out 1080 ms 1272 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 156 ms 888 KB Output is correct
2 Correct 251 ms 888 KB Output is correct
3 Correct 119 ms 888 KB Output is correct
4 Correct 97 ms 888 KB Output is correct
5 Correct 331 ms 888 KB Output is correct
6 Correct 5 ms 504 KB Output is correct
7 Correct 54 ms 888 KB Output is correct
8 Correct 85 ms 888 KB Output is correct
9 Correct 91 ms 888 KB Output is correct
10 Correct 188 ms 892 KB Output is correct
11 Correct 345 ms 1016 KB Output is correct
12 Correct 206 ms 888 KB Output is correct
13 Correct 329 ms 888 KB Output is correct
14 Correct 281 ms 1016 KB Output is correct
15 Correct 337 ms 892 KB Output is correct
16 Correct 330 ms 1016 KB Output is correct
17 Correct 153 ms 888 KB Output is correct
18 Correct 5 ms 504 KB Output is correct
19 Correct 410 ms 1144 KB Output is correct
20 Correct 383 ms 1020 KB Output is correct
21 Correct 259 ms 888 KB Output is correct
22 Correct 385 ms 892 KB Output is correct
23 Correct 209 ms 888 KB Output is correct
24 Correct 6 ms 888 KB Output is correct
25 Correct 261 ms 968 KB Output is correct
26 Correct 5 ms 504 KB Output is correct
27 Correct 338 ms 892 KB Output is correct
28 Correct 288 ms 1016 KB Output is correct
29 Correct 370 ms 1016 KB Output is correct
30 Correct 8 ms 504 KB Output is correct
31 Correct 6 ms 888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1090 ms 1400 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 156 ms 888 KB Output is correct
2 Correct 251 ms 888 KB Output is correct
3 Correct 119 ms 888 KB Output is correct
4 Correct 97 ms 888 KB Output is correct
5 Correct 331 ms 888 KB Output is correct
6 Correct 5 ms 504 KB Output is correct
7 Correct 54 ms 888 KB Output is correct
8 Correct 85 ms 888 KB Output is correct
9 Correct 91 ms 888 KB Output is correct
10 Correct 188 ms 892 KB Output is correct
11 Correct 345 ms 1016 KB Output is correct
12 Correct 206 ms 888 KB Output is correct
13 Correct 329 ms 888 KB Output is correct
14 Correct 281 ms 1016 KB Output is correct
15 Correct 337 ms 892 KB Output is correct
16 Correct 330 ms 1016 KB Output is correct
17 Execution timed out 1090 ms 1400 KB Time limit exceeded
18 Halted 0 ms 0 KB -