codeforces Kidnapping

来源:互联网 发布:免费负载均衡软件 编辑:程序博客网 时间:2024/06/05 05:26

原题链接:http://codeforces.com/gym/101246/problem/E



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
40 1 2 01 0 1 02 1 0 20 0 2 031 1 2
output
31 3 4


好久没刷题了,第一道就是一个记忆化搜索。


题意:给你一个n*n的图,每个点表示i到j的距离,然后给出k个值,每个值表示经过的两个点的距离,并且所有的值的顺序是歹徒按顺序走过的点的距离;起点是1,求所有可能的终点。



//某些Online Judge某些题目就会要求从给定文件读入测试数据,或将结果输出到给定文件中,或二者皆有//通过"stdio.h"中的freopen函数实现标准输入输出的重定向#include <cstdio>#include <cstring>#include <iostream>#include <set>using namespace std;int map[205][205];int aa[205];int dp[205][205];set<int> ans;int n, k;void dfs(int s,int step){int i;if (dp[s][step])//如果在相同的起点s遇到了相同的step,就没必要再继续了(肯定重复了)return;if (step == k){ans.insert(s);return;}for (i = 1; i <= n; i++)//存在经过相同路线和同一点的情况,所以每一点都要遍历{if (map[s][i] == aa[step]){dp[s][step] = 1;dfs(i, step + 1);}}}int main(){freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);int i, j;while (~scanf("%d", &n)){ans.clear();memset(dp, 0, sizeof(dp));for (i = 1; i <= n; i++)for (j = 1; j <= n; j++)scanf("%d", &map[i][j]);scanf("%d", &k);for (i = 0; i < k; i++)scanf("%d", &aa[i]);dfs(1,0);printf("%d\n", ans.size());for (set<int>::iterator it = ans.begin(); it != ans.end(); it++){printf(it == ans.begin() ? "%d" : " %d", *it);}printf("\n");}return 0;}


0 0
原创粉丝点击