This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimization "O2"
#pragma GCC optimization "unroll-loop"
#pragma GCC target ("avx2")
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define fs first
#define sc second
using namespace std;
typedef pair<ll,ll> LL;
const ll N = 1e3 + 9;
const ll inf = 1e15 + 7;
ll profit[N][N],n,m,d[N][N],B[N][N],S[N][N],adj[N][N];
ll x,y,w,k;
void out(){
for (ll i = 1;i <= n;i++){
for (ll j = 1;j <= n;j++) cout<<adj[i][j]<<" ";
cout<<"\n";
}
exit(0);
}
bool chk(ll x){
if (!x) return 1;
for (ll i = 1;i <= n;i++){
for (ll j = 1;j <= n;j++){
if (i == j) adj[i][j] = -inf;
else{
if (d[i][j] > inf/x) adj[i][j] = -inf;
else adj[i][j] = profit[i][j] - x*d[i][j];
}
}
}
//out();
for (ll l = 1;l <= n;l++){
for (ll i = 1;i <= n;i++){
for (ll j = 1;j <= n;j++) adj[i][j] = max(adj[i][j],adj[i][l] + adj[l][j]);
}
}
for (ll i = 1;i <= n;i++) if (adj[i][i] >= 0) return 1;
return 0;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define task "test"
if (fopen(task".inp","r")){
freopen(task".inp","r",stdin);
//freopen(task".out","w",stdout);
}
cin>>n>>m>>k;
for (ll i = 1;i <= n;i++)
for (ll j = 1;j <= k;j++) cin>>B[i][j]>>S[i][j];
for (ll i = 1;i <= n;i++)
for (ll j = 1;j <= n;j++) d[i][j] = inf;
while(m--){
cin>>x>>y>>w;
d[x][y] = w;
}
for (ll l = 1;l <= n;l++)
for (ll i = 1;i <= n;i++)
for (ll j = 1;j <= n;j++) d[i][j] = min(d[i][j],d[i][l] + d[l][j]);
for (ll i = 1;i <= n;i++){
for (ll j = 1;j <= n;j++){
if (i == j||d[i][j] == inf) continue;
ll now = 0;
for (ll l = 1;l <= k;l++)
if (S[j][l] != -1 && B[i][l] != -1) now = max(now,S[j][l] - B[i][l]);
profit[i][j] = now;
}
}
ll l,mid,h; //cout<<chk(2); return 0;
l = 0; h = inf;
while(l <= h){
mid = (l + h)/2;
if (chk(mid)) l = mid + 1;
else h = mid - 1;
}
cout<<h;
}
Compilation message (stderr)
merchant.cpp:1: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
1 | #pragma GCC optimization "O2"
|
merchant.cpp:2: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
2 | #pragma GCC optimization "unroll-loop"
|
merchant.cpp: In function 'int main()':
merchant.cpp:51:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
51 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | 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... |