Submission #147868

#TimeUsernameProblemLanguageResultExecution timeMemory
14786812tqianTravelling Merchant (APIO17_merchant)C++14
0 / 100
36 ms2056 KiB
#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

const double PI = 4 * atan(1);

#define sz(x) (int)(x).size()
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define pii pair <int, int>
#define vi vector<int>
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define vpi vector<pair<int, int>>
#define vpd vector<pair<double, double>>
#define pd pair<double, double>

#define f0r(i,a) for(int i=0;i<a;i++)
#define f1r(i,a,b) for(int i=a;i<b;i++)
#define trav(a, x) for (auto& a : x)

void fast_io(){
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
}
void io(string taskname){
    string fin = taskname + ".in";
    string fout = taskname + ".out";
    const char* FIN = fin.c_str();
    const char* FOUT = fout.c_str();
    freopen(FIN, "r", stdin);
    freopen(FOUT, "w", stdout);
    fast_io();
}
const int MAXN = 1e2 + 5;
const int MAXK = 1e3 + 5;
const ll INF = 1e18;
ll buy[MAXN][MAXK];
ll sell[MAXN][MAXK];
ll dist[MAXN][MAXN];
ll weight[MAXN][MAXN];
ll floyd[MAXN][MAXN];
ll floyd2[MAXN][MAXN];
//ll graph[MAXN][MAXN];
int n, m, k;

int check(ll r){
    f0r(i, n){
        f0r(j, n){
            if(dist[i][j] == INF){
                floyd[i][j] = -INF;
                continue;
            }
            floyd[i][j] = (weight[i][j] - r*dist[i][j]);
          //  if(r == 3)cout << i + 1 << " " << j + 1 << " " << floyd[i][j] << endl;
           // cout << graph[i][j] << endl;
           floyd[i][j] = max(-INF, floyd[i][j]);
        }
        floyd[i][i] = -INF;
    }
    f0r(mid, n){
        f0r(i, n){
            f0r(j, n){
                floyd[i][j] = max(floyd[i][j], floyd[i][mid] + floyd[mid][j]);
                if(floyd[i][i]>= 0) return 1;
                floyd[i][j] = min(floyd[i][j], INF);
                floyd2[i][j] = floyd[i][j];
            }
        }
    }
    f0r(mid, n){
        f0r(i, n){
            f0r(j, n){
                floyd2[i][j] = max(floyd2[i][j], floyd2[i][mid] + floyd2[mid][j]);
                if(floyd2[i][j]>floyd[i][j]) return 1;
            }
        }
    }

    return 0;
}
int main(){
    fast_io();
    cin >> n >> m >> k;
    f0r(i, n){
        f0r(j, k){
            int b, s;
            cin >> b >> s;
            buy[i][j] = b;
            sell[i][j] = s;
        }
    }
    f0r(i, MAXN){
        f0r(j, MAXN){
            dist[i][j] = INF;
        }
    }
    f0r(i, m){
        int u, v;
        ll w;
        cin >> u >> v >> w;
        u--; v--;
        dist[u][v] = min(dist[u][v], w);
    }
    f0r(i, n) dist[i][i] = 0;
    f0r(mid, n){
        f0r(i, n){
            f0r(j, n){
                dist[i][j] = min(dist[i][j], dist[i][mid] + dist[mid][j]);
            }
        }
    }
    f0r(i, n){
        f0r(j, n){
            if(i == j) continue;
            f0r(it, k){
                if(sell[j][it] == -1 || buy[i][it] == -1) continue;
                weight[i][j] = max(weight[i][j], sell[j][it] - buy[i][it]);
            }
        }
    }
    ll lo = 0;
    ll hi = 1e18;
    while(abs(lo - hi)>1){
        ll mid = (lo + hi)/2;
        if(check(mid)){
            lo = mid;
        }
        else{
            hi = mid-1;
        }
    }
    if(lo>hi) swap(lo, hi);
    if(check(hi)) cout << hi << endl;
    else cout << lo << endl;
//    cout << check(3) << endl;
    return 0;
}

Compilation message (stderr)

merchant.cpp:1:0: warning: ignoring #pragma comment  [-Wunknown-pragmas]
 #pragma comment(linker, "/stack:200000000")
 
merchant.cpp: In function 'void io(std::__cxx11::string)':
merchant.cpp:47:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     freopen(FIN, "r", stdin);
     ~~~~~~~^~~~~~~~~~~~~~~~~
merchant.cpp:48:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     freopen(FOUT, "w", stdout);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...