zoukankan      html  css  js  c++  java
  • QS Network (最小生成树——prim方法)

    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
    题意:大致就是一群人之间要建网,给出每两个人之间连网线的价钱和每个人所用的路由器(就当是路由器了啦)的价钱,让你找一个最便宜且能把所有人连起来的方法。

    这题的关键是每与别人连一次就要一台路由器,例如A和B,C相连那么A就要买两个。

    其他到没啥难点。

    上代码:

    #include<stdio.h>
    #include<cstring>
    #include<iostream>
    #include<queue>
    #include<cstring>
    #include<vector>
    #include<set>
    #include<algorithm>
    
    #define INF 0x3f3f3f3f
    
    using namespace std;
    
    int board[1005][1005];
    int dian[1005];
    int N;
    bool book[1005];
    
    int money;
    
    void Prim()
    {
    	int min,x;
    	for(int k=1 ; k<N ; k++)
    	{
    		min = INF;
    		for(int i=1 ; i<N ; i++)
    		{
    			if(book[i])continue;
    			for(int j=0 ; j<N ; j++)
    			{
    				if(j == i)continue;
    				if(book[j])
    				{
    					if(board[i][j]+dian[i]+dian[j]<min)
    					{
    						min = board[i][j]+dian[i]+dian[j];
    						x = i;
    					}
    				}
    			}
    		}
    		book[x] = true;
    		money += min;
    	}
    }
    
    int main()
    {
    	int T;
    	scanf("%d",&T);
    	while(T--)
    	{
    		scanf("%d",&N);
    		money = 0;
    		for(int i=0 ; i<N ; i++)
    		{
    			scanf("%d",&dian[i]);
    		}
    		for(int i=0 ; i<N ; i++)
    		{
    			for(int j=0 ; j<N ; j++)
    			{
    				scanf("%d",&board[i][j]);
    			}
    		}
    		memset(book,false,sizeof(book));
    		book[0] = true;
    		Prim();
    		printf("%d
    ",money);
    	}
    	return 0;
    }


  • 相关阅读:
    OJ练习31——T155 Min Stack
    OJ练习30——T125 Valid Palindrome
    OJ练习29——T119 Pascal's Triangle II
    最大公约数——414AMashmokh and Numbers
    数学——415B. Mashmokh and Tokens
    水题——415A. Mashmokh and Lights
    湖南大学2015年“新生杯”程序设计竞赛
    react的ES6写法
    【Ionic】ionic安装和项目的创建 及相关介绍
    Flex 弹性布局教程很不错
  • 原文地址:https://www.cnblogs.com/vocaloid01/p/9514310.html
Copyright © 2011-2022 走看看