[kuangbin]专题四 最短路练习 MPI Maelstrom POJ - 1502【dijkstra】

【题目描述】
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.
BIT最近接收了他们的新超级计算机,一台32处理器的Apollo Odyssey分布式共享内存机,带有分层通信子系统。 Valentine McKee的研究顾问Jack Swigert要求她对新系统进行基准测试。
``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.’’
“由于Apollo是一个分布式共享内存机器,因此内存访问和通信时间并不统一,”Valentine告诉Swigert。“共享相同内存子系统的处理器之间的通信速度很快,但是不在同一子系统上的处理器之间的通信速度较慢。Apollo与我们实验室机器之间的沟通速度较慢。”
``How is Apollo’s port of the Message Passing Interface (MPI) working out?’’ Swigert asked.
“Apollo的消息传递接口(MPI)端口如何运作?”Swigert问道。
``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.’’
“不太好,”Valentine回答道。 ``要从一个处理器向所有其他n-1个处理器广播消息,它们只执行一系列n-1个发送。这真的将事物序列化并杀死了表现。’’
``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.’’
“是的,”Valentine微笑着说。 "有。一旦第一个处理器将消息发送给另一个处理器,这两个处理器就可以同时向另外两个主机发送消息。然后会有四个主机可以发送,依此类推。
``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.’’
`不是真正的二叉树 - 我们应该利用我们网络的一些特定功能。我们的接口卡允许每个处理器同时向连接到它的任何数量的其他处理器发送消息。但是,消息不一定同时到达目的地 - 涉及通信费用。一般而言,我们需要考虑网络拓扑中每个链路的通信成本,并相应地进行规划,以最大限度地缩短进行广播所需的总时间。

【输入】
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.
输入将描述连接n个处理器的网络的拓扑。输入的第一行将是n,即处理器的数量,使得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.
输入的其余部分定义邻接矩阵A.邻接矩阵是正方形且大小为n×n。它的每个条目都是整数或字符x。 A(i,j)的值表示直接从节点i向节点j发送消息的费用。 A(i,j)的x值表示不能直接从节点i向节点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.
请注意,对于节点向自身发送消息不需要网络通信,因此对于1 <= i <= n,A(i,i)= 0。此外,您可以假设网络是无向的(消息可以以相同的开销进入任一方向),因此A(i,j)= A(j,i)。因此,仅提供A的(严格地)下三角形部分上的条目。
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.
程序的输入将是A的下三角形部分。也就是说,第二行输入将包含一个条目A(2,1)。下一行将包含两个条目,A(3,1)和A(3,2),依此类推。

【输出】
Your program should output the minimum communication time required to broadcast a message from the first processor to all the other processors.
您的程序应输出从第一个处理器向所有其他处理器广播消息所需的最短通信时间。

【样例输入】
5
50
30 5
100 20 50
10 x x 10

【样例输出】
35

题目链接:https://cn.vjudge.net/problem/POJ-1502

dijkstra模板题,唯一难点是阅读理解。。。。。

代码如下:

#include <iostream>
#include <queue>
#include <vector>
#include <string>
#include <cstdlib>
using namespace std;
#define INF 0x3f3f3f3f
static const int MAXN=100;
struct Node{
    int v,c;
    Node(int _v=0,int _c=0):v(_v),c(_c){}
    bool operator < (const Node &r) const {
        return c>r.c;
    }
};
struct Edge{
    int v,cost;
    Edge(int _v=0,int _cost=0):v(_v),cost(_cost){}
};
vector<Edge> E[MAXN+10];
bool vis[MAXN+10];
int dist[MAXN+10];
void dijkstra(int n,int start)
{
    for(int i=1;i<=n;i++)
    {
        vis[i]=false;
        dist[i]=INF;
    }
    priority_queue<Node> Q;
    while(!Q.empty())
        Q.pop();
    dist[start]=0;
    Q.push(Node(start,0));
    while(!Q.empty())
    {
        Node tmp=Q.top();
        Q.pop();
        int u=tmp.v;
        if(vis[u])
            continue;
        vis[u]=true;
        for(int i=0;i<E[u].size();i++)
        {
            int v=E[u][i].v;
            int cost=E[u][i].cost;
            if(!vis[v] && dist[v]>dist[u]+cost)
            {
                dist[v]=dist[u]+cost;
                Q.push(Node(v,dist[v]));
            }
        }
    }
}
void addedge(int u,int v,int w)
{
    E[u].push_back(Edge(v,w));
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0),cout.tie(0);
    int n;
    cin>>n;
    string s;
    for(int i=2;i<=n;i++)
        for(int j=1;j<i;j++)
        {
            cin>>s;
            if(s!="x")
            {
                int x=atoi(s.c_str());
                addedge(i,j,x);
                addedge(j,i,x);
            }
        }
    dijkstra(n,1);
    int ans=dist[1];
    for(int i=2;i<=n;i++)
        ans=max(ans,dist[i]);
    cout<<ans<<endl;
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值