灌水(最小生成树)(Irrigation (minimum spanning tree))-其他
灌水(最小生成树)(Irrigation (minimum spanning tree))
Farmer John已经决定把水灌到他的n(1<=n<=300)块农田,农田被数字1到n标记。把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水库。建造一个水库需要花费wi(1<=wi<=100000),连接两块土地需要花费Pij(1<=pij<=100000,pij=pji,pii=0). 计算Farmer John所需的最少代价。
Input
*第一行:一个数n
*第二行到第n+1行:第i+1行含有一个数wi
*第n+2行到第2n+1行:第n+1+i行有n个被空格分开的数,第j个数代表pij。
Output
*第一行:一个单独的数代表最小代价.
Sample Input
4
5
4
4
3
0 2 2 2
2 0 3 3
2 3 0 4
2 3 4 0
Sample Output
9
输出详解:
Farmer John在第四块土地上建立水库,然后把其他的都连向那一个,这样就要花费3+2+2+2=9
解析:
本题既有边权,又有点权,如何将它们统一起来呢?不妨设置一个超级水源点,将它与各个点连边,代价为在那个点修水库所需的代价。接下来就可以使用最小生成树算法进行计算最小代价了。
代码如下:
#include<bits/stdc++.h>
using namespace std;
int n,m1,m2,aa;
int v[310][310],lowest[310],z=0;
bool d[310];
void mst()
{
int ans=0;
memset(d,true,sizeof(d));
for(int i=1;i<=n;i++)
lowest[i]=2147483647;
lowest[1]=0;
while(true)
{
m1=2147483647;
m2=0;
for(int i=1;i<=n;i++)
if(d[i]==true&&lowest[i]<m1)
{
m2=i;
m1=lowest[i];
}
ans=ans+m1;
d[m2]=false;
z++;
if(z==n)
break;
for(int i=1;i<=n;i++)
if(d[i]==true&&v[m2][i]<lowest[i])
lowest[i]=v[m2][i];
}
cout<<ans<<endl;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)//构造一个n+1点,从它引边到每一个点,边权值为建每一个点的代价
{
scanf("%d",&v[i][n+1]);
v[n+1][i]=v[i][n+1];
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&v[i][j]);
n++;
mst();
//cout<<ans;
return 0;
}
Farmer John has decided to pour water into his n (1 & lt; = n & lt; = 300) farmland, which is marked by the numbers 1 to n. There are two ways to irrigate a piece of land, drinking water from other farmland, or building a reservoir on the land. It costs wi (1 & lt; = wi & lt; = 100000) to build a reservoir and PIJ (1 & lt; = PIJ & lt; = 100000, PIJ = PJI, PII = 0) to connect two lands Calculate the minimum cost of Farmer John.
Input
*First line: a number n
*Line 2 to line n + 1: line I + 1 contains a number wi
*Line n + 2 to line 2n + 1: line n + 1 + I has n numbers separated by spaces, and the j number represents PIJ.
Output
*Line 1: a single number represents the minimum cost
Sample Input
four
five
four
four
three
0 2 2 2
2 0 3 3
2 3 0 4
2 3 4 0
Sample Output
nine
Output details:
Farmer John builds a reservoir on the fourth land, and then connects the others to that one. In this way, it costs 3 + 2 + 2 + 2 = 9
Resolution:
This topic has both edge power and point power. How to unify them? You might as well set up a super water source point and connect it with each point at the cost of building a reservoir at that point. Next, the minimum spanning tree algorithm can be used to calculate the minimum cost.
The code is as follows:
#include<bits/stdc++.h>
using namespace std;
int n,m1,m2,aa;
int v[310][310],lowest[310],z=0;
bool d[310];
void mst()
{
int ans=0;
memset(d,true,sizeof(d));
for(int i=1;i<=n;i++)
lowest[i]=2147483647;
lowest[1]=0;
while(true)
{
m1=2147483647;
m2=0;
for(int i=1;i<=n;i++)
if(d[i]==true&&lowest[i]<m1)
{
m2=i;
m1=lowest[i];
}
ans=ans+m1;
d[m2]=false;
z++;
if(z==n)
break;
for(int i=1;i<=n;i++)
if(d[i]==true&&v[m2][i]<lowest[i])
lowest[i]=v[m2][i];
}
cout<<ans<<endl;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)//构造一个n+1点,从它引边到每一个点,边权值为建每一个点的代价
{
scanf("%d",&v[i][n+1]);
v[n+1][i]=v[i][n+1];
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&v[i][j]);
n++;
mst();
//cout<<ans;
return 0;
}