답안 #28588

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
28588 2017-07-16T07:43:42 Z 볼빨간 승관이(#1152, sys7961, deneb2016, hyorothy) Alternative Mart (FXCUP2_mart) C++11
1 / 1
843 ms 15504 KB
#include<bits/stdc++.h>

using std::vector;
using std::pair;
using pii = pair<int, int>;
vector<pii> dist[50010];
vector<pii> G[50010];
bool chk[50010];

struct node {
	int d;
	int idx;
	int from;
	bool operator>(const node& p)const {
		return d > p.d;
	}
};
pii has(const vector<pii>& vec, int a) {
	for (auto& b : vec) {
		if (b.second == a)return b;
	}
	return{ -1,-1 };
}
int main() {
	int n, m, k, q;
	scanf("%d%d%d%d", &n, &m, &k, &q);
	std::queue<node> que;
	for (int i = 0; i < k; i++) {
		int a;
		scanf("%d", &a);
		que.push({ 0,a,a });
		dist[a].push_back({ 0,a });
	}
	for (int i = 0; i < m; i++) {
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		G[a].push_back({ b,c });
		G[b].push_back({ a,c });
	}

	while (!que.empty()) {
		int idx = que.front().idx;
		que.pop();
		chk[idx] = false;
		for (pii& p : dist[idx]) {
			int d = p.first;
			int from = p.second;
			for (auto next : G[idx]) {
				int to = next.first;
				int cost = next.second;
				bool has = false;
				for (pii& a : dist[to]) {
					if (a.second == from) {
						has = true;
						if (a.first > d + cost) {
							a.first =  d + cost;
							if (!chk[to]) {
								chk[to] = true;
								que.push({ d + cost, to });
							}
						}
						break;
					}
				}
				if (!has) {
					dist[to].push_back({ d + cost,from });
					if (dist[to].size() > 11) {
						auto it = std::max_element(dist[to].begin(), dist[to].end());
						if (it->second != from) {
							if (!chk[to]) {
								chk[to] = true;
								que.push({ d + cost, to });
							}
						}
						dist[to].erase(it);
					}
					else {
						if (!chk[to]) {
							chk[to] = true;
							que.push({ d + cost, to });
						}

					}
				}

			}
		}

	}
	while (q--) {
		int s, x;
		scanf("%d%d", &s, &x);
		int arr[10];
		for (int i = 0; i < x; i++) {
			scanf("%d", &arr[i]);
			chk[arr[i]] = true;
		}
		std::sort(dist[s].begin(), dist[s].end());
		for (pii& p : dist[s]) {
			if (!chk[p.second]) {
				printf("%d %d\n", p.second, p.first);
				break;
			}
		}
		for (int i = 0; i < x; i++) {
			chk[arr[i]] = false;
		}
	}
}

Compilation message

mart.cpp: In function 'int main()':
mart.cpp:26:35: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d%d%d", &n, &m, &k, &q);
                                   ^
mart.cpp:30:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &a);
                  ^
mart.cpp:36:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d", &a, &b, &c);
                              ^
mart.cpp:92:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &s, &x);
                        ^
mart.cpp:95:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d", &arr[i]);
                        ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 4416 KB Output is correct
2 Correct 3 ms 4416 KB Output is correct
3 Correct 0 ms 4416 KB Output is correct
4 Correct 0 ms 4416 KB Output is correct
5 Correct 0 ms 4416 KB Output is correct
6 Correct 0 ms 4416 KB Output is correct
7 Correct 3 ms 4548 KB Output is correct
8 Correct 0 ms 4548 KB Output is correct
9 Correct 76 ms 4812 KB Output is correct
10 Correct 19 ms 4812 KB Output is correct
11 Correct 19 ms 5472 KB Output is correct
12 Correct 29 ms 5464 KB Output is correct
13 Correct 509 ms 7188 KB Output is correct
14 Correct 413 ms 7188 KB Output is correct
15 Correct 86 ms 7848 KB Output is correct
16 Correct 106 ms 7584 KB Output is correct
17 Correct 243 ms 13656 KB Output is correct
18 Correct 426 ms 14028 KB Output is correct
19 Correct 279 ms 13656 KB Output is correct
20 Correct 466 ms 14032 KB Output is correct
21 Correct 336 ms 13920 KB Output is correct
22 Correct 459 ms 14036 KB Output is correct
23 Correct 346 ms 14184 KB Output is correct
24 Correct 429 ms 14184 KB Output is correct
25 Correct 363 ms 14316 KB Output is correct
26 Correct 379 ms 14316 KB Output is correct
27 Correct 96 ms 8244 KB Output is correct
28 Correct 119 ms 7848 KB Output is correct
29 Correct 416 ms 14712 KB Output is correct
30 Correct 476 ms 14316 KB Output is correct
31 Correct 323 ms 14320 KB Output is correct
32 Correct 439 ms 14048 KB Output is correct
33 Correct 379 ms 14196 KB Output is correct
34 Correct 516 ms 14712 KB Output is correct
35 Correct 429 ms 14448 KB Output is correct
36 Correct 533 ms 14720 KB Output is correct
37 Correct 546 ms 15108 KB Output is correct
38 Correct 426 ms 15108 KB Output is correct
39 Correct 146 ms 8508 KB Output is correct
40 Correct 73 ms 8508 KB Output is correct
41 Correct 393 ms 15108 KB Output is correct
42 Correct 596 ms 15116 KB Output is correct
43 Correct 709 ms 14976 KB Output is correct
44 Correct 793 ms 14976 KB Output is correct
45 Correct 843 ms 14976 KB Output is correct
46 Correct 623 ms 14976 KB Output is correct
47 Correct 589 ms 15240 KB Output is correct
48 Correct 616 ms 15240 KB Output is correct
49 Correct 653 ms 15504 KB Output is correct
50 Correct 729 ms 15504 KB Output is correct
51 Correct 573 ms 15372 KB Output is correct
52 Correct 613 ms 15504 KB Output is correct
53 Correct 263 ms 13392 KB Output is correct
54 Correct 583 ms 14316 KB Output is correct
55 Correct 549 ms 14316 KB Output is correct
56 Correct 713 ms 14844 KB Output is correct
57 Correct 503 ms 6660 KB Output is correct