POJ 1847 Tram【Floyd】

题意:给出n个站点,每个站点都有铁路通向其他站点 如果当前要走得路恰好是该站点的开关指向的铁路,则不用扳开关,否则要手动扳动开关,给出起点和终点,问最少需要扳动多少次开关

输入的第一行是n,start,end

接下来的n行,每一行中,第一个数是该站点向外连接的铁路条数,

第二个数是该站点的开关指向的铁路(因为这儿没有读懂= =所以都建不出图来--5555参见这一句话:Switch in the i-th intersection is initially pointing in the direction of the first intersection listed.)

所以直接指向的w=0(即为不需要扳动开关)

没有直接指向的w=1(即为需要扳动开关)

建出图来,再求最短路就可以了

因为n<=100,所以可以用floyd来做

学习的这一篇:http://blog.csdn.net/freezhanacmore/article/details/8619040

 1 #include<iostream>  
 2 #include<cstdio>  
 3 #include<cstring> 
 4 #include <cmath> 
 5 #include<stack>
 6 #include<vector>
 7 #include<map> 
 8 #include<set>
 9 #include<queue> 
10 #include<algorithm>  
11 #define mod=1e9+7;
12 using namespace std;
13 
14 typedef long long LL;
15 const int INF = 10000000;
16 const int maxn=110;
17 int d[maxn][maxn];
18 
19 int main(){
20     int n,a,b,i,j,k,num,x;
21     scanf("%d %d %d",&n,&a,&b);
22         for(i=1;i<=n;i++){ //初始化 
23             for(j=1;j<=n;j++){
24                 if(i==j) d[i][j]=0;
25                 else d[i][j]=INF;
26             }
27         }
28         
29         for(i=1;i<=n;i++){//建图 
30             scanf("%d",&num);
31             for(j=1;j<=num;j++){
32                 scanf("%d",&x);
33                 if(j==1) d[i][x]=0;

34                 else d[i][x]=1;
35             }            
36         }
37         
38         for(k=1;k<=n;k++)
39           for(i=1;i<=n;i++)
40             for(j=1;j<=n;j++)
41             d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
42             
43             if(d[a][b]==INF) printf("-1
");
44             else printf("%d
",d[a][b]);
45             
46             return 0;
47 }
View Code

第一道最短路的题目--

go--go--go

原文地址:https://www.cnblogs.com/wuyuewoniu/p/4394615.html