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>
#pragma GCC optimize("O3,unroll-loops")
#define pb push_back
#define F first
#define S second
#define ld long double
#define int long long
#define all(a) a.begin(),a.end()
#define pii pair <int,int>
#define sz(v) (int)v.size()
#define rep(i , a , b) for(int i=a;i <= (b);i++)
#define per(i , a , b) for(int i=a;i >= (b);i--)
#define deb(x) cout <<#x << " : " << x << "\n" ;
using namespace std ;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 202 + 10 , maxk = 1e4, sq = 1e5 , lg = 19, inf = 1e18+10 , mod = 998244353;
int b[maxn][maxk] , s[maxn][maxk] , d[maxn][maxn] , w[maxn][maxn] , sp[maxn] , n;
bool ch(int x){
rep(i , 1, n)sp[i] = 0 ;
rep(i , 1 ,n+1){
rep(j , 1 ,n){
rep(k , 1 , n){
if(d[j][k] == inf)continue ;
sp[k] = min(sp[k] , sp[j] + (-w[j][k] + d[j][k]*x));
}
}
}
rep(j , 1, n){
rep(k , 1, n){
if(d[j][k] == inf)continue ;
if(sp[k] > sp[j]+(-w[j][k]+d[j][k]*x))return 1 ;
}
}
return 0 ;
}
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);
int m , k ;
cin >> n >> m >> k ;
rep(i , 1, n){
rep(j , 1, k){
cin >> b[i][j] >> s[i][j] ;
}
}
rep(i , 1, n){
rep(j , 1 , n){
w[i][j] = -inf ;
if(i == j)continue ;
w[i][j] = 0 ;
rep(z , 1 , k){
if(b[i][z] == -1 || s[j][z] == -1)continue ;
w[i][j] = max(w[i][j] , -b[i][z] + s[j][z]) ;
}
}
}
rep(i , 1, n){
rep(j , 1 ,n)d[i][j] = inf ;
d[i][i] = 0;
}
while(m--){
int v, u , w ;
cin >> v >> u >> w ;
d[v][u] = min(d[v][u] , w);
}
rep(i ,1 ,n){
rep(j , 1, n){
rep(z, 1 , n){
d[j][z] = min(d[j][z] , d[j][i] + d[i][z]) ;
}
}
}
int l = 0, r =2e9 ;
while(r-l>1){
int mid = (l+r)/2 ;
if(ch(mid) == 1)l=mid ;
else r = mid ;
}
rep(i , 1, n){
rep(j , 1, n){
sp[j] = inf ;
}
rep(j , 1, n){
if(j == i)continue ;
sp[j] = (-w[i][j] + (l+1)*d[i][j]);
}
rep(i , 1 ,n+1){
rep(j , 1 ,n){
rep(k , 1 , n){
if(d[j][k] == inf)continue ;
sp[k] = min(sp[k] , sp[j] + (-w[j][k] + d[j][k]*(l+1)));
}
}
}
bool ok =0 ;
rep(j , 1 ,n){
rep(k , 1 , n){
if(d[j][k] == inf)continue ;
if(sp[k] > sp[j] + (-w[j][k] + d[j][k]*(l+1)))ok = 1;
}
}
if(ok == 1)continue ;
if(sp[i] == 0){
cout << l+1 << "\n";
exit(0) ;
}
}
cout << l << "\n";
return 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... |