/*input
4 5 2
10 9 5 2
6 4 20 15
9 7 10 9
-1 -1 16 11
1 2 3
2 3 3
1 4 1
4 3 1
3 1 1
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 105, MAXK = 1005, INF = 1001001000;
const long long INFLL = 1e18;
int N, M, K;
int rawdist[MAXN][MAXN], gain[MAXN][MAXN], buy[MAXN][MAXK], sel[MAXN][MAXK];
long long curdist[MAXN][MAXN], dist1[MAXN];
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
bool check(long long val)
{
FOR(i, 1, N) FOR(j, 1, N) curdist[i][j] = rawdist[i][j] * val - gain[i][j];
FOR(i, 2, N) dist1[i] = INFLL; dist1[1] = 0;
bool cont = true;
for (int trial = 0; trial <= N + 3 && cont; ++trial) {
if (trial == N + 3) return true;
cont = false;
FOR(i, 1, N) if (rawdist[i][1] != INF) FOR(j, 1, N) if (rawdist[j][1] != INF && rawdist[i][j] != INF) if (dist1[i] + curdist[i][j] < dist1[j]) {
dist1[j] = dist1[i] + curdist[i][j];
cont = true;
}
}
FOR(i, 2, N) if (dist1[i] + curdist[i][1] <= 0) return true;
return false;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> N >> M >> K;
FOR(i, 1, N) FOR(j, 1, K) cin >> buy[i][j] >> sel[i][j];
FOR(i, 1, N) FOR(j, 1, N) rawdist[i][j] = INF;
FOR(i, 1, N) rawdist[i][i] = 0;
FOR(_, 0, M - 1) {
int u, v, w;
cin >> u >> v >> w;
rawdist[u][v] = min(rawdist[u][v], w);
}
FOR(k, 1, N) FOR(i, 1, N) FOR(j, 1, N) rawdist[i][j] = min(rawdist[i][j], rawdist[i][k] + rawdist[k][j]);
FOR(i, 1, N) FOR(j, 1, N) FOR(k, 1, K) if (~buy[i][k] && ~sel[j][k]) gain[i][j] = max(gain[i][j], sel[j][k] - buy[i][k]);
int lo = 0, hi = INF;
while (lo < hi) {
int mid = (lo + hi + 1) >> 1;
if (check(mid)) lo = mid;
else hi = mid - 1;
}
cout << lo << endl;
}
Compilation message
merchant.cpp: In function 'bool check(long long int)':
merchant.cpp:27:22: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
^
merchant.cpp:32:2: note: in expansion of macro 'FOR'
FOR(i, 2, N) dist1[i] = INFLL; dist1[1] = 0;
^~~
merchant.cpp:32:33: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
FOR(i, 2, N) dist1[i] = INFLL; dist1[1] = 0;
^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
1276 KB |
Output is correct |
2 |
Correct |
3 ms |
1280 KB |
Output is correct |
3 |
Correct |
20 ms |
1280 KB |
Output is correct |
4 |
Correct |
2 ms |
768 KB |
Output is correct |
5 |
Correct |
3 ms |
768 KB |
Output is correct |
6 |
Correct |
1 ms |
768 KB |
Output is correct |
7 |
Correct |
4 ms |
768 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
768 KB |
Output is correct |
10 |
Correct |
4 ms |
768 KB |
Output is correct |
11 |
Correct |
4 ms |
768 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
768 KB |
Output is correct |
2 |
Correct |
3 ms |
768 KB |
Output is correct |
3 |
Correct |
4 ms |
1024 KB |
Output is correct |
4 |
Correct |
3 ms |
768 KB |
Output is correct |
5 |
Correct |
6 ms |
768 KB |
Output is correct |
6 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
1280 KB |
Output is correct |
2 |
Correct |
97 ms |
1272 KB |
Output is correct |
3 |
Correct |
32 ms |
1280 KB |
Output is correct |
4 |
Correct |
38 ms |
1280 KB |
Output is correct |
5 |
Correct |
47 ms |
1280 KB |
Output is correct |
6 |
Correct |
29 ms |
1280 KB |
Output is correct |
7 |
Correct |
7 ms |
896 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
9 ms |
896 KB |
Output is correct |
10 |
Correct |
6 ms |
768 KB |
Output is correct |
11 |
Correct |
5 ms |
768 KB |
Output is correct |
12 |
Correct |
5 ms |
768 KB |
Output is correct |
13 |
Incorrect |
1 ms |
768 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
768 KB |
Output is correct |
2 |
Correct |
3 ms |
768 KB |
Output is correct |
3 |
Correct |
4 ms |
1024 KB |
Output is correct |
4 |
Correct |
3 ms |
768 KB |
Output is correct |
5 |
Correct |
6 ms |
768 KB |
Output is correct |
6 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |