답안 #254798

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
254798 2020-07-30T15:35:57 Z model_code Viruses (BOI20_viruses) C++17
100 / 100
89 ms 2688 KB
#include <bits/stdc++.h>
using namespace std;
#define MAXG 305
#define MAXL 55
#define MYINFLL 1000000000000000000LL

#define V(args...) (vector<int>({args}))

static long long a[MAXG][MAXL][MAXL];
static set<pair<long long, pair<int, pair<int, int>>>> q;

void update(int i, int st, int en, long long val) {
	if (val < a[i][st][en]) {
		set<pair<long long, pair<int, pair<int, int>>>>::iterator it = q.find(make_pair(a[i][st][en], make_pair(i, make_pair(st, en))));
		if (it != q.end()) {
			q.erase(it);
		}
		a[i][st][en] = val;
		q.insert(make_pair(val, make_pair(i, make_pair(st, en))));
	}
}

int main() {
	int G, N, M;
	scanf("%d %d %d",&G,&N,&M);
	int origG = G;

	vector<pair<int, vector<int>>> mutations;

	for (int i = 0; i < N; i++) {
		int startGene, len;
		scanf("%d %d", &startGene, &len);

		if (len == 1) {
			int endGene;
			scanf("%d", &endGene);
			mutations.push_back(make_pair(startGene, V(endGene)));
		} else {
			int cur = startGene;
			while (1) {
				int nextGene;
				scanf("%d", &nextGene);
				len--;
				if (len == 1) {
					int finalGene;
					scanf("%d", &finalGene);
					mutations.push_back(make_pair(cur, V(nextGene, finalGene)));
					break;
				}
				mutations.push_back(make_pair(cur, V(nextGene, G)));
				cur = G;
				G++;
			}
		}
	}

	vector<int> mutationsByRhs[MAXG];
	for (int ind = 0; ind < (int) mutations.size(); ind++) {
		vector<int> rhs = mutations[ind].second;
		if ((rhs.size() == 1) || ((rhs.size() == 2) && (rhs[0] == rhs[1]))) {
			mutationsByRhs[rhs[0]].push_back(ind);
		} else {
			mutationsByRhs[rhs[0]].push_back(ind);
			mutationsByRhs[rhs[1]].push_back(ind);
		}
	}

	set<string> keywords;
	set<string> prefixesSet;
	prefixesSet.insert("");

	for (int i = 0; i < M; i++) {
		int len;
		scanf("%d", &len);
		string s = "";
		for (int j = 0; j < len; j++) {
			int elem;
			scanf("%d", &elem);
			s += elem ? "1" : "0";
		}
		keywords.insert(s);
		for (int j = 1; j <= len; j++) {
			prefixesSet.insert(s.substr(0, j));
		}
	}

	vector<pair<string, bool>> prefixes;
	map<string, int> indexes;
	for (string prefix : prefixesSet) {
		bool isTerminal = false;
		for (int i = 0; i < prefix.size(); i++) {
			set<string>::iterator it = keywords.find(prefix.substr(i));
			if (it != keywords.end()) {
				isTerminal = true;
			}
		}
		indexes.insert(make_pair(prefix, (int) prefixes.size()));
		prefixes.push_back(make_pair(prefix, isTerminal));
	}

	int S = (int) prefixes.size();

	for (int i = 0; i < G; i++) {
		for (int st = 0; st < S; st++) {
			for (int en = 0; en < S; en++) {
				a[i][st][en] = MYINFLL;
			}
		}
	}

	for (int st = 0; st < S; st++) {
		if (!prefixes[st].second) {
			for (int i = 0; i < 2; i++) {
				string s = prefixes[st].first + (i == 0 ? "0" : "1");
				while (prefixesSet.find(s) == prefixesSet.end()) {
					s = s.substr(1, s.size() - 1);
				}
				int en = indexes[s];
				if (!prefixes[en].second) {
					update(i, st, en, 1LL);
				}
			}
		}
	}

	while (q.size() > 0) {
		set<pair<long long, pair<int, pair<int, int>>>>::iterator current = q.begin();
		int i = current->second.first;
		int st = current->second.second.first;
		int en = current->second.second.second;
		q.erase(current);

		for (int ind : mutationsByRhs[i]) {
			pair<int, vector<int>> mutation = mutations[ind];
			if (mutation.second.size() == 1) {
				update(mutation.first, st, en, a[i][st][en]);
			} else {
				if (mutation.second[0] == i) {
					for (int md = 0; md < S; md++) {
						update(mutation.first, st, md, a[i][st][en] + a[mutation.second[1]][en][md]);
					}
				}
				if (mutation.second[1] == i) {
					for (int md = 0; md < S; md++) {
						update(mutation.first, md, en, a[mutation.second[0]][md][st] + a[i][st][en]);
					}
				}
			}
		}
	}

	for (int i = 2; i < origG; i++) {
		long long res = MYINFLL;
		for (int en = 0; en < S; en++) {
			if (a[i][0][en] < res) {
				res = a[i][0][en];
			}
		}
		if (res == MYINFLL) {
			printf("YES\n");
		} else {
			printf("NO %lli\n", res);
		}
	}

	return 0;
}

Compilation message

Viruses.cpp: In function 'int main()':
Viruses.cpp:91:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < prefix.size(); i++) {
                   ~~^~~~~~~~~~~~~~~
Viruses.cpp:25:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d %d",&G,&N,&M);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~
Viruses.cpp:32:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &startGene, &len);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
Viruses.cpp:36:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d", &endGene);
    ~~~~~^~~~~~~~~~~~~~~~
Viruses.cpp:42:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &nextGene);
     ~~~~~^~~~~~~~~~~~~~~~~
Viruses.cpp:46:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
      scanf("%d", &finalGene);
      ~~~~~^~~~~~~~~~~~~~~~~~
Viruses.cpp:74:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &len);
   ~~~~~^~~~~~~~~~~~
Viruses.cpp:78:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d", &elem);
    ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 768 KB Output is correct
2 Correct 1 ms 768 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 0 ms 512 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 0 ms 512 KB Output is correct
11 Correct 1 ms 512 KB Output is correct
12 Correct 1 ms 512 KB Output is correct
13 Correct 1 ms 640 KB Output is correct
14 Correct 1 ms 512 KB Output is correct
15 Correct 0 ms 512 KB Output is correct
16 Correct 1 ms 512 KB Output is correct
17 Correct 1 ms 512 KB Output is correct
18 Correct 1 ms 512 KB Output is correct
19 Correct 1 ms 512 KB Output is correct
20 Correct 1 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 768 KB Output is correct
2 Correct 1 ms 768 KB Output is correct
3 Correct 2 ms 1536 KB Output is correct
4 Correct 2 ms 2688 KB Output is correct
5 Correct 3 ms 1536 KB Output is correct
6 Correct 3 ms 1536 KB Output is correct
7 Correct 3 ms 1920 KB Output is correct
8 Correct 4 ms 2048 KB Output is correct
9 Correct 2 ms 1280 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 1152 KB Output is correct
12 Correct 1 ms 768 KB Output is correct
13 Correct 1 ms 1408 KB Output is correct
14 Correct 1 ms 896 KB Output is correct
15 Correct 1 ms 1152 KB Output is correct
16 Correct 1 ms 512 KB Output is correct
17 Correct 1 ms 1024 KB Output is correct
18 Correct 1 ms 1024 KB Output is correct
19 Correct 1 ms 896 KB Output is correct
20 Correct 1 ms 1152 KB Output is correct
21 Correct 1 ms 1024 KB Output is correct
22 Correct 1 ms 640 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 0 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1536 KB Output is correct
2 Correct 2 ms 2688 KB Output is correct
3 Correct 3 ms 1536 KB Output is correct
4 Correct 3 ms 1536 KB Output is correct
5 Correct 3 ms 1920 KB Output is correct
6 Correct 4 ms 2048 KB Output is correct
7 Correct 2 ms 1280 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 1 ms 640 KB Output is correct
10 Correct 1 ms 640 KB Output is correct
11 Correct 1 ms 640 KB Output is correct
12 Correct 0 ms 640 KB Output is correct
13 Correct 1 ms 640 KB Output is correct
14 Correct 0 ms 640 KB Output is correct
15 Correct 0 ms 640 KB Output is correct
16 Correct 0 ms 640 KB Output is correct
17 Correct 1 ms 640 KB Output is correct
18 Correct 1 ms 640 KB Output is correct
19 Correct 1 ms 512 KB Output is correct
20 Correct 1 ms 512 KB Output is correct
21 Correct 0 ms 512 KB Output is correct
22 Correct 1 ms 640 KB Output is correct
23 Correct 1 ms 640 KB Output is correct
24 Correct 1 ms 640 KB Output is correct
25 Correct 14 ms 1152 KB Output is correct
26 Correct 1 ms 1536 KB Output is correct
27 Correct 89 ms 1920 KB Output is correct
28 Correct 6 ms 1920 KB Output is correct
29 Correct 77 ms 1408 KB Output is correct
30 Correct 80 ms 1408 KB Output is correct
31 Correct 11 ms 2024 KB Output is correct
32 Correct 7 ms 2048 KB Output is correct
33 Correct 3 ms 1536 KB Output is correct
34 Correct 89 ms 1528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 768 KB Output is correct
2 Correct 1 ms 768 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 0 ms 512 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 0 ms 512 KB Output is correct
11 Correct 1 ms 512 KB Output is correct
12 Correct 1 ms 512 KB Output is correct
13 Correct 1 ms 640 KB Output is correct
14 Correct 1 ms 512 KB Output is correct
15 Correct 0 ms 512 KB Output is correct
16 Correct 1 ms 512 KB Output is correct
17 Correct 1 ms 512 KB Output is correct
18 Correct 1 ms 512 KB Output is correct
19 Correct 1 ms 512 KB Output is correct
20 Correct 1 ms 512 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 512 KB Output is correct
23 Correct 1 ms 640 KB Output is correct
24 Correct 1 ms 640 KB Output is correct
25 Correct 1 ms 640 KB Output is correct
26 Correct 0 ms 640 KB Output is correct
27 Correct 1 ms 640 KB Output is correct
28 Correct 0 ms 640 KB Output is correct
29 Correct 0 ms 640 KB Output is correct
30 Correct 0 ms 640 KB Output is correct
31 Correct 1 ms 640 KB Output is correct
32 Correct 1 ms 640 KB Output is correct
33 Correct 1 ms 512 KB Output is correct
34 Correct 1 ms 512 KB Output is correct
35 Correct 0 ms 512 KB Output is correct
36 Correct 1 ms 640 KB Output is correct
37 Correct 1 ms 640 KB Output is correct
38 Correct 1 ms 640 KB Output is correct
39 Correct 1 ms 384 KB Output is correct
40 Correct 1 ms 512 KB Output is correct
41 Correct 1 ms 384 KB Output is correct
42 Correct 1 ms 512 KB Output is correct
43 Correct 1 ms 768 KB Output is correct
44 Correct 1 ms 640 KB Output is correct
45 Correct 1 ms 768 KB Output is correct
46 Correct 1 ms 768 KB Output is correct
47 Correct 1 ms 768 KB Output is correct
48 Correct 2 ms 768 KB Output is correct
49 Correct 1 ms 768 KB Output is correct
50 Correct 1 ms 768 KB Output is correct
51 Correct 1 ms 768 KB Output is correct
52 Correct 3 ms 768 KB Output is correct
53 Correct 1 ms 768 KB Output is correct
54 Correct 2 ms 768 KB Output is correct
55 Correct 1 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 768 KB Output is correct
2 Correct 1 ms 768 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 0 ms 512 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 0 ms 512 KB Output is correct
11 Correct 1 ms 512 KB Output is correct
12 Correct 1 ms 512 KB Output is correct
13 Correct 1 ms 640 KB Output is correct
14 Correct 1 ms 512 KB Output is correct
15 Correct 0 ms 512 KB Output is correct
16 Correct 1 ms 512 KB Output is correct
17 Correct 1 ms 512 KB Output is correct
18 Correct 1 ms 512 KB Output is correct
19 Correct 1 ms 512 KB Output is correct
20 Correct 1 ms 512 KB Output is correct
21 Correct 2 ms 1536 KB Output is correct
22 Correct 2 ms 2688 KB Output is correct
23 Correct 3 ms 1536 KB Output is correct
24 Correct 3 ms 1536 KB Output is correct
25 Correct 3 ms 1920 KB Output is correct
26 Correct 4 ms 2048 KB Output is correct
27 Correct 2 ms 1280 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 1152 KB Output is correct
30 Correct 1 ms 768 KB Output is correct
31 Correct 1 ms 1408 KB Output is correct
32 Correct 1 ms 896 KB Output is correct
33 Correct 1 ms 1152 KB Output is correct
34 Correct 1 ms 512 KB Output is correct
35 Correct 1 ms 1024 KB Output is correct
36 Correct 1 ms 1024 KB Output is correct
37 Correct 1 ms 896 KB Output is correct
38 Correct 1 ms 1152 KB Output is correct
39 Correct 1 ms 1024 KB Output is correct
40 Correct 1 ms 640 KB Output is correct
41 Correct 1 ms 384 KB Output is correct
42 Correct 0 ms 384 KB Output is correct
43 Correct 1 ms 512 KB Output is correct
44 Correct 1 ms 640 KB Output is correct
45 Correct 1 ms 640 KB Output is correct
46 Correct 1 ms 640 KB Output is correct
47 Correct 0 ms 640 KB Output is correct
48 Correct 1 ms 640 KB Output is correct
49 Correct 0 ms 640 KB Output is correct
50 Correct 0 ms 640 KB Output is correct
51 Correct 0 ms 640 KB Output is correct
52 Correct 1 ms 640 KB Output is correct
53 Correct 1 ms 640 KB Output is correct
54 Correct 1 ms 512 KB Output is correct
55 Correct 1 ms 512 KB Output is correct
56 Correct 0 ms 512 KB Output is correct
57 Correct 1 ms 640 KB Output is correct
58 Correct 1 ms 640 KB Output is correct
59 Correct 1 ms 640 KB Output is correct
60 Correct 14 ms 1152 KB Output is correct
61 Correct 1 ms 1536 KB Output is correct
62 Correct 89 ms 1920 KB Output is correct
63 Correct 6 ms 1920 KB Output is correct
64 Correct 77 ms 1408 KB Output is correct
65 Correct 80 ms 1408 KB Output is correct
66 Correct 11 ms 2024 KB Output is correct
67 Correct 7 ms 2048 KB Output is correct
68 Correct 3 ms 1536 KB Output is correct
69 Correct 89 ms 1528 KB Output is correct
70 Correct 1 ms 384 KB Output is correct
71 Correct 1 ms 512 KB Output is correct
72 Correct 1 ms 384 KB Output is correct
73 Correct 1 ms 512 KB Output is correct
74 Correct 1 ms 768 KB Output is correct
75 Correct 1 ms 640 KB Output is correct
76 Correct 1 ms 768 KB Output is correct
77 Correct 1 ms 768 KB Output is correct
78 Correct 1 ms 768 KB Output is correct
79 Correct 2 ms 768 KB Output is correct
80 Correct 1 ms 768 KB Output is correct
81 Correct 1 ms 768 KB Output is correct
82 Correct 1 ms 768 KB Output is correct
83 Correct 3 ms 768 KB Output is correct
84 Correct 1 ms 768 KB Output is correct
85 Correct 2 ms 768 KB Output is correct
86 Correct 1 ms 640 KB Output is correct
87 Correct 10 ms 1152 KB Output is correct
88 Correct 0 ms 384 KB Output is correct
89 Correct 1 ms 384 KB Output is correct
90 Correct 1 ms 1152 KB Output is correct
91 Correct 1 ms 896 KB Output is correct
92 Correct 43 ms 1912 KB Output is correct
93 Correct 35 ms 1664 KB Output is correct
94 Correct 2 ms 1536 KB Output is correct
95 Correct 1 ms 1280 KB Output is correct
96 Correct 1 ms 1408 KB Output is correct
97 Correct 11 ms 1408 KB Output is correct
98 Correct 3 ms 1664 KB Output is correct
99 Correct 23 ms 1792 KB Output is correct
100 Correct 2 ms 1536 KB Output is correct
101 Correct 6 ms 1408 KB Output is correct
102 Correct 82 ms 1920 KB Output is correct
103 Correct 7 ms 1920 KB Output is correct
104 Correct 23 ms 1920 KB Output is correct
105 Correct 1 ms 896 KB Output is correct
106 Correct 37 ms 1480 KB Output is correct
107 Correct 47 ms 1408 KB Output is correct