Luogu1099 树网的核 (暴力?,floyd?)(还未想正解,暴力就A了)
阅读理解两小时,手敲暴力思考5分钟。然后\(n^3\)就A了
暴力代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long
#define ON_DEBUG
#ifdef ON_DEBUG
#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin);
#else
#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;
#endif
struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std;
const int N = 307;
int dis[N][N], D[N];
int vis[N];
int main(){
//FileOpen();
int n, S;
io >> n >> S;
R(i,1,n){
R(j,1,n){
if(i != j)
dis[i][j] = 2147483647;
}
}
R(i,2,n){
int u, v, w;
io >> u >> v >> w;
dis[u][v] = dis[v][u] = w;
}
int DIS = 0;
R(k,1,n){
R(i,1,n){
if(dis[i][k] == 2147483647) continue;
R(j,1,n){
if(dis[k][j] == 2147483647) continue;
dis[i][j] = Min(dis[i][j], dis[i][k] + dis[k][j]);
DIS = Max(DIS, dis[i][j]);
}
}
}
R(i,1,n){
R(j,1,n){
if(dis[i][j] == DIS){
R(k,1,n){
if(dis[i][k] + dis[k][j] == dis[i][j]){
vis[k] = true;
}
}
}
}
}
int ans = 2147483647;
R(i,1,n){
R(j,i,n){
if(vis[i] && vis[j] && dis[i][j] <= S){
R(xx,1,n) D[xx] = 2147483647;
R(k,1,n){
if(dis[i][k] + dis[k][j] == dis[i][j]){
R(l,1,n){
D[l] = Min(D[l], dis[k][l]);
}
}
}
int maxx = 0;
R(k,1,n){
maxx = Max(maxx, D[k]);
}
ans = Min(ans, maxx);
}
}
}
printf("%d", ans);
return 0;
}