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>
using namespace std ;
const long long inf = 1e18 ;
const int MAXN = 100 + 10 ;
const int MAXK = 1000 + 10 ;
bool Overflow_Mul(long double x , long double y)
{
return (x * y > inf) ;
}
long long s[MAXN][MAXK] , b[MAXN][MAXK] ;
long long dist[MAXN][MAXN] , best[MAXN][MAXK] ;
long long ans[MAXN][MAXN] ;
int n , m , k ;
void preprocess()
{
for(int x = 1 ; x <= n ; ++x)
{
for(int y = 1 ; y <= n ; ++y)
{
best[x][y] = 0 ;
for(int z = 1 ; z <= k ; ++z)
{
if(b[x][z] != -1 && s[y][z] != -1)
best[x][y] = max(best[x][y] , s[y][z] - b[x][z]) ;
}
}
}
for(int z = 1 ; z <= n ; ++z)
{
for(int x = 1 ; x <= n ; ++x)
{
for(int y = 1 ; y <= n ; ++y)
dist[x][y] = min(dist[x][y] , dist[x][z] + dist[z][y]) ;
}
}
}
bool check(long long mid)
{
for(int x = 1 ; x <= n ; ++x)
{
for(int y = 1 ; y <= n ; ++y)
{
ans[x][y] = -1ll * inf ;
if(best[x][y] == inf || dist[x][y] == inf || x == y)
continue ;
if(!Overflow_Mul(mid , dist[x][y]))
ans[x][y] = max(-1ll * inf , best[x][y] - mid * dist[x][y]) ;
}
}
for(int z = 1 ; z <= n ; ++z)
{
for(int x = 1 ; x <= n ; ++x)
{
for(int y = 1 ; y <= n ; ++y)
ans[x][y] = max(ans[x][y] , min(inf , ans[x][z] + ans[z][y])) ;
}
}
for(int i = 1 ; i <= n ; ++i)
{
if(ans[i][i] >= 0)
return true ;
}
return false ;
}
int main()
{
ios_base::sync_with_stdio(0) ;
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 ; i <= n ; ++i)
{
for(int j = 1 ; j <= n ; ++j)
dist[i][j] = inf ;
}
for(int i = 0 ; i < m ; ++i)
{
int x , y , z ;
cin>>x>>y>>z ;
dist[x][y] = z ;
}
preprocess() ;
long long ans = 0 ;
long long l = 1 , r = 1e15 ;
while(l <= r)
{
long long mid = (l + r) >> 1 ;
if(check(mid))
ans = mid , l = mid+1ll ;
else
r = mid-1ll ;
}
return cout<<ans<<"\n" , 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... |