• (简单) POJ 1502 MPI Maelstrom,Dijkstra。


      Description

      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.''
     
      就是求最短路里面最大的那个。。。
     
    代码如下:
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    
    using namespace std;
    
    const int MaxN=110;
    const int INF=10e8;
    
    bool vis[MaxN];
    
    void Dijkstra(int cost[][MaxN],int lowcost[],int n,int start)
    {
        for(int i=1;i<=n;++i)
        {
            vis[i]=0;
            lowcost[i]=INF;
        }
        lowcost[start]=0;
    
        for(int j=1;j<=n;++j)
        {
            int k=-1;
            int minn=INF;
    
            for(int i=1;i<=n;++i)
                if(!vis[i] && lowcost[i]<minn)
                {
                    minn=lowcost[i];
                    k=i;
                }
    
            if(k==-1)
                break;
    
            vis[k]=1;
    
            for(int i=1;i<=n;++i)
                if(!vis[i])
                    lowcost[i]=min(lowcost[k]+cost[k][i],lowcost[i]);
        }
    }
    
    int ans[MaxN];
    int map1[MaxN][MaxN];
    
    int main()
    {
        int n;
        int maxn;
        char c[20];
    
        while(~scanf("%d",&n))
        {
            for(int i=1;i<=n;++i)
                for(int j=1;j<=i;++j)
                    if(i==j)
                        map1[i][j]=0;
                    else
                    {
                        scanf("%s",c);
    
                        if(c[0]!='x')
                            sscanf(c,"%d",&map1[i][j]);
                        else
                            map1[i][j]=INF;
    
                        map1[j][i]=map1[i][j];
                    }
    
            Dijkstra(map1,ans,n,1);
    
            maxn=-1;
    
            for(int i=1;i<=n;++i)
                if(ans[i]>maxn)
                    maxn=ans[i];
    
            cout<<maxn<<endl;
        }
    
        return 0;
    }
    View Code
  • 相关阅读:
    two pointers思想 ---- 利用两个i, j两个下标,同时对序列进行扫描,以O(n)复杂度解决问题的一种思想
    二分法
    区间贪心
    error C2825: '_Iter': 当后面跟“::”时必须为类或命名空间 -- 原因可能是参数错误或者自定义函数名和库函数名冲突
    模态窗口的定时关闭
    数据结构(二十二)二叉树遍历算法的应用与二叉树的建立
    数据结构(二十一)二叉树的非递归遍历算法
    数据结构(二十)二叉树的递归遍历算法
    数据结构(十九)二叉树的定义和性质
    数据结构(十八)树的定义与存储结构
  • 原文地址:https://www.cnblogs.com/whywhy/p/4338553.html
Copyright © 2020-2023  润新知