房子供水问题最小成本
#include
#include
using namespace std;
#define N 10000
struct Node
{
int u;
int v;
int cost;
int flag;
}Tree[N];
int compare(int a, int b)
{
return a > b;
}
int main()
{
int n;
cin >> n;
int well[N];
for (int i = 1; i <= n; i++)
cin >> well[i];
int m = n - 1;
int vist[N] = { 0 };
int pre[N];
for (int i = 1; i <= n; i++)
pre[i] = 999999999;
int k = 1;
int min_sum = 0;
int min = 99999999;
while (m–)
{
int u, v, cost;
cin >> u >> v >> cost;
Tree[k].u = u;
Tree[k].v = v;
Tree[k].cost = cost;
Tree[k].flag = 0;
if (cost > well[v] && cost > well[u])//两房都建井或者铺一方引水渠
{
if (vist[u] != 1 && vist[v]!=1)pre[k] = well[u] + well[v];//两房都建井
if (vist[u] != 1 && vist[v] == 1)pre[k] = well[u];
if (vist[v] == 1 && vist[v] != 1)pre[k] = vist[v];
vist[v] = 1;
vist[u] = 1;
}
else//铺水渠
{
if (compare(well[u], well[v]))
{
if (vist[v] == 1)
{
pre[k] = cost;//引水渠
}
else
{
pre[k] = cost + well[v];//建井引水渠
}
Tree[k].flag = 1;
vist[u] = 1;
vist[v] = 1;
}
else
{
if (vist[u] == 1)
{
pre[k] = cost;
}
else
{
pre[k] = cost + well[u];
}
Tree[k].flag = 1;
vist[u] = 1;
vist[v] = 1;
}
}
min_sum += pre[k];
}
cout << min_sum << endl;
return 0;
}