1338:【例3-3】医院设置
即使今天不圆满,也应该画上句号!
解法相当暴力!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int N=105;
int node[N],t[N],edge[N][N];
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
int left,right;
scanf("%d%d%d",node+i,&left,&right);
edge[i][left]=edge[left][i]=edge[i][right]=edge[right][i]=1;
}
int ans=0x0fffffff;
//遍历每一个结点,找到最优值
for(int i=1;i<=n;i++){
int sum=0,path=1;
memset(t,0,sizeof(t));
queue<int> q;
q.push(i);
t[i]=1;
while(!q.empty()){
int cnt=q.size();
for(int j=1;j<=cnt;j++){
int v=q.front();
q.pop();
for(int k=1;k<=n;k++){
if(!t[k]&&edge[v][k]){
q.push(k);
t[k]=1;
sum+=node[k]*path;
}
}
}
path++;
}
ans=min(ans,sum);
}
cout<<ans;
return 0;
}