#include <bits/stdc++.h>
#define N 150
#define K 1050
#define f first
#define s second
using namespace std;
#define int long long
typedef long long ll;
typedef pair<ll, ll> pii;
ll inf = 2000000000000000000;
bool can[N][N];
ll n, m, k, dist[N][N], best_cost[N][N], B[N][K], S[N][K];
ll E[N][N], dp[N][N];
vector< pii > grafo[N];
void dfs(int x, int ini)
{
for(auto v: grafo[x])
{
if(can[ini][v.f]) continue;
can[ini][v.f] = 1;
dfs(v.f, ini);
}
}
ll find_ciclo()
{
ll ans = 0;
for(int i = 1; i <= n; i++)
{
can[i][i] = true;
dfs(i, i);
}
for(int i = 1; i <= n; i++)
{
if(can[1][i] and can[i][1])
{
ll cima = best_cost[1][i];
ll baixo = dist[i][1] + dist[1][i];
if(baixo != 0) ans = max(ans, cima/baixo);
}
}
return ans;
}
void dijkstra(int ini)
{
priority_queue < pii, vector< pii >, greater < pii > > pq;
for(int i = 1; i <= n; i++) dist[ini][i] = inf;
dist[ini][ini] = 0;
pq.push({0, ini});
while(!pq.empty())
{
int x = pq.top().s;
ll d = pq.top().f;
pq.pop();
if(d > dist[ini][x]) continue;
for(auto v: grafo[x])
{
if(dist[ini][v.f] > dist[ini][x] + v.s)
{
dist[ini][v.f] = dist[ini][x] + v.s;
pq.push({dist[ini][v.f], v.f});
}
}
}
}
bool solve(ll x)
{
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
for(int q = 1; q <= k; q++)
{
if(B[i][q] == -1 or S[j][q] == -1) continue;
ll C = -B[i][q] + S[j][q];
best_cost[i][j] = max(best_cost[i][j], C);
}
//cout<<"CUSTO "<<i<<" "<<j<<" "<<best_cost[i][j]<<"\n";
}
}
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
dp[i][j] = inf;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
if(dist[i][j] == inf) continue;
E[i][j] = -best_cost[i][j] + x*dist[i][j];
dp[i][j] = E[i][j];
//cout<<i<<" "<<j<<" "<<dp[i][j]<<"\n";
}
dp[i][i] = inf;
}
for(int q = 1; q <= n; q++)
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
dp[i][j] = min(dp[i][j], dp[i][q] + dp[q][j]);
for(int i = 1; i <= n; i++)
{
if(dp[i][i] <= 0)
{
//cout<<"CICLO NEGATIVO "<<i<<"\n";
return true;
}
}
return false;
}
int32_t main()
{
ios::sync_with_stdio(false); cin.tie(0);
cin>>n>>m>>k;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= k; j++) cin>>B[i][j]>>S[i][j];
for(int i = 1, a, b; i <= m; i++)
{
ll c;
cin>>a>>b>>c;
grafo[a].push_back({b, c});
}
for(int i = 1; i <= n; i++) dijkstra(i);
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
for(int q = 1; q <= k; q++)
{
if(B[i][q] == -1 or S[j][q] == -1) continue;
ll C = -B[i][q] + S[j][q];
best_cost[i][j] = max(best_cost[i][j], C);
}
//cout<<"CUSTO "<<i<<" "<<j<<" "<<best_cost[i][j]<<"\n";
}
}
ll ini = 0LL, fim = inf, mid, best;
while(fim >= ini)
{
mid = (ini + fim)/2;
if(solve(mid))
{
best = mid;
ini = mid + 1;
}
else fim = mid - 1;
}
cout<<best<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
816 ms |
2552 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
23 ms |
2552 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
339 ms |
2552 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
23 ms |
2552 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |