POI X Sums(同余最短路)

Sums

Memory limit: 32 MB

We are given a set of positive integers . Consider a set of non-negative integers , such that a number  belongs to  if and only if  is a sum of some elements from  (the elements may be repeated). For example, if , then sample numbers belonging to the set  are: 0 (the sum of 0 elements), 2, 4  and 12  or  or ; and the following do not belong to : 1 and 3.

Task

Write a program which:

  • reads from the standard input the description of the set  and the sequence of numbers ,
  • for each number  determines whether it belongs to the set ,
  • writes the result to the standard output.

Input

In the first line there is one integer : the number of elements of the set . The following  lines contain the elements of the set , one per line. In the -st line there is one positive integer .

In the -nd line there is one integer . Each of the following  lines contains one integer in the range from  to , they are respectively the numbers .

Output

The output should consist of  lines. The -th line should contain the word TAK ("yes" in Polish), if  belongs to , and it should contain the word NIE ("no") otherwise.

Example

For the input data:

3
2
5
7
6
0
1
4
12
3
2

the correct result is:

TAK
NIE
TAK
TAK
NIE
TAK

这道题的地址为:http://main.edu.pl/en/archive/oi/10/sum 
or
https://szkopul.edu.pl/problemset/problem/4CirgBfxbj9tIAS2C7DWCCd7/site/?key=statement
这是叉姐在他的网站发出来的一道题,我大致理解了叉姐说的是啥意思,自己写了一发,但是这个评测网站,,,不知道是怎样的操作,反正交了题好久好久没有评测结果。
反正思路就是,先把n个数字a_i读入,然后从0开始跑最短路。建好图之后,直接读入k个数字进行判断即可。
其中,最短路的dis[i]表示的是,到达i所需要的最小步数。这个i是大于0小于自己定的一个模数mod的。
取最小的那个a复杂度肯定最小嘛,复杂度是O(mod log mod)。
接下来读入k个数字b_i,那么我们将他对mod取模得t,比对dis[t],如果b比t小,那是无法这么早就到达的,输出NIE,否则肯定有办法到达,输出TAK。
实在不懂,手玩一下样例就很好理解了。
emmmm。。。过了过了23333
代码如下:

#include
#include
#include
#include
#include
using namespace std;
int mod = 50000;
int dis[50005], a[50005];
bool vst[50005];
int n;

void dijkstra() {
	memset(dis, 0x3f, sizeof(dis));
	priority_queue, greater >q;
	dis[0] = 0;
	q.push(0);
	while(!q.empty()) {
		int u= q.top();
		q.pop();
		if(vst[u])
			continue;
		vst[u] = 1;
		for(int i = 0; i < n; i++) {
			if(dis[u] + a[i] < dis[ (u + a[i]) % mod ]) {
				dis[ (u + a[i]) % mod ] = dis[u] + a[i];
				q.push( (u + a[i]) % mod );
			}
		}
	}
}

int main() {
	ios_base::sync_with_stdio(0);
	cin >> n;
	for(int i = 0; i < n; i++) {
		cin >> a[i];
	} 
	sort(a, a + n);
	mod = a[0];//好像直接拿个Min一路比过来也行-- 
	dijkstra();
	int k, t;
	cin >> k;
	while(k--) {
		cin >> t;
		if( t < dis[ (t % mod) ] )
			cout << "NIE\n";
		else
			cout << "TAK\n";
	}
	return 0;
}


你可能感兴趣的:(Dijkstra(点对点),————图算法————)