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
/*
Name: zoj1586
Date: 2015-08-12
Description: 最小生成树问题,变化的一点是,求边的时候要做个加法,具体看代码。
*/
#include<cstdio>
#include<string.h>
#include<math.h>
#define INF 0x3f3f3f3f
using namespace std;
const int mx=1010;
int edge[mx][mx];//网线的价格加双方适配器价格
int a[mx];//适配器的价格
int n;
void init()
{
int i,j,k;
scanf("%d",&n);
for(i = 0; i < n; i++ )
scanf("%d", &a[i]);//输入每个地方需要适配器的价格
for(i = 0; i < n; i++ )
{
for(j =0; j < n; j++)
{
scanf("%d", &edge[i][j]);//输入网线价格
if(i == j)
edge[i][j] += INF;//自身设为无穷大
else
edge[i][j] += a[i]+a[j];//网线价格加两地适配器价格
}
}
}
void prim()
{
int i,j,k,min;
int pay[mx];
int sum = 0;
pay[0]= -1; // 0已经在集合内了记做 -1.
for(i = 1; i < n;i++)
pay[i] = edge[0][i];
for(j = 1; j < n; j++)//老套路 循环n-1次
{
min=INF;
for(i = 0;i < n; i++)
if(pay[i]!=-1 && pay[i]<min)//如果 i 不在集合内 且距离小
{
min = pay[i];
k = i; //下标 标记
}
sum += min; //总价格
pay[k] = -1;//标记 -1
for(i = 0; i< n; i++)//更新其他点到集合的距离
{
if(pay[i]!=-1 && pay[i] > edge[k][i])
pay[i] = edge[i][k];
}
}
printf("%d\n",sum);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
init();
prim();
}
return 0;
}
#include<cstdio>
#include<cstring>
#include<cmath>
#define mem(a, b) memset(a, (b), sizeof(a))
#define Wi(a) while(a--)
#define Si(a) scanf("%d", &a)
#define Pi(a) printf("%d\n", (a))
#define INF 0x3f3f3f
#include<algorithm>
using namespace std;
int n;
int per[1500];
int a[1500];
void init(){
for(int i = 0; i <= n; i++)
per[i] = i;
}
struct node{
int from, to, val;
};
node p[1001000];//题上没说范围的话适当大一点 免得RE.
bool operator < (node a, node b)
{
return a.val < b.val;
}
int find(int x)
{
return x==per[x] ? x : (per[x] = find(per[x]));
}
bool join(int x, int y)
{
int fx = find(x);
int fy = find(y);
if(fx != fy)
{
per[fx] = fy;
return true;
}
return false;
}
int main(){
int t; Si(t);
Wi(t){
Si(n);init();
int i, j, k;
for(i = 0; i < n; i++)
{
Si(a[i]);
}
int v;
for(i = 0, k = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
Si(v);
p[k].from = i;
p[k].to = j;
p[k].val = v+a[i]+a[j];
k++;
}
}
sort(p, p+k);
int ans = 0;
for(i = 0; i < k; i++)
{
if( join(p[i].from, p[i].to))
ans += p[i].val;
}
Pi(ans);
}
return 0;
}
#include<cstdio>
#include<cstring>
#include<cmath>
#define mem(a, b) memset(a, (b), sizeof(a))
#define Wi(a) while(a--)
#define Si(a) scanf("%d", &a)
#define Pi(a) printf("%d\n", (a))
#define INF 0x3f3f3f
int map[1010][1010];
int n;
int val[1010], a[1010];
void prim()
{
int i,j,k;
int ans = 0, minn;
val[0] = -1;
for(i = 1; i < n; i++)
{
val[i] = map[0][i];
}
for(i = 1; i < n; i++)
{
k = 1; minn = INF;
for(j = 0; j < n; j++)
{
if(val[j] != -1 && val[j] < minn)
{
minn = val[j];
k = j;
}
}
val[k] = -1;
ans += minn;
for(j = 0; j < n; j++)
{
if(val[j] != -1 && val[j] > map[j][k])
val[j] = map[j][k];
}
}
Pi(ans);
}
int main(){
int t;Si(t);
Wi(t){
Si(n);
int i, j ,k;
for(i = 0; i < n; i++)
Si(a[i]);
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
Si(map[i][j]);
if(i == j) map[i][j] += INF;
else map[i][j] += a[i]+a[j];
}
}
prim();
}
return 0;
}