답안 #28637

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
28637 2017-07-16T08:15:21 Z Official Fan of ACG(#1221, cseteram, 16silver, pps789) Alternative Mart (FXCUP2_mart) C++14
1 / 1
3349 ms 39448 KB
#include<cstdio>
#include<algorithm>
#include<queue>
#include<functional>
#include<set>
#include<iterator>
using namespace std;

const int INF = 0x3fFFffFF;
const int NMAX = 50000, KMAX = 11;
typedef pair<int, int> pii;
typedef pair<pii, int> tri;
set<pii> dp[NMAX + 1];
vector<pii> g[NMAX + 1];
vector<int> S;

int main() {
	int N, M, K, Q; scanf("%d%d%d%d", &N, &M, &K, &Q);
	for (int i = 0; i < K; i++) {
		int x; scanf("%d", &x); S.push_back(x);
	}

	for (int i = 0; i < M; i++) {
		int u, v, w; scanf("%d%d%d", &u, &v, &w);
		g[u].push_back({ v,w });
		g[v].push_back({ u,w });
	}

	priority_queue<tri, vector<tri>, greater<tri>> pq;
	for (int s : S) {
		pq.push(tri(pii(0, s), s));
		dp[s].insert(pii(0, s));
	}
	while (!pq.empty()) {
		auto t = pq.top(); pq.pop();
		int here = t.second;
		int cost = t.first.first, from = t.first.second;
		if (dp[here].count({ cost,from })) {
			for (auto edge : g[here]) {
				int there = edge.first, ncost = edge.second + cost;
				pii nstate = { ncost, from };
				bool ok = dp[there].size() < KMAX || nstate < (*dp[there].rbegin());
				bool coin = false;
				for (const auto& p : dp[there]) if (p.second == from) {
					if (p.first <= ncost) ok = false;
					else coin = true;
				}
				if (ok) {
					dp[there].insert(nstate);
					if (coin) {
						for (auto it = dp[there].begin(); it != dp[there].end(); it++) {
							if (it->second == from && (*it) != nstate) {
								dp[there].erase(it);
								break;
							}
						}
					}
					if (dp[there].size() > KMAX) dp[there].erase(prev(dp[there].end()));
					pq.push(tri(pii(ncost, from), there));
				}
			}
		}
	}

	for (int q = 0; q < Q; q++) {
		int st, X; scanf("%d%d", &st, &X);
		set<int> fails;
		for (int i = 0; i < X; i++) {
			int f; scanf("%d", &f); fails.insert(f);
		}

		for (const auto& p : dp[st]) if (!fails.count(p.second)) {
			printf("%d %d\n", p.second, p.first); break;
		}
	}
}

Compilation message

mart.cpp: In function 'int main()':
mart.cpp:18:51: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  int N, M, K, Q; scanf("%d%d%d%d", &N, &M, &K, &Q);
                                                   ^
mart.cpp:20:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int x; scanf("%d", &x); S.push_back(x);
                         ^
mart.cpp:24:43: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int u, v, w; scanf("%d%d%d", &u, &v, &w);
                                           ^
mart.cpp:66:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int st, X; scanf("%d%d", &st, &X);
                                    ^
mart.cpp:69:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    int f; scanf("%d", &f); fails.insert(f);
                          ^
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4696 KB Output is correct
2 Correct 0 ms 4696 KB Output is correct
3 Correct 3 ms 4696 KB Output is correct
4 Correct 3 ms 4696 KB Output is correct
5 Correct 0 ms 4696 KB Output is correct
6 Correct 3 ms 4696 KB Output is correct
7 Correct 3 ms 4960 KB Output is correct
8 Correct 0 ms 4960 KB Output is correct
9 Correct 59 ms 5572 KB Output is correct
10 Correct 26 ms 5504 KB Output is correct
11 Correct 66 ms 7508 KB Output is correct
12 Correct 43 ms 7468 KB Output is correct
13 Correct 676 ms 13820 KB Output is correct
14 Correct 733 ms 13828 KB Output is correct
15 Correct 103 ms 8992 KB Output is correct
16 Correct 96 ms 8656 KB Output is correct
17 Correct 1246 ms 30816 KB Output is correct
18 Correct 836 ms 29776 KB Output is correct
19 Correct 1679 ms 33956 KB Output is correct
20 Correct 819 ms 32020 KB Output is correct
21 Correct 1556 ms 33972 KB Output is correct
22 Correct 993 ms 32112 KB Output is correct
23 Correct 1349 ms 35656 KB Output is correct
24 Correct 1309 ms 33816 KB Output is correct
25 Correct 1126 ms 35656 KB Output is correct
26 Correct 1696 ms 33816 KB Output is correct
27 Correct 119 ms 9488 KB Output is correct
28 Correct 119 ms 9200 KB Output is correct
29 Correct 1156 ms 31932 KB Output is correct
30 Correct 769 ms 30744 KB Output is correct
31 Correct 1176 ms 35560 KB Output is correct
32 Correct 1686 ms 32976 KB Output is correct
33 Correct 1946 ms 34020 KB Output is correct
34 Correct 1563 ms 35904 KB Output is correct
35 Correct 1413 ms 35828 KB Output is correct
36 Correct 1676 ms 35964 KB Output is correct
37 Correct 1729 ms 39196 KB Output is correct
38 Correct 1596 ms 36012 KB Output is correct
39 Correct 99 ms 10012 KB Output is correct
40 Correct 96 ms 10040 KB Output is correct
41 Correct 1559 ms 33816 KB Output is correct
42 Correct 1533 ms 33848 KB Output is correct
43 Correct 2196 ms 36192 KB Output is correct
44 Correct 1893 ms 36088 KB Output is correct
45 Correct 1723 ms 36164 KB Output is correct
46 Correct 1676 ms 36156 KB Output is correct
47 Correct 3349 ms 39448 KB Output is correct
48 Correct 2233 ms 39432 KB Output is correct
49 Correct 2373 ms 39448 KB Output is correct
50 Correct 2076 ms 39432 KB Output is correct
51 Correct 2543 ms 39448 KB Output is correct
52 Correct 2626 ms 39432 KB Output is correct
53 Correct 506 ms 32020 KB Output is correct
54 Correct 1416 ms 35816 KB Output is correct
55 Correct 1833 ms 35828 KB Output is correct
56 Correct 2106 ms 36172 KB Output is correct
57 Correct 583 ms 10192 KB Output is correct