博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
FZU 1096 QS Network
阅读量:4932 次
发布时间:2019-06-11

本文共 3905 字,大约阅读时间需要 13 分钟。

QS Network

Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on
FZU. Original ID:
64-bit integer IO format: %I64d      Java class name: Main
 

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

1310 20 300 100 200100 0 300200 300 0

Sample Output

370 解题:需要注意:每个网络适配器只能用于单一的通信中
1 #include 
2 #include
3 #include
4 #include
5 #define INF 0x3f3f3f3f 6 #define pii pair
7 using namespace std; 8 const int maxn = 1010; 9 struct arc{10 int to,w,next;11 arc(int x = 0,int y = 0,int z = -1){12 to = x;13 w = y;14 next = z;15 }16 }e[maxn*maxn];17 int head[maxn],d[maxn],adapter[maxn],tot,n;18 bool done[maxn];19 priority_queue< pii,vector< pii >,greater< pii > >q;20 void add(int u,int v,int w){21 e[tot] = arc(v,w,head[u]);22 head[u] = tot++;23 }24 int prim(){25 int ans = 0;26 for(int i = 0; i < maxn; ++i){27 d[i] = INF;28 done[i] = false;29 }30 while(!q.empty()) q.pop();31 q.push(make_pair(d[0] = 0,0));32 while(!q.empty()){33 int u = q.top().second;34 q.pop();35 if(done[u]) continue;36 done[u] = true;37 ans += d[u];38 for(int i = head[u]; ~i; i = e[i].next){39 if(d[e[i].to] > e[i].w) q.push(make_pair(d[e[i].to] = e[i].w,e[i].to));40 }41 }42 return ans;43 }44 int main(){45 int kase;46 scanf("%d",&kase);47 while(kase--){48 memset(head,-1,sizeof(head));49 int ans = tot = 0,tmp;50 scanf("%d",&n);51 for(int i = 0; i < n; ++i)52 scanf("%d",adapter+i);53 for(int i = 0; i < n; ++i){54 for(int j = 0; j < n; ++j){55 scanf("%d",&tmp);56 if(i != j) add(i,j,tmp+adapter[i]+adapter[j]);57 }58 }59 printf("%d\n",prim());60 }61 return 0;62 }
View Code

 

转载于:https://www.cnblogs.com/crackpotisback/p/4333876.html

你可能感兴趣的文章
avcodec_open2()分析
查看>>
何如获取单选框中某一个选中的值
查看>>
paip.输入法编程----删除双字词简拼
查看>>
QQ悬浮返回顶部
查看>>
MySQL建表语句的一些特殊字段
查看>>
《Unix环境高级编程》读书笔记 第8章-进程控制
查看>>
腾讯前端二面题目详解
查看>>
mascara-1
查看>>
Jquery Form表单取值
查看>>
Android API level 与version对应关系
查看>>
Team Name
查看>>
String类
查看>>
西门子_TDC_数据耦合小经验
查看>>
接口测试与postman
查看>>
LINQ To XML的一些方法
查看>>
[LeetCode] Copy List with Random Pointer
查看>>
openstack部署之nova
查看>>
JS组件系列——表格组件神器:bootstrap table
查看>>
存储过程Oracle(一)
查看>>
log4j日志归档
查看>>