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>
using namespace std;
#define N 1010
#define ll long long
#define fs first
#define sc second
#define ii pair<ll,int>
#define pb push_back
#define int ll
int times,pos[N],low[N],tham[N],deg[N],dem,num[N],n,m,k,buy[N][N],sell[N][N],cost[N][N],dis[N][N],ktr[N][N];
vector<ii>g[N];
stack<int>st;
void visit(int u)
{
low[u]=num[u]=++times;
st.push(u);
for(auto v:g[u])
if(num[v.sc]!=0)
{
low[u]=min(low[u],num[v.sc]);
}else
{
visit(v.sc);
low[u]=min(low[u],low[v.sc]);
}
if(num[u]==low[u])
{
// cout<<u<<endl;
dem++;
int k;
do{
k=st.top();
st.pop();
pos[k]=dem;
low[k]=num[k]=1e9;
}while(k!=u);
}
}
ll get_cost(int u,int v)
{
ll res=-1e18;
for(int i=1;i<=k;i++)
if(buy[u][i]!=-1&&sell[v][i]!=-1)
res=max(res,sell[v][i]-buy[u][i]);
return res;
}
void dijkstra(int S)
{
priority_queue<ii>s;
dis[S][S]=0;
s.push({0,S});
while(!s.empty())
{
ii cc=s.top();
s.pop();
ll L=-cc.fs;
int u=cc.sc;
if(ktr[S][u]==1)
continue;
ktr[S][u]=1;
for(auto v:g[u])
if(dis[S][v.sc]>L+v.fs)
{
dis[S][v.sc]=L+v.fs;
s.push({-dis[S][v.sc],v.sc});
}
}
}
ii dp[N];
bool check(int X)
{
//for(int i=1;i<=n;i++)cout<<pos[i]<<endl;
times++;
for(int i=1;i<=n;i++)
dp[i]={-1e18,0};
for(int i=1;i<=n;i++)
if(tham[pos[i]]!=times)
{
dp[i]={0,0};
tham[pos[i]]=times;
}
for(int t=1;t<=n;t++)
{
for(int u=1;u<=n;u++)
for(int v=1;v<=n;v++)
if(u!=v&&dis[u][v]<1e12)
{
if(dp[v]<ii{dp[u].fs+cost[u][v]-dis[u][v]*X,dp[u].sc+1})
{
dp[v]=ii{dp[u].fs+max(0ll,cost[u][v])-dis[u][v]*X,dp[u].sc+1};
// cout<<u<<" "<<v<<" "<<dp[v].fs<<" "<<cost[u][v]<<" "<<dis[u][v]*X<<endl;
}
// dp[v]=max(dp[v],ii{dp[u].fs+cost[u][v]-dis[u][v]*X,dp[u].sc+1});
}
//for(int i=1;i<=n;i++)cout<<dp[i].fs<<" "<<dp[i].sc<<endl;cout<<"cc\n";
}
// for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)cout<<i<<" "<<j<<" "<<cost[i][j]<<endl;
for(int u=1;u<=n;u++)
for(int v=1;v<=n;v++)
if(u!=v&&dis[u][v]<1e12)
{
if(dp[v]<ii{dp[u].fs+cost[u][v]-dis[u][v]*X,dp[u].sc+1})
return 1;
}
return 0;
}
int32_t main()
{
///// freopen("A.inp","r",stdin);
///freopen("A.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>m>>k;
for(int i=1;i<=n;i++)
for(int j=1;j<=k;j++)
cin>>buy[i][j]>>sell[i][j];
for(int i=1;i<=m;i++)
{
int u,v,L;
cin>>u>>v>>L;
g[u].pb({L,v});
// g[v].pb({L,u});
}
memset(cost,-0x3f3f,sizeof(cost));
memset(dis,0x3f3f,sizeof(dis));
// cout<<get_cost(1,2);return 0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
cost[i][j]=get_cost(i,j);
for(int i=1;i<=n;i++)
dijkstra(i);
for(int i=1;i<=n;i++)
if(num[i]==0)
visit(i);
//cout<<dem<<endl;
//cout<<check(2)<<endl;
// return 0;
int l=1,r=1e9,res=0;
while(l<=r)
{
int mid=(l+r)/2;
if(check(mid))
{
res=mid;
l=mid+1;
}else
r=mid-1;
}
cout<<res;
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... |