POJ

来源:互联网 发布:dw for mac中文破解版 编辑:程序博客网 时间:2024/04/30 03:47

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 Input55030 5100 20 5010 x x 10Sample Output35

题目大意:有一个信息传递系统,含有n个处理器,传递信息的方式是:刚开始编号为1的处理器拥有信息,他可以传给下一个处理器,然后这两个拥有信息的处理器可以同时向下传递给其他两个处理器,拥有信息的四个处理器再依次如此传递,直到所有处理器都接受到信息,则最短时间为。
思路:套用Dijkstra即可,注意要将字符转化为数字

#include<algorithm>#include<iostream>#include<stdlib.h>#include<cstring>#include<cstdio>#include<cstdio>#include<cmath>#include<queue>using namespace std;const int MAX = 105;const int INF = 0x3f3f3f3f;struct A{    int arcs[MAX][MAX];//权值-->路径长度     int arcnum;        //边的数目     int vexnum;        //顶点的数目}G;bool visit[MAX];   //顶点的访问情况int dis[MAX];      //出发点到其余点的最短距离 int v;//单源最短路Dijkstra算法 //求start到其他点的最短路长度 int Dijkstra(int start){    for(int i=1 ; i<=G.vexnum ; i++){        dis[i] = G.arcs[start][i];        visit[i] = false;    }    visit[start] = true;    //再依次添加n-1个点,更新最短路     for(int i=1 ; i<=G.vexnum-1 ; i++){        int m = INF;        for(int j=1 ; j<=G.vexnum ; j++){            //找到距离最小的             if(!visit[j] && dis[j]<m){                m = dis[j];                v = j;            }        }        //找到最短距离的点         visit[v] = true;        for(int j=1 ; j<=G.vexnum ; j++){            //更新起始点到未访问点的最短距离             dis[j] = min(dis[j],dis[v]+G.arcs[v][j]);        }     }    int ans=0;    for(int i=1 ; i<=G.vexnum ; i++){        ans = max(ans,dis[i]);    }     return ans; }int main(void){    cin>>G.vexnum;     //初始化图     for(int i=1 ; i<=G.vexnum ; i++){        for(int j=1 ; j<=G.vexnum ; j++){            G.arcs[i][j] = (i==j)?0:INF;          }    }    //输入权值(路径长度)     for(int i=2 ; i<=G.vexnum ; i++){          for(int j=1 ; j<i ; j++){            char str[10];            cin>>str;            //将输入的字符串转化为整数             if(str[0] != 'x'){                G.arcs[i][j]=G.arcs[j][i]=atoi(str);            }        }                   }    cout<<Dijkstra(1)<<endl;    return 0;}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 驾驶证违章罚单丢了怎么办 身份证被别人办了信用卡怎么办 被别人办了信用卡怎么办 考驾照体检忘带身份证怎么办 c证扣12分怎么办新规 c照12分不够扣怎么办 扣了18分怎么办一次性 c照累计扣12分怎么办 车辆超速扣12分怎么办 一次超速扣12分怎么办 分扣了罚款未交怎么办 c照一次扣12分怎么办 人在外地身份证到期了怎么办 手机进水了屏幕不亮怎么办 北京一证通过期怎么办 小米6音量键进水怎么办 考驾照怕过不了怎么办 学车对车没感觉怎么办 居住证到期2个月怎么办 生育险差一个月怎么办 驾照扣了38分怎么办 新疆转入山东上学怎么办手续 驾照过日期换证怎么办 机动车被扣24分怎么办 车辆被扣24分怎么办 现在深圳牌十年老车怎么办? 护士证过期4年了怎么办 护士资格证延续注册过期了怎么办 护士资格证过期没注册怎么办 护士资格证注册时间过期怎么办 辅警体检视力不行怎么办 护士延续注册体检怀孕怎么办 护士资格证没有延续注册怎么办 申请信用卡没有座机号码怎么办 网上申请信用卡没有座机号码怎么办 我叫上门服务被骗了怎么办 上门服务被骗了3000多怎么办 微信被骗9000元怎么办 奥迪a8气囊灯亮怎么办 驾考站岗迟到了怎么办 老板欠员工工资不给怎么办