POJ1502(最短路入门题)

  MPI Maelstrom

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7471   Accepted: 4550

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题意:给出结点数,用邻接矩阵给出每个节点的距离,因为结点i到结点i的距离为0以及路径是双向的所以只给出邻接矩阵对角线的左下半块。下面分别用求最短路径的方法实现
/*
    dijkstra 1502    Accepted    428K    0MS    G++
*/
#include"cstdio"
#include"cstring"
#include"algorithm"
using namespace std;
const int MAXN=105;
const int INF=0x3fffffff;
int mp[MAXN][MAXN];
int V;
int dijkstra(int s)
{
    int d[MAXN];
    int vis[MAXN];
    for(int i=1;i<=V;i++)
    {
        vis[i]=0;
        d[i]=mp[s][i];
    }

    int n=V;
    while(n--)
    {
        int mincost,k;
        mincost=INF;
        for(int i=1;i<=V;i++)
        {
            if(!vis[i]&&mincost>d[i])
            {
                mincost=d[i];
                k=i;
            }
        }    

        vis[k]=1;
        for(int i=1;i<=V;i++)
        {
            if(!vis[i]&&d[i]>d[k]+mp[k][i])
            {
                d[i]=d[k]+mp[k][i];
            }
        }
    }

    int ans=-1;
    for(int i=1;i<=V;i++)    ans=max(ans,d[i]);
    return ans;
}
int main()
{
    while(scanf("%d",&V)!=EOF)
    {
        for(int i=1;i<=V;i++)
            for(int j=1;j<=i;j++)
                if(i==j)    mp[i][j]=0;
                else{
                    char x[10];
                    scanf("%s",x);
                    if(x[0]==‘x‘)    mp[i][j]=mp[j][i]=INF;
                    else mp[i][j]=mp[j][i]=atoi(x);
                }    

        printf("%d\n",dijkstra(1));
    }
    return 0;
}
/*
    堆优化dijkstra 1502    1502    Accepted    632K    0MS    G++
*/
#include"cstdio"
#include"cstring"
#include"algorithm"
#include"vector"
#include"queue"
using namespace std;
const int MAXN=105;
const int INF=0X3fffffff;
struct Edge{
    int to,cost;
    Edge(){}
    Edge(int to,int cost)
    {
        this->to=to;
        this->cost=cost;
    }
    friend bool operator<(const Edge &a,const Edge &b)
    {
        return a.cost < b.cost;
    }
};
vector<Edge> G[MAXN];
int V;
int dijkstra(int s)
{
    int d[MAXN];
    for(int i=1;i<=MAXN;i++)    d[i]=INF;
    d[s]=0;

    priority_queue<Edge> que;
    que.push(Edge(s,0));
    while(!que.empty())
    {
        Edge e=que.top();que.pop();
        int v=e.to;
        if(d[v]<e.cost)    continue;
        for(int i=0;i<G[v].size();i++)
        {
            Edge ek=G[v][i];
            if(d[ek.to]>d[v]+ek.cost)
            {
                d[ek.to]=d[v]+ek.cost;
                que.push(Edge(ek.to,d[ek.to]));
            }
        }
    }
    int ans=-1;
    for(int i=1;i<=V;i++)
        if(d[i]<INF)
            ans=max(ans,d[i]);
    return ans;
}
int main()
{
    while(scanf("%d",&V)!=EOF)
    {
        for(int i=1;i<=V;i++)
            G[i].clear();
        for(int i=1;i<=V;i++)
            for(int j=1;j<=i;j++)
                if(i==j){
                    G[i].push_back(Edge(j,0));
                    G[j].push_back(Edge(i,0));
                }
                else{
                    char x[10];
                    scanf("%s",x);
                    if(x[0]==‘x‘)    ;
                    else{
                        G[j].push_back(Edge(i,atoi(x)));
                        G[i].push_back(Edge(j,atoi(x)));
                    }
                }
        printf("%d\n",dijkstra(1));
    }
    return 0;
}
/*
    ford 1502    Accepted    444K    0MS    G++
*/
#include"cstdio"
#include"cstring"
#include"algorithm"
using namespace std;
const int MAXN=10005;
const int INF=0X3fffffff;
struct Edge{
    int from,to,cost;
}es[MAXN];
int V,E;
int ford(int s)
{
    int d[MAXN];
    for(int i=1;i<=V;i++)    d[i]=INF;
    d[s]=0;

    while(true)
    {
        bool update=false;
        for(int i=0;i<E;i++)
        {
            Edge e=es[i];
            if(d[e.from]!=INF&&d[e.to]>d[e.from]+e.cost)
            {
                d[e.to]=d[e.from]+e.cost;
                update=true;
            }
        }
        if(!update)    break;
    }
    int ans=-1;
    for(int i=1;i<=V;i++)
    {
        if(d[i]<INF)    ans=max(ans,d[i]);
    }
    return ans;
}
int main()
{
    while(scanf("%d",&V)!=EOF)
    {
        E=0;
        for(int i=1;i<=V;i++)
            for(int j=1;j<i;j++)
                {
                    char x[10];
                    scanf("%s",x);
                    if(x[0]!=‘x‘)
                    {
                        es[E].from=i,es[E].to=j,es[E++].cost=atoi(x);
                        es[E].from=j,es[E].to=i,es[E++].cost=atoi(x);
                    }
                }

        printf("%d\n",ford(1));
    }

    return 0;
}
/*
    spfa 1502    Accepted    660K    0MS    G++
*/
#include"cstdio"
#include"queue"
#include"algorithm"
#include"vector"
using namespace std;
const int MAXN=105;
const int INF=0X3fffffff;
vector<int> G[MAXN];
int mp[MAXN][MAXN];
int V;
int spfa(int s)
{
    int d[MAXN];
    int vis[MAXN];
    for(int i=1;i<=V;i++)
    {
        vis[i]=0;
        d[i]=INF;
    }

    queue<int> que;
    d[s]=0,que.push(s),vis[s]=1;

    while(!que.empty())
    {
        int v=que.front();que.pop();
        vis[v]=0;

        for(int i=0;i<G[v].size();i++)
        {
            int to=G[v][i];
            if(d[to]>d[v]+mp[v][to])
            {
                d[to]=d[v]+mp[v][to];
                que.push(to);
                vis[to]=0;
            }
        }
    }
    int ans=-1;
    for(int i=1;i<=V;i++)
        if(d[i]<INF)
            ans=max(ans,d[i]);

    return ans;
}
int main()
{
    while(scanf("%d",&V)!=EOF)
    {
        for(int i=1;i<=V;i++)
            for(int j=1;j<i;j++)
            {
                char x[10];
                scanf("%s",x);
                if(x[0]!=‘x‘)
                {
                    G[i].push_back(j),G[j].push_back(i);
                    mp[i][j]=mp[j][i]=atoi(x);
                }
            }

        printf("%d\n",spfa(1));
    }

    return 0;
}
/*
    floyd 1502    Accepted    428K    0MS    G++
*/
#include"cstdio"
#include"algorithm"
using namespace std;
const int MAXN=105;
const int INF=0X3fffffff;
int mp[MAXN][MAXN];
int V;
int main()
{
    while(scanf("%d",&V)!=EOF)
    {
        for(int i=1;i<=V;i++)
            for(int j=1;j<=i;j++)
            {
                if(i==j){
                    mp[i][j]=0;
                    continue;
                }
                char x[10];
                scanf("%s",x);
                if(x[0]!=‘x‘)    mp[i][j]=mp[j][i]=atoi(x);
                else    mp[i][j]=mp[j][i]=INF;
            }

        for(int k=1;k<=V;k++)
            for(int i=1;i<=V;i++)
                for(int j=1;j<=V;j++)
                    mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);
        int ans=-1;
        for(int i=1;i<=V;i++)
            if(mp[1][i]<INF)
                ans=max(mp[1][i],ans);

        printf("%d\n",ans);
    }

    return 0;
}
时间: 2024-10-14 10:39:10

POJ1502(最短路入门题)的相关文章

最短路入门题

http://acm.hdu.edu.cn/showproblem.php?pid=2544 DJ #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #define N 1000001 using namespace std; int map[101][101]; int n,m; int v[101],

hdu 2767 Proving Equivalences(强连通入门题)

1 /************************************************* 2 Proving Equivalences(hdu 2767) 3 强连通入门题 4 给个有向图,求至少加多少条边使得图是所有点都是强连通的 5 由a->b->c->a易知n个点至少要n条边,每个出度和入度都要大 6 于1.先求所有所有强连通分量,把每个强连通分量看成一个点 7 在找每个点的出度和入度,最后还差的出度和入度的最大值就是 8 答案. 9 10 ************

hdu 5001 walk 概率dp入门题

Description I used to think I could be anything, but now I know that I couldn't do anything. So I started traveling. The nation looks like a connected bidirectional graph, and I am randomly walking on it. It means when I am at node i, I will travel t

hdu1796:容斥入门题

简单的容斥入门题.. 容斥基本的公式早就知道了,但是一直不会写. 下午看到艾神在群里说的“会枚举二进制数就会容斥”,后来发现还真是这样.. 然后直接贴代码了 #include <iostream> #include <stdio.h> #include<string.h> #include<algorithm> #include<string> #include<ctype.h> using namespace std; long l

HDU 5521.Meeting 最短路模板题

Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 3361    Accepted Submission(s): 1073 Problem Description Bessie and her friend Elsie decide to have a meeting. However, after Farmer Jo

hdu1695 GCD(莫比乌斯入门题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意: 给出n.m.k ,求出1<=x<=n, 1<=y<=m 且gcd(x,y) == k 的(x,y)的对数 解析: 显然就是求 [1,n/k] 与 [1, m/k]有多少数对的最大公约数是1 莫比乌斯入门题 我们设 为满足且和的的对数 为满足且和的的对数 那么,很显然,反演后得到 我们所需要的答案便是  f(1) = ∑i=1μ(i)*(n/i)*(m/i)  ,求解这个式

网络流最经典的入门题 各种网络算法都能AC。

Drainage Ditches 题目抽象:给你m条边u,v,c.   n个定点,源点1,汇点n.求最大流.  最好的入门题,各种算法都可以拿来练习 (1):  一般增广路算法  ford() 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <string&g

poj1511/zoj2008 Invitation Cards(最短路模板题)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Invitation Cards Time Limit: 5 Seconds      Memory Limit: 65536 KB In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fa

hdu 1754:I Hate It(线段树,入门题,RMQ问题)

I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 33726    Accepted Submission(s): 13266 Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.这让很多学生很反感.不管你喜不喜欢,现在需要你做的是,就是按照老师的要求