QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#55034#1274. Walking PlanYaoBIGTL 26ms7372kbC++2.4kb2022-10-12 00:28:272022-10-12 00:28:28

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-10-12 00:28:28]
  • 评测
  • 测评结果:TL
  • 用时:26ms
  • 内存:7372kb
  • [2022-10-12 00:28:27]
  • 提交

answer

#include "bits/stdc++.h"
#define rep(i, a, n) for (auto i = a; i <= (n); i++)
#define revrep(i, a, n) for (auto i = n; i >= (a); i++)
#define all(a) a.begin(), a.end()
#define sz(a) (int)(a).size()
template<class T> bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } else return 0; }
template<class T> bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } else return 0; }
using namespace std;

template<class A, class B> string to_string(pair<A, B> p);
template<class A> string to_string(A v) {
	bool first = 1;
	string res = "{";
	for (auto x: v) {
		if (!first) res += ", ";
		first = 0;
		res += to_string(x);
	}
	res += "}";
	return res;
}
template<class A, class B> string to_string(pair<A, B> p) {
	return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

void debug_out() { cerr << endl; }
template<class H, class... T> void debug_out(H h, T... t) {
	cerr << " " << to_string(h);
	debug_out(t...);
}

#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

const int MatN = 50;
using Vec = array<ll, MatN>;
using Mat = array<Vec, MatN>;
const ll inf = 1ll << 60;

Mat operator *(const Mat &a, const Mat &b) {
	Mat c{};
	rep(i, 0, MatN - 1) rep(j, 0, MatN - 1) c[i][j] = inf;
	rep(i, 0, MatN - 1) rep(k, 0, MatN - 1) rep(j, 0, MatN - 1) chmin(c[i][j], a[i][k] + b[k][j]);
	return c;
}

int main() {
	ios::sync_with_stdio(0); cin.tie(0);

	int cas; cin >> cas; while (cas--) {
		int n, m; cin >> n >> m;
		vector<vector<ll>> d(n, vector<ll>(n, inf));
		rep(i, 0, n - 1) d[i][i] = 0;
		rep(i, 1, m) {
			int x, y, w; cin >> x >> y >> w;
			x--, y--;
			chmin(d[x][y], 1ll * w);
		}
		rep(k, 0, n - 1) rep(i, 0, n - 1) rep(j, 0, n - 1) chmin(d[i][j], d[i][k] + d[k][j]);
		rep(i, 0, n - 1) {
			d[i][i] = inf;
			rep(j, 0, n - 1) if (j != i) chmin(d[i][i], d[i][j] + d[j][i]);
		}

		Mat A{};
		rep(i, 0, MatN - 1) rep(j, 0, MatN - 1) A[i][j] = i == j ? 0 : inf;
		vector<Mat> ss(101, A);
		vector<Mat> bs(101, A);
		rep(i, 0, n - 1) rep(j, 0, n - 1) A[i][j] = d[i][j];
		rep(i, 1, 100) ss[i] = ss[i - 1] * A;
		A = ss[100];
		rep(i, 1, 100) bs[i] = bs[i - 1] * A;
		int q; cin >> q;
		while (q--) {
			int x, y, k; cin >> x >> y >> k;
			x--, y--;
			auto c = bs[k / 100] * ss[k % 100];
			ll ans = c[x][y] < inf ? c[x][y] : -1;
			printf("%lld\n", ans);
		}
	}
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 26ms
memory: 7372kb

input:

2
3 3
1 2 1
2 3 10
3 1 100
3
1 1 1
1 2 1
1 3 1
2 1
1 2 1
1
2 1 1

output:

111
1
11
-1

result:

ok 4 lines

Test #2:

score: -100
Time Limit Exceeded

input:

10
5 10
4 2 55
2 1 26
3 4 83
5 2 16
4 5 54
3 4 38
2 1 68
2 5 1
4 5 85
4 2 60
20
2 1 13
1 4 17
3 2 20
2 4 16
4 2 17
4 2 2
3 1 2
1 5 10
2 1 8
4 5 15
4 2 16
3 1 18
5 2 7
4 2 9
4 3 16
1 4 18
3 2 5
1 5 9
5 1 19
1 2 16
20 50
4 16 25
3 16 990
9 18 863
19 12 236
4 10 360
13 4 585
14 17 164
8 18 198
2 17 804...

output:

128
-1
246
-1
191
70
119
-1
94
173
189
253
67
123
-1
-1
125
-1
195
-1
-1
23449
3476
18735
17412
23124
29499
26452
9757
21128
9524
-1
4486
8797
8041
33717
32669
5108
32534
13020
22800
4411
3529
37460
4191
15863
5342
22005
-1
14496
16535
13644
-1
33956
28717
24721
13816
26289
8840
28137
9991
14430
-1
...

result: