标签:
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 11159 | Accepted: 4089 |
Description
Input
Output
Sample Input
3 2 1 2 2 3 2 3 1 2 1 2
Sample Output
0
Source
1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 #define N 1000000 5 int map[101][101]; 6 int main() 7 { 8 int n,sta,end,m,a; 9 int i,j; 10 11 scanf("%d%d%d",&n,&sta,&end); 12 for(i=1; i<=n; i++) 13 for(j=1; j<=n; j++) 14 map[i][j]=N; 15 for(i=1; i<=n; i++) 16 { 17 scanf("%d",&m); 18 for(j=1; j<=m; j++) 19 { 20 scanf("%d",&a); 21 if(j==1) 22 map[i][a]=0; 23 else 24 map[i][a]=1; 25 } 26 } 27 28 int k; 29 for(k=1; k<=n; k++) //floyd算法 30 for(i=1; i<=n; i++) 31 for(j=1; j<=n; j++) 32 if(map[i][k]+map[k][j]<map[i][j]) 33 map[i][j]=map[i][k]+map[k][j]; 34 35 if(map[sta][end]<N) 36 cout<<map[sta][end]<<endl; 37 else 38 cout<<-1<<endl; 39 return 0; 40 }
标签:
原文地址:http://www.cnblogs.com/jasonlixuetao/p/4540177.html