This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof...(u) ? ", " : ""), dout(u...);}
#else
#define debug(...) 7122
#endif
#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second
const int INF = 2e9;
const int SIZE = 105;
const int KSIZ = 1005;
int n, m, k;
vector<pair<int, int>> adj[SIZE];
int a[SIZE][KSIZ], b[SIZE][KSIZ];
int dis[SIZE][SIZE], profit[SIZE][SIZE];
ll dp[SIZE][SIZE];
bool ok(int p) {
if (p == 0) return 1;
FOR (i, 1, n) FOR (j, 1, n) {
if (i == j) dp[i][j] = -INF;
else dp[i][j] = profit[i][j] - 1ll * dis[i][j] * p;
}
FOR (k, 1, n) FOR (i, 1, n) FOR (j, 1, n) dp[i][j] = max(dp[i][j], dp[i][k] + dp[k][j]);
FOR (i, 1, n) if (dp[i][i] >= 0) return 1;
return 0;
}
void solve() {
cin >> n >> m >> k;
FOR (i, 1, n) FOR (j, 1, k) cin >> a[i][j] >> b[i][j];
while (m--) {
int a, b, w;
cin >> a >> b >> w;
adj[a].pb(b, w);
}
FOR (i, 1, n) {
fill(dis[i] + 1, dis[i] + n + 1, INF);
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
pq.emplace(dis[i][i] = 0, i);
while (pq.size()) {
auto [d, pos] = pq.top();
pq.pop();
if (d > dis[i][pos]) continue;
for (auto [np, w] : adj[pos]) if (d + w < dis[i][np]) pq.emplace(dis[i][np] = d + w, np);
}
}
FOR (i, 1, n) FOR (j, 1, n) if (i != j) {
int mx = 0;
FOR (x, 1, k) if (a[i][x] != -1 && b[j][x] != -1 && a[i][x] < b[j][x]) mx = max(mx, b[j][x] - a[i][x]);
profit[i][j] = mx;
}
int l = 0, r = 1e9;
while (l < r) {
int mid = (l + r) / 2 + 1;
if (ok(mid)) l = mid;
else r = mid - 1;
}
cout << l << '\n';
}
int main() {
Waimai;
solve();
}
/*
in1
4 5 2
10 9 5 2
6 4 20 15
9 7 10 9
-1 -1 16 11
1 2 3
2 3 3
1 4 1
4 3 1
3 1 1
out1
2
*/
# | 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... |