zoukankan      html  css  js  c++  java
  • POJ1502MPI Maelstromdijkstra+输入处理

    BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchical communication subsystem. Valentine McKee's research advisor, Jack Swigert, has asked her to benchmark the new system. 
    ``Since the Apollo is a distributed shared memory machine, memory access and communication times are not uniform,'' Valentine told Swigert. ``Communication is fast between processors that share the same memory subsystem, but it is slower between processors that are not on the same subsystem. Communication between the Apollo and machines in our lab is slower yet.'' 

    ``How is Apollo's port of the Message Passing Interface (MPI) working out?'' Swigert asked. 

    ``Not so well,'' Valentine replied. ``To do a broadcast of a message from one processor to all the other n-1 processors, they just do a sequence of n-1 sends. That really serializes things and kills the performance.'' 

    ``Is there anything you can do to fix that?'' 

    ``Yes,'' smiled Valentine. ``There is. Once the first processor has sent the message to another, those two can then send messages to two other hosts at the same time. Then there will be four hosts that can send, and so on.'' 

    ``Ah, so you can do the broadcast as a binary tree!'' 

    ``Not really a binary tree -- there are some particular features of our network that we should exploit. The interface cards we have allow each processor to simultaneously send messages to any number of the other processors connected to it. However, the messages don't necessarily arrive at the destinations at the same time -- there is a communication cost involved. In general, we need to take into account the communication costs for each link in our network topologies and plan accordingly to minimize the total time required to do a broadcast.''

    Input

    The input will describe the topology of a network connecting n processors. The first line of the input will be n, the number of processors, such that 1 <= n <= 100. 

    The rest of the input defines an adjacency matrix, A. The adjacency matrix is square and of size n x n. Each of its entries will be either an integer or the character x. The value of A(i,j) indicates the expense of sending a message directly from node i to node j. A value of x for A(i,j) indicates that a message cannot be sent directly from node i to node j. 

    Note that for a node to send a message to itself does not require network communication, so A(i,i) = 0 for 1 <= i <= n. Also, you may assume that the network is undirected (messages can go in either direction with equal overhead), so that A(i,j) = A(j,i). Thus only the entries on the (strictly) lower triangular portion of A will be supplied. 

    The input to your program will be the lower triangular section of A. That is, the second line of input will contain one entry, A(2,1). The next line will contain two entries, A(3,1) and A(3,2), and so on.

    Output

    Your program should output the minimum communication time required to broadcast a message from the first processor to all the other processors.

    Sample Input

    5
    50
    30 5
    100 20 50
    10 x x 10

    Sample Output

    35

    题意:
    一个信息站需要往其他信息站传输信息。当一个站接受到信息之后马上向跟它相连的信息站传输信息。已知每两个相连信息站之间传输时间。
    求:信息从第一个站传到每个站的最短时间。 
    思路:将传输代价作为路径长度,即求第一个信息站到其它所有信息站最短路径的最大值。

    题意比较难读懂,做题还用到了一个新函数atoi

    atoi用法:把字符串型转化成整型
    头文件#include<stdlib.h>
    注意:atoi()函数只能转换为整数,如果一定要写成浮点数的字符串,它会发生强制转换
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<stdlib.h>
     4 using namespace std;
     5 
     6 int main()
     7 {
     8     char ss[10];
     9     int aa=40;
    10     scanf("%s",&ss);
    11     int dd=atoi(ss);
    12     printf("**%d**\n",aa+dd);
    13     return 0;
    14 }

    测试数据输出

    
    
    题目代码如下:
      1 #include<iostream>
      2 #include<string.h>
      3 #include<cmath>
      4 #include<stdio.h>
      5 #include<algorithm>
      6 #include<stdlib.h>
      7 #include<iomanip>
      8 #define inf 0x3f3f3f3f
      9 #define ios() std::ios::sync_with_stdio(false)
     10 #define cin() cin.tie(0)
     11 #define cout() cout.tie(0)
     12 #define mem1(a) memset(a,0,sizeof(a))
     13 #define mem2(b) memset(b,'\0',sizeof(b))
     14 using namespace std;
     15 
     16 
     17 int a[110][110];
     18 int dis[110],book[110];
     19 int n;
     20 char s[110];
     21 
     22 void init()
     23 {
     24     for(int i=1; i<=n; i++)
     25     {
     26         for(int j=1; j<=n; j++)
     27         {
     28             if(i==j)
     29                 a[i][j]=0;
     30             else
     31                 a[i][j]=inf;
     32         }
     33     }
     34     return ;
     35 }
     36 
     37 int dijkstra()
     38 {
     39     mem1(book);
     40     for(int i=1; i<=n; i++)
     41         dis[i]=a[1][i];
     42     book[1]=1;
     43     int u;
     44     for(int i=1; i<=n-1; i++)
     45     {
     46         int minn=inf;
     47         for(int j=1; j<=n; j++)
     48         {
     49             if(book[j]==0&&dis[j]<minn)
     50             {
     51                 minn=dis[j];
     52                 u=j;
     53             }
     54         }
     55         book[u]=1;
     56         for(int k=1; k<=n; k++)
     57         {
     58             if(a[u][k]<inf)
     59             {
     60                 if(dis[k]>dis[u]+a[u][k])
     61                 {
     62                     dis[k]=dis[u]+a[u][k];
     63                 }
     64             }
     65         }
     66     }
     67     int maxx=-inf;
     68     for(int i=1; i<=n; i++)
     69         maxx=max(maxx,dis[i]);
     70     return maxx;
     71 }
     72 
     73 int main()
     74 {
     75 //    ios();
     76 //    cin();
     77 //    cout();
     78 
     79     cin>>n;
     80     init();
     81     //?数组a开到110即可,x的ASCII是120,不会和其他重复
     82     for(int i=2; i<=n; i++)
     83     {
     84         for(int j=1; j<i; j++)
     85         {
     86 
     87 //            cin>>a[i][j];
     88 //            if(a[i][j]=='x')
     89 //            {
     90 //                a[i][j]=inf;
     91 //            }
     92 //            a[j][i]=a[i][j];
     93             scanf("%s",s);
     94             if(s[0]=='x')
     95                 a[i][j]=a[j][i]=inf;
     96             else
     97                 a[i][j]=a[j][i]=atoi(s);//注意输入
     98         }
     99     }
    100     cout<<dijkstra()<<endl;
    101     return 0;
    102 }
     
    一般写法,不用函数:(由于数据比较小,所以可以利用五行代码三层for循环来写)
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<iostream>
     4 #include<queue>
     5 using namespace std;
     6 #define inf 0x3f3f3f3f
     7 
     8 int e[110][110];
     9 char a[20];
    10 
    11 int main()
    12 {
    13     int n;
    14     scanf("%d",&n);
    15     memset(e,0,sizeof(e));
    16     for(int i=1; i<=n; i++)
    17     {
    18         for(int j=1; j<=n; j++)
    19         {
    20             if(i==j)
    21                 e[i][j]=0;
    22             else
    23                 e[i][j]=inf;
    24         }
    25     }
    26 
    27     int sum;
    28     for(int i=2; i<=n; i++)
    29     {
    30         for(int j=1; j<=i-1; j++)
    31         {
    32             scanf("%s",a);
    33             int len=strlen(a);
    34             if(a[0]=='x')
    35                 sum=inf;
    36             else
    37             {
    38                 sum=0;
    39                 for(int k=0; k<len; k++)
    40                 {
    41                     sum=sum*10+(a[k]-'0');
    42                 }
    43             }
    44             e[i][j]=e[j][i]=sum;
    45         }
    46     }
    47 
    48     for(int k=1; k<=n; k++)
    49     {
    50         for(int i=1; i<=n; i++)
    51         {
    52             for(int j=1; j<=n; j++)
    53             {
    54                 e[i][j]=min(e[i][j],e[i][k]+e[k][j]);
    55             }
    56         }
    57     }
    58 
    59     int ans=-inf;
    60     for(int i=1;i<=n;i++)
    61         ans=max(ans,e[1][i]);
    62 
    63     printf("%d\n",ans);
    64     return 0;
    65 }
    
    
    
     
  • 相关阅读:
    PHP单引号和双引号的区别
    thinkphp实现导航高亮的简单方法
    js选中当前菜单后高亮显示的导航条
    Dubbo新版管控台
    把本人基于Dubbo的毕业设计分享粗来~
    Windows 查看端口占用情况
    GitLab 环境搭建【CentOS7】
    【非专业前端】vue+element+webpack
    【非专业前端】使用vue2.5.17+element2.4.5
    ElasticSearch6.3.2------入门
  • 原文地址:https://www.cnblogs.com/OFSHK/p/11285999.html
Copyright © 2011-2022 走看看