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 <iostream>
#include <fstream>
#include <queue>
#include <climits>
#define mp make_pair
using namespace std;
const int nmax=105;
const int kmax=1005;
queue< int > q;
vector<int> v[nmax];
long long cost[nmax][nmax],wn[nmax][nmax],cc[nmax][nmax];
long long d[nmax],prof[nmax][kmax],pret[nmax][kmax];
int viz[nmax],inq[nmax];
int n,k,m,nod,item,nxt,i,j,a,b,c,ind;
long long p,ans;
bool check(long long x)
{
while(!q.empty()) q.pop();
q.push(0);
for(int i=0;i<=n;i++)
viz[i]=inq[i]=0;
for(int i=1;i<=n;i++)
d[i]=LLONG_MIN;
while(!q.empty())
{
nod=q.front();inq[nod]=0;
q.pop();
viz[nod]++;
if(viz[nod]>=n+2) return 1;
for(int nxt=1;nxt<=n;nxt++)
if(nxt!=nod&&cost[nod][nxt]<=1LL*1000*1000*1000)
{
if(d[nod]+wn[nod][nxt]-1LL*x*cost[nod][nxt]>=d[nxt])
{
if(!inq[nxt])
{
q.push(nxt);
inq[nxt]=1;
}
d[nxt]=d[nod]+wn[nod][nxt]-1LL*x*cost[nod][nxt];
}
}
}
return 0;
}
void bf(int s)
{
q.push(s);
while(!q.empty())
{
nod=q.front();q.pop();
for(i=0;i<v[nod].size();i++)
{
nxt=v[nod][i];
if(cost[s][nod]+cc[nod][nxt]<cost[s][nxt])
{
cost[s][nxt]=cost[s][nod]+cc[nod][nxt];
q.push(nxt);
}
}
}
}
int main()
{
//freopen("data.in","r",stdin);
ios_base::sync_with_stdio(false);
cin>>n>>m>>k;
for(i=1;i<=n;i++)
{
pret[i][0]=-1,prof[i][0]=-1;
for(j=1;j<=k;j++)
{
cin>>pret[i][j];
cin>>prof[i][j];
}
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
for(ind=1;ind<=k;ind++)
if(pret[i][ind]!=-1&&prof[j][ind]!=-1)
wn[i][j]=max(wn[i][j],prof[j][ind]-pret[i][ind]);
for(i=1;i<=k;i++)
pret[0][i]=prof[0][i]=-1;
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
if(i!=j)
cost[i][j]=1LL*1e16;
for(i=1; i<=m; i++)
{
cin>>a>>b>>c;
cc[a][b]=c;
v[a].push_back(b);
}
int kt;
for(int s=1;s<=n;s++)
bf(s);
for(p=30; p>=0; p--)
if(check(ans+(1LL<<p)))
ans+=(1LL<<p);
cout<<ans;
return 0;
}
Compilation message (stderr)
merchant.cpp: In function 'void bf(int)':
merchant.cpp:52:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0;i<v[nod].size();i++)
~^~~~~~~~~~~~~~
merchant.cpp: In function 'int main()':
merchant.cpp:94:9: warning: unused variable 'kt' [-Wunused-variable]
int kt;
^~
# | 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... |