#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;
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 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++)
{
if(i == j) 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 = 10000000000LL, mid, best;
while(fim >= ini)
{
mid = (ini + fim)/2;
if(solve(mid))
{
best = mid;
ini = mid + 1;
}
else fim = mid - 1;
}
cout<<best<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
405 ms |
2436 KB |
Output is correct |
2 |
Incorrect |
63 ms |
2436 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
2436 KB |
Output is correct |
2 |
Correct |
16 ms |
2436 KB |
Output is correct |
3 |
Correct |
20 ms |
2436 KB |
Output is correct |
4 |
Correct |
21 ms |
2436 KB |
Output is correct |
5 |
Correct |
29 ms |
2436 KB |
Output is correct |
6 |
Correct |
4 ms |
2436 KB |
Output is correct |
7 |
Incorrect |
12 ms |
2436 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
172 ms |
2436 KB |
Output is correct |
2 |
Execution timed out |
1051 ms |
2808 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
2436 KB |
Output is correct |
2 |
Correct |
16 ms |
2436 KB |
Output is correct |
3 |
Correct |
20 ms |
2436 KB |
Output is correct |
4 |
Correct |
21 ms |
2436 KB |
Output is correct |
5 |
Correct |
29 ms |
2436 KB |
Output is correct |
6 |
Correct |
4 ms |
2436 KB |
Output is correct |
7 |
Incorrect |
12 ms |
2436 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |