Submission #261183

#TimeUsernameProblemLanguageResultExecution timeMemory
261183evpipis여행하는 상인 (APIO17_merchant)C++11
0 / 100
90 ms2424 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int len = 105, max_k = 1005, inf = 1e9+10;
const ll long_inf = 1e18+10;

int n, m, k;
int dis[len][len], prof[len][len], buy[len][max_k], sell[len][max_k];
ll path[len][len], state[len];

bool check(int c){
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            path[i][j] = long_inf;

    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            if (dis[i][j] != inf)
                path[i][j] = c*1LL*dis[i][j] - prof[i][j];

    for (int d = 0; d < n; d++)
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++){
                if (i == d || d == j)
                    continue;
                if (path[i][d] != long_inf && path[d][j] != long_inf)
                    path[i][j] = min(path[i][j], path[i][d]+path[d][j]);
                //if (path[i][j] < -long_inf)
                  //  path[i][j] = -long_inf;
                assert(path[i][j] >= -long_inf);
            }

    // although it works fine with that without any
    // further overflow checks, it is wrong!!!
    for (int i = 0; i < n; i++)
        if (path[i][i] <= 0)
            return true;
    return false;
}

int bs(){
    int l = 1, r = 1e9, ans = 0;
    while (l <= r){
        int mid = (l+r)/2;
        if (check(mid))
            ans = mid, l = mid+1;
        else
            r = mid-1;
    }

    return ans;
}

int main(){
    //freopen("0001-cycle-bydist0.in", "r", stdin);

    scanf("%d %d %d", &n, &m, &k);
    for (int i = 0; i < n; i++)
        for (int j = 0; j < k; j++)
            scanf("%d %d", &buy[i][j], &sell[i][j]);

    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            dis[i][j] = inf;

    for (int i = 0; i < m; i++){
        int a, b, c;
        scanf("%d %d %d", &a, &b, &c);
        dis[a-1][b-1] = c;
    }

    // fix dis, prof...
    for (int d = 0; d < n; d++)
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                dis[i][j] = min(dis[i][j], dis[i][d]+dis[d][j]);

    for (int a = 0; a < n; a++)
        for (int b = 0; b < n; b++)
            for (int d = 0; d < k; d++)
                if (buy[a][d] != -1 && sell[b][d] != -1)
                    prof[a][b] = max(prof[a][b], sell[b][d]-buy[a][d]);

    printf("%d\n", bs());
    return 0;
}

Compilation message (stderr)

merchant.cpp: In function 'int main()':
merchant.cpp:59:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d %d", &n, &m, &k);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
merchant.cpp:62:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d %d", &buy[i][j], &sell[i][j]);
             ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
merchant.cpp:70:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d %d", &a, &b, &c);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...