제출 #487649

#제출 시각아이디문제언어결과실행 시간메모리
487649leaked여행하는 상인 (APIO17_merchant)C++14
33 / 100
1091 ms2116 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 n,m,k;
    cin>>n>>m>>k;
    for(int i=0;i<n;i++){
        for(int j=0;j<k;j++){
            cin>>b[i][j]>>s[i][j];
        }
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(i==j) continue;
            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]);
            }
        }
    }
    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;
        cin>>v>>u>>w;--v;--u;
        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]);
            }
        }
    }
    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]);
                }
            }
        }
        for(int s=0;s<n;s++){
            iter=30;
            vec<ll>dp(n,-inf);
            for(int j=0;j<n;j++){
                if(s!=j)
                    umax(dp[j],f[s][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][s]!=-inf) return 1;
                        }
                    }
                }
            }
            if(dp[s]>=0) return 1;
        }
        return 0;
    };
//    cout<<check(3);
//    return 0;
    ll l=1,r=inf;
    while(l!=r){
        ll mid=(l+r)/2;
        if(check(mid)) l=mid+1;
        else r=mid;
    }
    cout<<l-1;
    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...