题目:
In the planet w-503 of galaxy cgb, there is a kind of intelligent creature named QS. QScommunicate with each other via networks. If two QS want to get connected, they need to buy two network adapters (one for each QS) and a segment of network cable. Please be advised that ONE NETWORK ADAPTER CAN ONLY BE USED IN A SINGLE CONNECTION.(ie. if a QS want to setup four connections, it needs to buy four adapters). In the procedure of communication, a QS broadcasts its message to all the QS it is connected with, the group of QS who receive the message broadcast the message to all the QS they connected with, the procedure repeats until all the QS's have received the message.
A sample is shown below:
A sample QS network, and QS A want to send a message.
Step 1. QS A sends message to QS B and QS C;
Step 2. QS B sends message to QS A ; QS C sends message to QS A and QS D;
Step 3. the procedure terminates because all the QS received the message.
Each QS has its favorate brand of network adapters and always buys the brand in all of its connections. Also the distance between QS vary. Given the price of each QS's favorate brand of network adapters and the price of cable between each pair of QS, your task is to write a program to determine the minimum cost to setup a QS network.
Input
The 1st line of the input contains an integer t which indicates the number of data sets.
From the second line there are t data sets.
In a single data set,the 1st line contains an interger n which indicates the number of QS.
The 2nd line contains n integers, indicating the price of each QS's favorate network adapter.
In the 3rd line to the n+2th line contain a matrix indicating the price of cable between ecah pair of QS.
Constrains:
all the integers in the input are non-negative and not more than 1000.
<b< dd="">
Output
for each data set,output the minimum cost in a line. NO extra empty lines needed.
<b< dd="">
Sample Input
1
3
10 20 30
0 100 200
100 0 300
200 300 0
<b< dd="">
Sample Output
370
题意描述:
题目描述的很有意思,不过有点绕。简单来说就是每两个人要建立联系,需要分别购买一个自己最喜欢的接收器,和共同购买一个共用转换器。
先输入结点的个数N,再输入N个数,表示结点最喜欢的接收器的价格,最后输入N*N的矩阵表示i和j建立联系的转换器价格。
解题思路:
该开始的时候想用Prim算法,在选择一条边可以建造的时候,将两个结点都计一下数,最后乘以每个结点喜欢的接收器的价格再加上最小生成树的值。但是这种解法是有漏洞的,因为如果接收器是后来加的,它的费用是没有考虑进最省路径的,所以有可能,接收器的价格很贵,虽然转换器是最省的但加上接收器的话就不是最省路径了。综上所述,选择Kruskal算法,不同的是将接收器的价格事先计算在这条边上,那么就跟模板题一样了。
代码实现:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct edge
{
int u,v,w;
};
int cmp(struct edge x,struct edge y)
{
return x.w<y.w;
}
struct edge e[*];
int merge(int v,int u);
int getf(int v);
int map[][],f[];
int main()
{
int t,n,a[],i,j,k,c,sum;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=;i<=n;i++)
scanf("%d",&a[i]);
for(i=;i<=n;i++)
for(j=;j<=n;j++)
scanf("%d",&map[i][j]); k=;
for(i=;i<=n;i++)
{
for(j=i+;j<=n;j++)
{
e[k].u=i;
e[k].v=j;
e[k++].w=map[i][j]+a[i]+a[j];
}
}
sort(e+,e+k,cmp);
for(i=;i<=n;i++)
f[i]=i;
sum=;
c=;
for(i=;i<k;i++)//边的数目
{
if( merge(e[i].u,e[i].v) )
{
c++;
sum += e[i].w;
}
if( c == n-)
break;
}
printf("%d\n",sum);
}
return ;
} int merge(int v,int u)
{
int t1,t2;
t1=getf(v);
t2=getf(u);
if(t1 != t2)
{
f[t2]=t1;
return ;
}
return ;
}
int getf(int v)
{
if(f[v]==v)
return v;
else
{
f[v]=getf(f[v]);
return f[v];
}
}
易错分析:
1、注意遍历的时候遍历的是边的条数(代码功力还是要加强的)