# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
265348 | abacaba | Travelling Merchant (APIO17_merchant) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <chrono>
#include <vector>
#include <map>
#include <random>
#include <set>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <stdio.h>
#include <queue>
#include <bitset>
#include <cstdlib>
#include <deque>
#include <cassert>
#include <stack>
#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// typedef tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
#define mp make_pair
#define f first
#define se second
#define pb push_back
#define ppb pop_back
#define emb emplace_back
#define ll long long
#define ull unsigned long long
#define cntbit(x) __builtin_popcount(x)
#define endl '\n'
#define uset unordered_set
#define umap unordered_map
#define pii pair<int, int>
#define ld long double
#define pll pair<long long, long long>
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T> inline T range(T l, T r) {
return uniform_int_distribution<T>(l, r)(rng);
}
template <typename T> void Min(T &a, T b) {
a = min(a, b);
}
template <typename T> void Max(T &a, T b) {
a = max(a, b);
}
#define int long long
const int mod = 1e9 + 7;
const int inf = 2e15;
const int N = 1e2 + 15;
const int K = 1e3 + 15;
int n, m, k, buy[N][K], sell[N][K];
int g[N][N];
int mx[N][N], d[N][N];
int dp[N][N];
bool used[N];
int rec(int x, int y) {
if(dp[x][y] != inf)
return dp[x][y];
dp[x][y] = mx[x][y];
for(int k = 1; k <= n; ++k)
if(k != x && k != y) {
Min(dp[x][y], rec(x, k) + rec(k, y));
Max(dp[x][y], -inf);
}
return dp[x][y];
}
bool check(int mid) {
for(int i = 0; i < N; ++i) {
fill(d[i], d[i] + N, inf);
fill(dp[i], dp[i] + N, inf);
}
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
if(g[i][j])
d[i][j] = g[i][j] * mid;
for(int k = 1; k <= n; ++k)
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
Min(d[i][j], d[i][k] + d[k][j]);
memcpy(mx, d, sizeof(d));
for(int i = 1; i <= n; ++i)
dp[i][i] = 0;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
for(int s = 1; s <= k; ++s)
if(buy[i][s] != -1 && sell[j][s] != -1) {
Min(mx[i][j], d[i][j] + buy[i][s] - sell[j][s]);
Max(mx[i][j], -inf);
}
int ans = inf;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
if(i != j)
Min(ans, rec(i, j) + mx[j][i]);
return ans <= 0;
}
main() {
ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0);
// freopen("input.txt", "r", stdin);
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;
cin >> u >> v;
cin >> g[u][v];
}
int l = 1, r = 2e9, res = 0;
while(l <= r) {
int mid = (l + r) / 2;
if(check(mid))
l = mid + 1, res = mid;
else
r = mid - 1;
}
cout << res << endl;
return 0;
}#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <chrono>
#include <vector>
#include <map>
#include <random>
#include <set>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <stdio.h>
#include <queue>
#include <bitset>
#include <cstdlib>
#include <deque>
#include <cassert>
#include <stack>
#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// typedef tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
#define mp make_pair
#define f first
#define se second
#define pb push_back
#define ppb pop_back
#define emb emplace_back
#define ll long long
#define ull unsigned long long
#define cntbit(x) __builtin_popcount(x)
#define endl '\n'
#define uset unordered_set
#define umap unordered_map
#define pii pair<int, int>
#define ld long double
#define pll pair<long long, long long>
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T> inline T range(T l, T r) {
return uniform_int_distribution<T>(l, r)(rng);
}
template <typename T> void Min(T &a, T b) {
a = min(a, b);
}
template <typename T> void Max(T &a, T b) {
a = max(a, b);
}
#define int long long
const int mod = 1e9 + 7;
const int inf = 2e15;
const int N = 1e2 + 15;
const int K = 1e3 + 15;
int n, m, k, buy[N][K], sell[N][K];
int g[N][N];
int mx[N][N], d[N][N];
int dp[N][N];
bool used[N];
int rec(int x, int y) {
if(dp[x][y] != inf)
return dp[x][y];
dp[x][y] = mx[x][y];
for(int k = 1; k <= n; ++k)
if(k != x && k != y) {
Min(dp[x][y], rec(x, k) + rec(k, y));
Max(dp[x][y], -inf);
}
return dp[x][y];
}
bool check(int mid) {
for(int i = 0; i < N; ++i) {
fill(d[i], d[i] + N, inf);
fill(dp[i], dp[i] + N, inf);
}
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
if(g[i][j])
d[i][j] = g[i][j] * mid;
for(int k = 1; k <= n; ++k)
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
Min(d[i][j], d[i][k] + d[k][j]);
memcpy(mx, d, sizeof(d));
for(int i = 1; i <= n; ++i)
dp[i][i] = 0;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
for(int s = 1; s <= k; ++s)
if(buy[i][s] != -1 && sell[j][s] != -1) {
Min(mx[i][j], d[i][j] + buy[i][s] - sell[j][s]);
Max(mx[i][j], -inf);
}
int ans = inf;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
if(i != j)
Min(ans, rec(i, j) + mx[j][i]);
return ans <= 0;
}
main() {
ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0);
// freopen("input.txt", "r", stdin);
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;
cin >> u >> v;
cin >> g[u][v];
}
int l = 1, r = 2e9, res = 0;
while(l <= r) {
int mid = (l + r) / 2;
if(check(mid))
l = mid + 1, res = mid;
else
r = mid - 1;
}
cout << res << endl;
return 0;
}