# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
487954 | Mahdi | Travelling Merchant (APIO17_merchant) | C++17 | 0 ms | 0 KiB |
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 ("Ofast")
using namespace std;
#define all(v) v.begin(), v.end()
#define pii pair<int, int>
#define pil pair<int, ll>
#define pll pair<ll, ll>
#define F first
#define S second
typedef long long ll;
const int N=105, K=1e3+5;
int n, m, k, b[N][K], s[N][K], dis[N][N], a[N][N];
bool c[N][N];
ll d[N][N];
bool ok(ll x){
for(int i=0;i<n;++i){
for(int j=0;j<n;++j)
ds[i][j]=d[i][j]=-x*dis[i][j]+a[i][j];
}
for(int o=0;o<n;++o){
for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
if(c[i][o] && c[o][j])
d[i][j]=max(d[i][j], d[i][o]+d[o][j]);
}
}
for(int i=0;i<n;++i){
if(d[i][i]>0)
return 1;
}
}
for(int i=0;i<n;++i){
for(int j=i+1;j<n;++j){
if(c[i][j] && c[j][i] && d[i][j]+d[j][i]>=0)
return 1;
}
}
return 0;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
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){
fill(dis[i], dis[i]+n, 1e9+5);
dis[i][i]=0;
}
int u, v, w;
for(int i=0;i<m;++i){
cin>>u>>v>>w;
dis[--u][--v]=w;
}
for(int o=0;o<n;++o){
for(int i=0;i<n;++i){
for(int j=0;j<n;++j)
dis[i][j]=min(dis[i][j], dis[i][o]+dis[o][j]);
}
}
for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
if(dis[i][j]==5+1e9)
dis[i][j]=0;
else
c[i][j]=1;
}
}
for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
for(int o=0;o<k;++o){
if(b[i][o]!=-1 && s[j][o]!=-1)
a[i][j]=max(a[i][j], s[j][o]-b[i][o]);
}
}
}
int l=0, r=1e9;
while(l<r-1){
int mid=(l+r)/2;
if(ok(mid))
l=mid;
else
r=mid;
}
cout<<l;
}