1. 程式人生 > >ZOJ1586 QS Network【最小生成樹】

ZOJ1586 QS Network【最小生成樹】

QS Network

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Sunny Cup 2003 - Preliminary Round

April 20th, 12:00 - 17:00

Problem E: QS Network

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.

Output

for each data set,output the minimum cost in a line. NO extra empty lines needed.

Sample Input

1
3
10 20 30
0 100 200
100 0 300
200 300 0

Sample Output

370

Author: JIANG, JiefengSource: Zhejiang University Local Contest 2003, Preliminary

問題描述:每個人均有一個路由器,第二行給出的n個數分別表示第i個人的路由器需要花的錢。 接下來的方陣表示 i 和 j 之間建立連線需要花費的網線費用。 每兩點相連的花費為兩點的網線費用和相應兩個路由器的花費。 求讓這n個路由器相連的最小花費

解題思路:簡單最小生成樹問題,主要是建邊,使用Kruskal演算法

AC的C++程式碼:

#include<iostream>
#include<cstdio> 
#include<algorithm>

using namespace std;

const int N=1005;
int g[N][N];
int cost[N];
int pre[N];

void init(int n)
{
	for(int i=0;i<=n;i++)
	  pre[i]=i;
}

int find(int x)
{
	if(x!=pre[x])
	  pre[x]=find(pre[x]);
	return pre[x];
}

bool join(int x,int y)
{
	int fx=find(x);
	int fy=find(y);
	if(fx!=fy){
		pre[fx]=fy;
		return true;
	}
	return  false;
}

struct Edge{
	int a,b,c;
	bool operator<(const Edge &t)const
	{
		return c<t.c;
	}
}e[N*N];

int main()
{
	int n,t;
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		   scanf("%d",&cost[i]);
		init(n);
		for(int i=1;i<=n;i++)
		  for(int j=1;j<=n;j++)
		  	scanf("%d",&g[i][j]);
	
		int k=0;
		for(int i=1;i<=n;i++)
		  for(int j=i+1;j<=n;j++){
		  	e[k].a=i,e[k].b=j,e[k].c=g[i][j]+cost[i]+cost[j];
		  	k++;
		  }
		sort(e,e+k);
		int ans=0,cnt=0;
		for(int i=0;i<k;i++){
			if(join(e[i].a,e[i].b)){//不構成環 
				ans+=e[i].c;
				cnt++;
				if(cnt==n-1)
				  break;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}