Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Flody算法 700K 76MS

Posted by yuanchuanshun at 2010-05-07 20:39:44 on Problem 1847
/*网上搜的 Flody算法  700K  76MS */
#include<iostream>
using namespace std;
int a[102][102];
//int MAX=1000000;
#define MAX 100000
int N; 
void flody()
{
     int i,j,k;
     for(k=1;k<=N;k++)
         for(i=1;i<=N;i++)
             for(j=1;j<=N;j++)
             a[i][j]=min(a[i][j],a[i][k]+a[k][j]);
}
int main()
{
    int A,B,i,j,k,t;
    cin>>N>>A>>B;
    for(i=1;i<=N;i++)
        for(j=1;j<=N;j++)
            {
               if(i==j) a[i][j]=0;
               else a[i][j]=MAX;
            }
    
    for(i=1;i<=N;i++)
    {
        cin>>k;
        for(j=1;j<=k;j++)
        {
            cin>>t;
            if(j==1) a[i][t]=0;
            else  a[i][t]=1; 
        }
    }
    flody();
    if(a[A][B]==MAX)  cout<<-1<<endl;
    else  cout<<a[A][B]<<endl;
    //while(1);
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator