codeforces E. Kidnapping(记忆化搜索)

E. Kidnapping
time limit per test
1 second
memory limit per test
256 megabytes
input
input.txt
output
output.txt

Berland's Police has a serious problem. A foreign ambassador arrived to Berland with an important mission, and his daughter was kidnapped just from the Royal Palace! Inspired by adventures of Erast Fandorin, the Police Chief developed the following ingenious plan.

The ambassador agrees to pay ransom, but only if the kidnappers allow his servant to visit the girl and ensure that she is alive. The kidnappers take the blindfolded servant into a coach and transport him to the secret place, where they keep the ambassador's daughter. Certainly, the role of the servant is certainly played by a secret agent of the Police. The Police Chief knows that when the coach is moving, the wheels are creaking once on each full rotation. So, by counting the number of creaks and multiplying it by the length of the rim, one can easily calculate the distance covered by the coach.

In spite of this brilliant idea, the affair turned to be much more difficult than it could be in a detective story. There are n intersections in the city numbered from 1 to n, some pairs of intersections are connected by bidirectional roads. The kidnappers agreed to take the "servant" to the secret place, and the servant is quite sure that this place is located at one of the intersections. Also the agent has calculated the lengths of roads between each pair of consecutive intersections on the route passed by the coach. But during the trip the agent was concentrated on counting creaks, so he could not remember in which directions the coach turned at the intersections.

Now the route probably couldn't be restored uniquely! Moreover, the agent has a suspicion that the kidnappers could intentionally pass the same intersection or even the same road more than once to confuse the Police.

Your task is to determine all possible locations of the secret place, given that the trip starts at the intersection number 1.

Input

The first line of the input contains a single integer n (2 ≤ n ≤ 200). Each of the next n lines contains n integers each. The i-th number in the j-th line lij is the length of the road between the i-th and the j-th intersections. If lij = 0 then the road doesn't exist.

It is guaranteed that 0 ≤ lij ≤ 200lii = 0 and lij = lji. The next line contains one integer k (1 ≤ k ≤ 200) — the number of roads passed by the couch. The following line contains k integers r1r2, ..., rk (1 ≤ ri ≤ 200) — the lengths of roads between each pair of consecutive intersections on the route passed by the coach from the starting point to the secret place.

Output

To the first line of the output write m — the number of all possible locations of the secret place. The second line should contain the numbers of intersections in increasing order separated by spaces.

If there are no possible locations of the secret place, the output must contain the only integer 0.

Examples
input
4
0 1 2 0
1 0 1 0
2 1 0 2
0 0 2 0
3
1 1 2
output
3
1 3 4


tips:被codeforces的输入输出重定向给坑了,第一次用codeforces

说下这道题。。。题目的叙述很长。。但是题意出奇的简单,因为涉及到大量的重复搜索,使用到了记忆化搜索,具体是如果dp[i][j]深度为i的j节点如果已经进行过了搜索,便不再搜索
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<fstream>
#include<cstdlib>
#include<set>
using namespace std;
set<int>ans;
int k,n;
vector<int>g[222];
int map[222][222];
int a[222];
int dp[222][222];
void dfs(int x,int step)
{
	if(dp[step][x])return ;
	else dp[step][x]=1;
	
	if(step==k+1)
	{
		ans.insert(x);
		return;
	}
	
	int ret=0;
	for(int i=0;i<g[x].size();i++)
	{
		int nx=g[x][i];
		if(map[x][nx]==a[step])
		{
			dfs(nx,step+1);
		}
	}
}
int main()
{
	freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
cin>>n;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			//cin>>map[i][j];
			scanf("%d",&map[i][j]);
			if(map[i][j])g[i].push_back(j);
		}
	}
	scanf("%d",&k);
	for(int i=1;i<=k;i++)scanf("%d",&a[i]);//cin>>a[i];
	dfs(1,1);
	
	cout<<ans.size()<<endl;
	if(!ans.size())return 0;
	for(set<int>::iterator it=ans.begin();it!=ans.end();++it)
	{
		printf("%d ",*it);
	}
	cout<<endl;
	return 0;
 } 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您提供的链接是Codeforces的一个问题,问题编号为104377。Codeforces是一个知名的在线编程竞赛平台,经常举办各种编程比赛和训练。Gym是Codeforces的一个扩展包,用于组织私人比赛和训练。您提供的链接指向了一个问题的页面,但具体的问题内容和描述无法通过链接获取。如果您有具体的问题或需要了解关于Codeforces Gym的更多信息,请提供更详细的信息。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [http://codeforces.com/gym/100623/attachments E题](https://blog.csdn.net/weixin_30820077/article/details/99723867)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [http://codeforces.com/gym/100623/attachments H题](https://blog.csdn.net/weixin_38166726/article/details/99723856)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [CodeforcesPP:Codeforces扩展包](https://download.csdn.net/download/weixin_42101164/18409501)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值