zoukankan      html  css  js  c++  java
  • FZU1096QS Network

    Problem Description

    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
     
     
    解题报告:此题相对于一般的最小生成树来说,有一点小小的不同,就是特别说明定点也是有权值的,当然这个问题很好解决,只需要在计算这条路线的权值时把两个端点的端点值加进去就行了,由于输入时用矩阵形式输入,所以建议用prinme算法来解此题。下面是具体代码:
     
    #include<stdio.h>
    #include<string.h>
    int visit[5010],dote[5010],map[2000][2000],sum,T,n;
    struct node {
     int front,rear,length;
    }rode[5010];
    void popsort(int n) {
     for(int i=1;i<=n;++i)
     for(int j=1;j<=n-1;++j)
     if(rode[j].length>rode[j+1].length) {
      node d=rode[j];
      rode[j]=rode[j+1];
      rode[j+1]=d;
     }
    }
    int find(int i,int x) {
     return (i==x? i:find(x,visit[x]));
    }
    int main() {
     scanf("%d",&T);
     while(T--) {
      sum=0;
      scanf("%d",&n);
      for(int i=1;i<=n;++i) {
       visit[i]=i;
       scanf("%d",&dote[i]);
      }
      memset(visit,0,sizeof(visit));
      int z=0;
      for(int i=1;i<=n;++i)
      for(int j=1;j<=n;++j)
       scanf("%d",&map[i][j]);
      visit[1]=1;
      for(int k=1;k<n;++k) {
       int min=99999999,local1,local2;
       for(int i=1;i<=n;++i)
       if(visit[i]==0) {
        for(int j=1;j<=n;++j)
        if(visit[j]==1&&((map[i][j]+dote[i]+dote[j])<min)) {
         min=map[i][j]+dote[i]+dote[j];
         local1=i;
         local2=j;
        }
       }
       sum+=min;
       visit[local1]=visit[local2]=1;
      }
      printf("%d\n",sum);
     }
     return 0;
    }
  • 相关阅读:
    c#子类序列化与父类序列化(Serializable)的区别
    eclipse中启动项目报内存溢出问题通过修改配置解决
    java 代码的良好习惯
    s:iterator 标签使用错误记录
    html5 required属性的注意事项
    赋值文件夹名称为日期的doc命令
    cmd命令中截取日期字符
    s:if 标签 字符串比较 正确用法和错误用法
    Resource interpreted as Stylesheet but transferred with MIME type text/plain
    xmind指定32位jdk解决在64位系统上不能启动的问题
  • 原文地址:https://www.cnblogs.com/xiaxiaosheng/p/3033171.html
Copyright © 2011-2022 走看看