Gym

来源:互联网 发布:北京市java招聘 编辑:程序博客网 时间:2024/05/17 21:50
input.txt / output.txt
Statements

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 aren 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 number1.

Input

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

It is guaranteed that 0 ≤ lij ≤ 200,lii = 0 andlij = lji. The next line contains one integerk (1 ≤ k ≤ 200) — the number of roads passed by the couch. The following line containsk integers r1,r2, ..., 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 integer0.

Example
Input
40 1 2 01 0 1 02 1 0 20 0 2 031 1 2
Output
3

1 3 4

题意:

抓劫匪的题,最后总结为有n条路,a[x][y]表示第x条路与第y条路之间的距离,由于绑匪将人绑着,所以人只能计算出走的路程给出计算的路程问最后到达的劫匪窝点可能是那条街(每次都是从第一条路开始)

思路:直接搜索就行了,输入一个数把可能的点标记,然后再输入后来的数,但每次标记时需要把以前标记过得请0,最后被标记的那些点就是可能的点

ac代码:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int main(){    freopen("input.txt","r",stdin);    freopen("output.txt","w",stdout);    int a[205][205],b[205],c[205],n,m,h;    while(cin>>n)    {        for(int i=1;i<=n;i++)        {            for(int j=1;j<=n;j++)            cin>>a[i][j];        }        cin>>m;        memset(b,0,sizeof(b));        memset(c,0,sizeof(c));        b[1]=1;        for(int i=1;i<=m;i++)        {          cin>>h;          for(int j=1;j<=n;j++)          {              if(b[j])              {                  for(int k=1;k<=n;k++)                  {                      if(a[j][k]==h)                      c[k]=1;                  }              }          }          for(int j=1;j<=n;j++)          b[j]=c[j];          memset(c,0,sizeof(c));        }        int sum=0;        for(int i=1;i<=n;i++)        {            if(b[i]==1)            {                c[sum++]=i;            }        }        cout<<sum<<endl;        if(sum>0)        {            for(int i=0;i<sum-1;i++)            printf("%d ",c[i]);            printf("%d\n",c[sum-1]);        }    }    return 0;}

0 0