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 <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long int
#define endl '\n'
#define pb push_back
#define pi pair<int, int>
#define pii pair<int, pi>
#define fir first
#define sec second
#define MAXN 100005
#define mod 1000000007
const int inf = LLONG_MAX / 2;
int n, m, k;
int b[101][1001];
int s[101][1001];
int cost[101][101];
int stonks[101][101];
int g[101][101];
vector<int> adj[101];
void floyd(int dist[101][101])
{
for (int k = 0; k < n; k++)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
void floyd2(int dist[101][101])
{
for (int k = 0; k < n; k++)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
dist[i][j] = max(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
bool can(int mid)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
g[i][j] = stonks[i][j] - (min(cost[i][j], inf / mid) * mid);
}
// nesse grafo, agr quero saber se existe um ciclo que a soma dos custos das arestas eh >= 0
floyd(g);
for (int i = 0; i < n; i++)
{
if (g[i][i] >= 0)
return 1;
}
return 0;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m >> k;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < k; j++)
cin >> b[i][j] >> s[i][j];
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
cost[i][j] = inf;
}
for (int j = 0; j < m; j++)
{
int a, b, c;
cin >> a >> b >> c;
a--, b--;
adj[a].pb(b);
cost[a][b] = c;
}
floyd(cost);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
for (int x = 0; x < k; x++)
{
if (s[j][x] == -1 || b[i][x] == -1)
continue;
stonks[i][j] = max(stonks[i][j], s[j][x] - b[i][x]);
}
}
}
int l = 0, r = 1e18;
while (l < r)
{
int mid = (l + r + 1) >> 1;
(can(mid)) ? l = mid : r = mid - 1;
}
cout << l << endl;
return 0;
}
// n mercados
// m arestas de mao unica
// k diferentes items
// cada item tem
// b[i][j] - buy o item j no mercado i
// s[i][j] - sell o item j no mercado i
// arestas de v pra w usando t de tempo
// uma ideia eh
// pensar em resolver pra cada componente fortemente conexa
// busca binaria na resposta
// p / t >= mid
// entao
// p - (t * mid) >= 0
// 0 3 2 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... |