某国有n个城市需要通气,若在第i个城市打井的成本为w[i].也可以在两个城市间打管道,成本为p[i][j].那么城市i和j都看作可以通气的,求所有城市通气的最小成本之和
第一行输入n
接下来n行,每行输入一个w[i] (1<=w[i]<=100000)
再接下来n行,每行n个p[i][j]用空格隔开 (1<=p[i][j]<=100000)
最小成本值
4
5
4
4
3
0 2 2 2
2 0 3 3
2 3 0 4
2 3 4 0
9(不知道为什么输出了7,请大佬们帮忙看看,代码放在下面)
#include<bits/stdc++.h>
using namespace std;
const int N=305;
int wall[N];
struct under{
int num,x,y;
}gas[N*N];
bool send[N];
int n,m=1,k;
int mi=INT_MAX,mii;
bool judge(){
for(int i=0;i<n;i++){
if(send[i]==false) return false;
}
return true;
}
int main(){
cin>>n;
for(int i=0;i<n;i++){
cin>>wall[i];
if(wall[i]<mi){
mi=wall[i];
send[mii]=false;
send[i]=true;
mii=i;
}
}
// cout<<mi<<endl;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>k;
if(i==j) continue;
gas[m].num=k;
gas[m].x=i;
gas[m].y=j;
m++;
}
}
for(int i=0;i<n*n;i++){
for(int j=i;j<n*n;j++){
if(gas[i].num>gas[j].num){
swap(gas[i],gas[j]);
}
}
}
for(int i=0;i<n*n;i++){
if(!judge()){
if(!send[gas[i].x]||!send[gas[i].y]){
send[gas[i].x]=true;
send[gas[i].y]=true;
mi+=gas[i].num;
// cout<<mi<<" "<<gas[i].x<<" "<<gas[i].y<<endl;
}
else{
continue;
}
}
else{
break;
}
}
cout<<mi;
return 0;
}
谢谢!