제출 #487634

#제출 시각아이디문제언어결과실행 시간메모리
487634leaked여행하는 상인 (APIO17_merchant)C++14
0 / 100
1098 ms80844 KiB
#include <bits/stdc++.h>

#define f first
#define s second
#define vec vector
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pw(x) (1LL<<(x))
#define sz(x) (int)(x).size()
#define fast_iati ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

using namespace std;

template<class T> bool umin(T &a,const  T &b){return (a>b?a=b,1:0);}
template<class T> bool umax(T &a,const  T &b){return (a<b?a=b,1:0);}
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll>pll;
const int N=201;
const int K=2001;
const ll inf=1e17;
int b[N][K],s[N][K];
ll dst[N][N];
ll mx[N][N];
signed main(){
    fast_iati;
    int debug=0;
    int tests;
    if(debug) tests=10;
    else tests=1;
    while(tests--){
        int n,m,k;
        if(!debug)cin>>n>>m>>k;
        else n=4,m=5,k=2;
//        n=3,m=10,k=2;
        for(int i=0;i<n;i++){
            for(int j=0;j<k;j++){
                if(!debug)cin>>b[i][j]>>s[i][j];
                else{
                int x=rand();
                if(x%2) b[i][j]=rand();
                else b[i][j]=-1;
                x=rand();
                if(x%2) s[i][j]=rand();
                else s[i][j]=-1;
                if(b[i][j]!=-1 && s[i][j]!=-1 && b[i][j]<s[i][j]) swap(b[i][j],s[i][j]);
                }
                if(debug==2) cout<<b[i][j]<<' '<<s[i][j]<<' ';
            }
            if(debug==2)cout<<endl;
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(i==j) continue;
                mx[i][j]=0;
                for(int t=0;t<k;t++){
                    if(b[i][t]==-1 || s[j][t]==-1) continue;
                    umax(mx[i][j],(ll)-b[i][t]+s[j][t]);
                }
//                cout<<"YE "<<i<<' '<<j<<' '<<mx[i][j]<<endl;
            }
        }
        int tt=1;
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++) dst[i][j]=inf;
            dst[i][i]=0;
        }
        for(int i=0;i<m;i++){
            int v,u,w;
            if(!debug)cin>>v>>u>>w,--v,--u;
            else v=rand()%n,u=rand()%n,w=1;
            if(debug==2) cout<<v+1<<' '<<u+1<<' '<<w<<endl;
            tt&=(w==1);
            umin(dst[v][u],(ll)w);
        }
        for(int t=0;t<n;t++){
            for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
                    umin(dst[i][j],dst[i][t]+dst[t][j]);
                }
            }
        }
//        for(int i=0;i<n;i++){
//            for(int j=0;j<n;j++){
//                cout<<"DST "<<i<<' '<<j<<' '<<dst[i][j]<<endl;
//            }
//        }
        auto solve=[&]{
            auto check=[&](ll x){
                vec<vec<ll>>f(n,vec<ll>(n,-inf));
                int iter=n-1;
                for(int i=0;i<n;i++){
                    for(int j=0;j<n;j++){
                        if(dst[i][j]<=inf/x && i!=j){
                            umax(f[i][j],mx[i][j]-1ll*x*dst[i][j]);
//                            cout<<"PKRE "<<i<<' '<<j<<' '<<f[i][j]<<endl;
                        }else{
//                            cout<<"BAD " <<i<<' '<<j<<' '<<dst[i][j]<<' '<<inf/x<<endl;
                        }
                    }
                }
                vec<ll>dp(n,-inf);
                for(int j=1;j<n;j++) umax(dp[j],f[0][j]);
                while(iter--){
                    int ok=0;
                    for(int i=0;i<n;i++){
                        for(int j=0;j<n;j++){
                            if(f[i][j]==-inf || dp[i]==-inf) continue;
                            if(umax(dp[j],dp[i]+f[i][j]))
                                ok=1;
                            umin(dp[j],inf);
                        }
                    }
//                    if(iter==0 && ok) return 1;
                    if(!ok) break;
                }
                if(!iter){
                    for(int i=0;i<n;i++){
                        for(int j=0;j<n;j++){
                            if(f[i][j]==-inf || dp[i]==-inf) continue;
                            if(umax(dp[j],dp[i]+f[i][j])){
                                if(f[j][0]!=-inf) return 1;
                            }
                        }
                    }
                }
                if(dp[0]>=0) return 1;
                return 0;
            };
//            cout<<check(3);
//            return (ll)0;
            ll l=1,r=inf;
            while(l!=r){
                ll mid=(l+r)/2;
                if(check(mid)) l=mid+1;
                else r=mid;
            }
            return l-1;
        };
        const int CNT=200000;
        auto brute=[&](){
            vec<vec<ll>>dp(n,vec<ll>(CNT+1,-inf));
            dp[0][0]=0;
            ll mxt=0;
            for(int j=0;j<CNT;j++){
                for(int i=0;i<n;i++){
                    for(int to=0;to<n;to++){
                        if(i==to) continue;
                        if(j+dst[i][to]<=CNT){
                            int w=j+dst[i][to];
                            umax(dp[to][w],dp[i][j]+(ll)mx[i][to]);
//                            cout<<"STUP "<<to<<' '<<w<<' '<<dp[to][w]<<' '<<mxendl;
                        }
                    }
                }
            }
            for(int t=1;t<=CNT;t++){
                umax(mxt,dp[0][t]/t);
//                    assert(t<=m);
            }
            return mxt;
        };
        if(tt==1 && n<=50) cout<<brute();
        else cout<<solve();
//        cout<<solve()<<' '<<brute()<<endl;
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...