Submission #1117207

#TimeUsernameProblemLanguageResultExecution timeMemory
1117207ThegeekKnight16Travelling Merchant (APIO17_merchant)C++17
100 / 100
80 ms4344 KiB
#include <bits/stdc++.h> using namespace std; #define int long long const int INF = 1e9 + 10; bool bellman(int x, const vector<vector<int>> &preco, const vector<vector<int>> &tempo, const int &N) { vector<vector<int>> p(N+1, vector<int>(N+1)); for (int i = 1; i <= N; i++) for (int j = 1; j <= N; j++) p[i][j] = x * tempo[i][j] - preco[i][j]; for (int i = 1; i <= N; i++) p[i][i] = INF; vector<int> dist(N+1, 0); for (int t = 1; t <= N+1; t++) for (int i = 1; i <= N; i++) for (int j = 1; j <= N; j++) dist[j] = min(dist[j], dist[i] + p[i][j]); for (int i = 1; i <= N; i++) for (int j = 1; j <= N; j++) if (dist[j] > dist[i] + p[i][j]) return true; vector<vector<int>> grafo(N+1); vector<int> grau(N+1); for (int i = 1; i <= N; i++) for (int j = 1; j <= N; j++) if (dist[j] == dist[i] + p[i][j]) grafo[i].push_back(j), grau[j]++; queue<int> q; for (int i = 1; i <= N; i++) if (grau[i] == 0) q.push(i); while (!q.empty()) { int cur = q.front(); q.pop(); for (auto viz : grafo[cur]) if (--grau[viz] == 0) q.push(viz); } return *max_element(grau.begin(), grau.end()); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int N, M, K; cin >> N >> M >> K; vector<vector<pair<int, int>>> items(N+1, vector<pair<int, int>>(K)); for (int i = 1; i <= N; i++) for (auto &[B, S] : items[i]) cin >> B >> S; vector<vector<int>> preco(N+1, vector<int>(N+1, 0)); for (int i = 1; i <= N; i++) for (int j = 1; j <= N; j++) for (int k = 0; k < K; k++) { auto [Bi, Si] = items[i][k]; auto [Bj, Sj] = items[j][k]; if (Bi == -1 || Sj == -1) continue; if (Bi >= Sj) continue; preco[i][j] = max(preco[i][j], Sj - Bi); } vector<vector<int>> tempo(N+1, vector<int>(N+1, INF)); for (int i = 1; i <= M; i++) { int X, Y, P; cin >> X >> Y >> P; tempo[X][Y] = min(tempo[X][Y], P); } for (int i = 1; i <= N; i++) tempo[i][i] = 0; for (int k = 1; k <= N; k++) for (int i = 1; i <= N; i++) for (int j = 1; j <= N; j++) tempo[i][j] = min(tempo[i][j], tempo[i][k] + tempo[k][j]); int ini = 0, fim = INF; while (ini < fim) { int m = (ini + fim + 1) >> 1; if (bellman(m, preco, tempo, N)) ini = m; else fim = m-1; } cout << fim << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...