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<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
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<int, vector<int>, greater<int> >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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
同余最短路是指在模一个数的意义下求解最短路的问题。具体来说,给定一个有向图 $G=(V,E)$,每个边 $e\in E$ 都有一个非负的权值 $w_e$,以及一个模数 $p$。同时给定一个起点 $s$ 和一个终点 $t$。求一条从 $s$ 到 $t$ 的路径,使得路径上所有边权的和在模 $p$ 意义下最小。 证明正确性: 同余最短路算法基于 Bellman-Ford 算法,可以证明其正确性。Bellman-Ford 算法通过松弛每条边 $e$,对于每个顶点 $v$,维护从起点 $s$ 到 $v$ 的最短距离 $d_v$。同余最短路算法也是基于松弛每条边,但是对于每个顶点 $v$,维护从起点 $s$ 到 $v$ 在模 $p$ 意义下的最短距离 $d_v$。 因为同余最短路算法是在模 $p$ 意义下计算距离,所以需要使用模运算。在松弛每条边时,需要更新到达每个顶点的最短距离。具体地,设 $v$ 是边 $e=(u,v)$ 的终点,$w_e$ 是边 $e$ 的边权,则松弛边 $e$ 的操作可以表示为: $$d_v\leftarrow\min(d_v,d_u+w_e\mod p)$$ 其中,$d_u$ 表示从起点 $s$ 到 $u$ 的最短距离。 同余最短路算法的正确性证明可以参考 Bellman-Ford 算法的正确性证明。同余最短路算法的时间复杂度为 $O(VE)$。 代码实现: 以下是同余最短路算法的 Python 代码实现: ```python from collections import deque def shortest_path_mod_p(graph, s, t, p): n = len(graph) INF = float('inf') dist = [INF] * n dist[s] = 0 q = deque([s]) in_queue = [False] * n in_queue[s] = True while q: u = q.popleft() in_queue[u] = False for v, w in graph[u]: d = (dist[u] + w) % p if dist[v] > d: dist[v] = d if not in_queue[v]: q.append(v) in_queue[v] = True return dist[t] ``` 其中,`graph` 是图的邻接表表示,每个元素是一个二元组 `(v, w)`,表示从节点 `u` 到节点 `v` 有一条边权为 `w` 的边。`s` 和 `t` 分别是起点和终点的编号,`p` 是模数。函数返回从起点 `s` 到终点 `t` 在模 `p` 意义下的最短距离。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值