• poj1502 单源最短路径


    MPI Maelstrom
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 7850   Accepted: 4818

    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.''

    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

    1.dijkstra
    优先队列法O(m*logn)
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <cstring>
    #include <string>
    #include <stack>
    #include <vector>
    #include <algorithm>
    #include <queue>
    const int inf = 0x3f3f3f;
    const int MAXN = 5e2+10;
    const int MAXNN = 5e3+10;
    
    using namespace std;
    
    struct heapnode{
        //int st;
        int dist;
        int st;
        bool operator < (const heapnode& rhs) const{
            return dist>rhs.dist;
        }
    };
    
    struct edge{
        int st;
        int to;
        int dist;
        int next;
    };
    
    edge e[2*MAXNN];
    int first[MAXN];
    int top;
    int p[2*MAXNN]; //记录上一条边
    int d[MAXN];    //s到各节点的距离
    int done[MAXN]; //是否已经被永久标记
    
    int n,m,k;
    //int st,ed;
    
    void init(){
        memset(first,-1,sizeof(first));
        top = 0;
    }
    
    void addedge(int u,int v,int dist){
        e[top].st = u;
        e[top].to = v;
        e[top].next = first[u];
        e[top].dist = dist;
        first[u] = top++;
    }
    
    void dijkstra(int s){
        heapnode a;
        priority_queue<heapnode> Q;
        for(int i=0;i<n;i++){
            d[i] = inf;
        }
        d[s] = 0;
        memset(done,0,sizeof(done));
        //Q.push((heapnode){0,s});
        a.dist = 0;
        a.st = s;
        Q.push(a);
        while(!Q.empty()){
            heapnode x = Q.top();
            Q.pop();
            int u = x.st;
            /*cout<<"***"<<endl;
            cout<<u<<endl;
            cout<<"***"<<endl;*/
            if(done[u])continue;
            done[u] = 1;
            for(int i=first[u];i!=-1;i=e[i].next){
                if(d[e[i].to]>d[u]+e[i].dist){
                    d[e[i].to] = d[u]+e[i].dist;
                    p[e[i].to] = i;
                    //Q.push((heapnode){d[e[i].to],e[i].to});
                    a.dist = d[e[i].to];
                    a.st = e[i].to;
                    Q.push(a);
                }
            }
        }
    }
    
    int main()
    {
        int a;
        char ts[10];
        while(scanf("%d",&n)!=EOF){
            init();
            for(int i=2;i<=n;i++){
                for(int j=1;j<i;j++){
                    scanf("%s",ts);
                    if(ts[0]=='x'){
                        continue;
                    }
                    a = atoi(ts);
                    addedge(i-1,j-1,a);
                    addedge(j-1,i-1,a);
                }
            }
            dijkstra(0);
            int mmax = -inf;
            for(int i=0;i<n;i++){
               mmax = max(mmax,d[i]);
            }
            cout<<mmax<<endl;
        }
        return 0;
    }
    View Code
     
    在一个谎言的国度,沉默就是英雄
  • 相关阅读:
    React.js自学第一天
    优化的34条定律
    JS 打字机效果
    css3 翻书效果
    对象操作
    表单提交验证
    封装cookie组件
    iOS中为网站添加图标到主屏幕
    教你从Win8换回Win7
    关于VB中Print函数在数组中换行的理解
  • 原文地址:https://www.cnblogs.com/EdsonLin/p/5469231.html
Copyright © 2020-2023  润新知