poj 3145 The Domino Principle(bfs)

poj 3145 The Domino Principle(bfs)
Time Limit: 1000ms Memory Limit: 65536kB

Description
There is a configuration of N domino stones that is to be beautifully destroyed. For each stone i in the given configuration, we know the set Di of the stones that will fall at time t+1 (if they have not fallen before) if the stone i falls at time t.
Write a program to determine which stone should be pushed to fall at the time 0 to achieve complete destruction of the configuration that takes the longest possible time.

Input
The first line of the input contains the number of stones N in the configuration (1≤N≤1000). Next N lines describe sets Di (1≤i≤N). Each of these lines contains first the potency of the set Di and then the numbers of stones in the set separated by one or more spaces.

Output
The first line of the output must contain the time of fall of the last stone in the configuration, and the second line - the number of the stone that should be pushed first. If the maximal time can be achieved starting from several initial stones, output the maximal possible number. If the configuration can’t be destroyed completely, the first and only line of the file must contain the word ‘impossible’.

Sample Input

3
2 2 3
1 3
0

Sample Output

1
1


这应该算一个比较简单的题目,思路很简单,可以用最短路Floyd(但是会TLE)也可以用搜索bfs做。但是非常遗憾,两种方法都没能AC,而且晚上能找到的为数不多的题解都WA了,但是在poj上本题的确有人AC,题目也许有问题,也不知道哪一个小细节那么坑。
已知的小细节边界条件有输入1 0输出0 1.
也不知道我的bfs代码错在哪里,如果做过这个题知道其他什么注意点的还请指教,谢谢。


poj疑似有问题没有AC

#define MAX_N 1000 

#include<stdio.h>
#include<memory.h>

int n;
int d[MAX_N+1],suc[MAX_N+1][MAX_N+1];
int max_time=-1,first;
int visit[MAX_N+1],queue[MAX_N][2],head,tail,now;

int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
    {
        scanf("%d",&d[i]);
        for (int j=1;j<=d[i];j++)
            scanf("%d",&suc[i][j]);
    }
    for (int i0=1;i0<=n;i0++)
    {
        memset(visit,0,sizeof(visit));
        visit[i0]=true;
        queue[0][0]=i0;
        queue[0][1]=0;
        head=tail=0;
        while (head<=tail)
        {
            now=queue[head][0];
            for (int i=1;i<=d[now];i++)
                if (!visit[suc[now][i]])
                {
                    visit[suc[now][i]]=true;
                    tail++;
                    queue[tail][0]=suc[now][i];
                    queue[tail][1]=queue[head][1]+1;
                }
            head++;
            /*
            for (int i=0;i<=tail;i++)
                printf("%d(%d) ",queue[i][0],queue[i][1]);
            printf("\n");
            */
        }   
        if ((head==n)&&(queue[tail][1]>=max_time))
        {
            first=i0;
            max_time=queue[tail][1];
        }
    }
    if (max_time==-1)
        printf("impossible\n");
    else
        printf("%d\n%d\n",max_time,first);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值