QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#251298#5081. Forbidden TurnsSamNguyen05#WA 3ms15308kbC++202.4kb2023-11-14 15:27:072023-11-14 15:27:07

Judging History

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

  • [2023-11-14 15:27:07]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:15308kb
  • [2023-11-14 15:27:07]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

const long long INF = 0x1f1f1f1f1f1f1f1f;

template <class T1, class T2>
inline bool minimise(T1 &x, T2 y) {
	if (x > y) { x = y; return true; }
	return false;
}

template <class T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;

template <int N>
class DijkstraGraph {
private:
	int n;
	vector<pair<int, int>> adj[N];

public:
	void init(int _n) {
		n = _n;
		for (int i = 0; i < n; i++)
			adj[i].clear();
	}

	void add_edge(int u, int v, int w) {
		adj[u].emplace_back(v, w);
	}

	long long shortest_path(int s, int t) {
		static long long dist[N];
		min_priority_queue<pair<long long, int>> pq;

		memset(dist, 0x1f, sizeof dist);
		dist[s] = 0;
		pq.emplace(0, s);

		while (not pq.empty()) {
			int u = pq.top().second; pq.pop();
			if (u == t)
				return dist[t];

			for (const auto &e : adj[u]) {
				int v, w;
				tie(v, w) = e;
				if (minimise(dist[v], dist[u] + w))
					pq.emplace(dist[v], v);
			}
		}
		
		return dist[t];
	}
};

const int MAX = 3e4 + 7;
const int MAX_M = 3e5 + 7;
int M, N, K, S, T;

vector<tuple<int, int, int>> edges;
vector<int> enters[MAX], exits[MAX];
vector<pair<int, int>> forbidden[MAX];

void input() {
	cin >> M >> N >> K >> S >> T;
	for (int t = M; t--; ) {
		int u, v, c; cin >> u >> v >> c;
		enters[v].push_back(edges.size());
		exits[u].push_back(edges.size());
		edges.emplace_back(u, v, c);
	}

	for (int t = K; t--; ) {
		int x, y, z; cin >> x >> y >> z;
		forbidden[y].emplace_back(x, z);
	}

	for (int y = 0; y < N; y++)
		sort(forbidden[y].begin(), forbidden[y].end());
}

DijkstraGraph<MAX_M> dgraph;

void solve() {
	dgraph.init(M + 2);

	for (int i = 0; i < M; i++) {
		int u, v, c;
		tie(u, v, c) = edges[i];
		if (u == S)
			dgraph.add_edge(M, i, c);
		if (v == T)
			dgraph.add_edge(i, M + 1, 0);
	}

	for (int u = 0; u < N; u++) {
		for (int i : enters[u]) for (int j : exits[u]) {
			int x, y, c;
			tie(x, ignore, ignore) = edges[i];
			tie(ignore, y, c) = edges[j];

			if (binary_search(forbidden[u].begin(), forbidden[u].end(), make_pair(x, y)))
				continue;

			dgraph.add_edge(i, j, c);
		}
	}

	long long ans = dgraph.shortest_path(M, M + 1);
	if (ans >= INF)
		ans = -1;

	cout << ans;
}
	
int main(void) {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	input();
	solve();

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 15004kb

input:

9 7 3
3 2
6 3 2
3 0 3
0 1 12
1 0 4
1 2 2
1 5 4
4 1 8
5 4 7
5 2 5
0 1 2
4 1 5
1 5 2

output:

36

result:

ok single line: '36'

Test #2:

score: 0
Accepted
time: 3ms
memory: 15264kb

input:

4 4 1
0 3
0 1 2
1 2 3
0 2 7
2 3 10
0 1 2

output:

17

result:

ok single line: '17'

Test #3:

score: 0
Accepted
time: 3ms
memory: 15308kb

input:

4 4 0
0 3
0 1 2
1 2 3
0 2 7
2 3 10

output:

15

result:

ok single line: '15'

Test #4:

score: 0
Accepted
time: 3ms
memory: 15228kb

input:

4 4 1
1 0
1 2 3
2 3 10
3 2 12
2 0 7
1 2 0

output:

32

result:

ok single line: '32'

Test #5:

score: 0
Accepted
time: 1ms
memory: 15008kb

input:

13 8 5
0 5
0 2 3
2 1 7
4 2 10
2 3 10
3 2 12
2 5 10
3 6 10
6 7 5
7 3 5
6 4 10
4 1 0
1 4 10
4 6 10
0 2 1
0 2 5
3 2 5
2 3 2
6 4 2

output:

63

result:

ok single line: '63'

Test #6:

score: 0
Accepted
time: 0ms
memory: 15104kb

input:

4 4 2
1 0
1 2 3
2 3 10
3 2 12
2 0 7
1 2 0
2 3 2

output:

-1

result:

ok single line: '-1'

Test #7:

score: 0
Accepted
time: 0ms
memory: 15100kb

input:

4 4 0
1 0
1 2 3
2 3 2
3 2 3
2 0 7

output:

10

result:

ok single line: '10'

Test #8:

score: -100
Wrong Answer
time: 0ms
memory: 14968kb

input:

0 1 0
0 0

output:

-1

result:

wrong answer 1st lines differ - expected: '0', found: '-1'