最小生成树,边权表示修路钱,修好的路不用花钱设为0
#include<iostream>
#include<cstring>
using namespace std;
const int N = 110;
int g[N][N];
int dist[N];
bool st[N];
int n;
int prime()
{
memset(dist,0x3f,sizeof dist);
int res= 0;
for(int i = 0 ;i < n ; i++)
{
int t = -1;
for(int j = 1 ; j<= n ; j++)
if(!st[j] && (t == -1 || dist[t] > dist[j] ))
t = j;
if(i)res +=dist[t];
st[t] = true;
for(int j =1 ;j <= n ; j++)
dist[j] = min(dist[j],g[t][j]);
}
return res;
}
int main()
{
memset(g,0x3f,sizeof g);
cin >> n;
int m = n*(n-1)/2;
while(m --)
{
int a,b,c,d;
cin >> a >> b >> c >> d;
if(d == 1)
g[a][b] = g[b][a] = 0;
else {
g[a][b] = g[b][a] = c;
}
}
int t =prime();
cout << t << endl;
return 0;
}