이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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
if (curr > 1) continue;
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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |