-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4029.cpp
More file actions
executable file
·60 lines (56 loc) · 1.5 KB
/
Copy path4029.cpp
File metadata and controls
executable file
·60 lines (56 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* 最短路 spfa算法
* 答案数组中dis[k]存储mod m余k的数中可以被凑出的最小总价
**/
#include <iostream>
#include <stdio.h>
using namespace std;
int N, a[1010], M, ask[300233], dis[20233], m = 20233, INF = 41000000, que[10100000];
bool vis[20233]; //访问标记 是否在队中
void spfa() {
int head = 0, rear = 0;
for(int i = 1;i < m;++ i)
dis[i] = INF;
que[head] = 0; ++ rear;
//队空则结束循环
while(head < rear) {
//记录队首
int cur = que[head];
//队首出队
vis[que[head]] = 0;
++ head;
//计算每条边可以到达的节点及路径长度 若短于记录 则更新记录 若vis为0 则入队
for(int i = 0;i < N;++ i) {
//计算
int node = (cur + a[i]) % m;
int tmpdis = dis[cur] + a[i];
//判断
if(tmpdis < dis[node]) {
//更新
dis[node] = tmpdis;
//入队
if(vis[node] == 0) {
que[rear] = node;
++ rear;
vis[node] = 1;
}
}
}
}
}
int main() {
cin >> N;
for(int i = 0;i < N;++ i) {
scanf("%d", &a[i]);
if(a[i] < m) m = a[i];
}
spfa();
cin >> M;
for(int i = 0;i < M;++ i) {
scanf("%d", &ask[i]);
}
for(int i = 0;i < M;++ i) {
if(dis[ask[i] % m] <= ask[i]) printf("YES\n");
else printf("NO\n");
}
}