# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
309920 | vipghn2003 | Travelling Merchant (APIO17_merchant) | C++14 | 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>
#define int long long
template<typename T> void domax(T &a, T b) { if (b > a) a = b; }
template<typename T> void domin(T &a, T b) { if (b < a) a = b; }
using namespace std;
const long long inf = 3e18, MaxT = 10ll*1000*1000;
const int MaxN = 105, MaxK = 1005;
int n, m, K;
long long b[MaxN][MaxK], s[MaxN][MaxK], dist[MaxN][MaxN], best[MaxN][MaxN], cost[MaxN][MaxN];
bool can(long long eff) {
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cost[i][j]=-3e18;
if(dist[i][j]==3e18) continue;
cost[i][j]=best[i][j]-dist[i][j]*eff;
}
}
for(int k=0;k<n;k++)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cost[i][j]=max(cost[i][j],cost[i][k]+cost[k][j]);
if(cost[i][j]>=3e18) cost[i][j]=3e18;
}
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++) if(cost[i][j]+cost[j][i]>=0) return true;
}
return false;
}
main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin>>n>>m>>K;
for (int i = 0; i < n; i++) {
for (int k = 0; k < K; k++) {
cin>>b[i][k]>>s[i][k];
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++) dist[i][j]=3e18;
}
while(m--)
{
int u,v,w;
cin>>u>>v>>w;
u--;
v--;
dist[u][v]=w;
}
for(int u=0;u<n;u++)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
int val=dist[i][u]+dist[u][j];
dist[i][j]=min(dist[i][j],val);
}
}
}
for(int u=0;u<n;u++)
{
for(int v=0;v<n;v++)
{
best[u][v]=0;
for(int item=0;item<k;item++)
{
if(b[u][item]==-1||s[v][item]==-1) continue;
best[u][v]=max(best[u][v],s[v][item]-b[u][item]);
}
}
}
long long lo = 1ll, hi = 1000ll * 1000 * 1000;
long long ans = 0ll;
while (lo <= hi) {
long long mid = (lo + hi) / 2;
if (can(mid)) {
lo = mid + 1;
ans = mid;
} else {
hi = mid - 1;
}
}
assert(!can(ans+1));
printf("%lld\n", ans);
}