标签:class close space stream sync int bsp 复杂 click
如题,非常巧妙的一道图论*倍增,n <= 50 所以可以用高复杂度的Floyd搞。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 5 using namespace std; 6 7 int ans = (1<<31)-1; 8 int n,m; 9 int g[36][60][60]; 10 int ng[60][60]; 11 12 int main(){ 13 ios::sync_with_stdio(false); 14 memset(ng,0x3f,sizeof(ng)); 15 cin >> n >> m; 16 for(int i = 1;i <= m;i++){ 17 int x,y; 18 cin >> x >> y; 19 g[0][x][y] = 1; 20 } 21 for(int k = 1;k < 36;k++) 22 for(int i = 1;i <= n;i++) 23 for(int j = 1;j <= n;j++) 24 for(int t = 1;t <= n;t++) 25 g[k][i][j] |= g[k-1][i][t]&g[k-1][t][j]; 26 27 for(int k = 0;k < 36;k++) 28 for(int i = 1;i <= n;i++) 29 for(int j = 1;j <= n;j++) 30 if(g[k][i][j])ng[i][j] = 1; 31 32 for(int k = 1;k <= n;k++) 33 for(int i = 1;i <= n;i++) 34 for(int j = 1;j <= n;j++) 35 if(ng[i][j] > ng[i][k]+ng[k][j]) 36 ng[i][j] = ng[i][k] + ng[k][j]; 37 38 cout << ng[1][n]; 39 return 0; 40 }
标签:class close space stream sync int bsp 复杂 click
原文地址:https://www.cnblogs.com/Wangsheng5/p/11651316.html