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=;
const int INF=0x3fffffff;
int mp[MAXN][MAXN];
int V;
int dijkstra(int s)
{
int d[MAXN];
int vis[MAXN];
for(int i=;i<=V;i++)
{
vis[i]=;
d[i]=mp[s][i];
} int n=V;
while(n--)
{
int mincost,k;
mincost=INF;
for(int i=;i<=V;i++)
{
if(!vis[i]&&mincost>d[i])
{
mincost=d[i];
k=i;
}
} vis[k]=;
for(int i=;i<=V;i++)
{
if(!vis[i]&&d[i]>d[k]+mp[k][i])
{
d[i]=d[k]+mp[k][i];
}
}
} int ans=-;
for(int i=;i<=V;i++) ans=max(ans,d[i]);
return ans;
}
int main()
{
while(scanf("%d",&V)!=EOF)
{
for(int i=;i<=V;i++)
for(int j=;j<=i;j++)
if(i==j) mp[i][j]=;
else{
char x[];
scanf("%s",x);
if(x[]=='x') mp[i][j]=mp[j][i]=INF;
else mp[i][j]=mp[j][i]=atoi(x);
} printf("%d\n",dijkstra());
}
return ;
}
/*
堆优化dijkstra 1502 1502 Accepted 632K 0MS G++
*/
#include"cstdio"
#include"cstring"
#include"algorithm"
#include"vector"
#include"queue"
using namespace std;
const int MAXN=;
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=;i<=MAXN;i++) d[i]=INF;
d[s]=; priority_queue<Edge> que;
que.push(Edge(s,));
while(!que.empty())
{
Edge e=que.top();que.pop();
int v=e.to;
if(d[v]<e.cost) continue;
for(int i=;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=-;
for(int i=;i<=V;i++)
if(d[i]<INF)
ans=max(ans,d[i]);
return ans;
}
int main()
{
while(scanf("%d",&V)!=EOF)
{
for(int i=;i<=V;i++)
G[i].clear();
for(int i=;i<=V;i++)
for(int j=;j<=i;j++)
if(i==j){
G[i].push_back(Edge(j,));
G[j].push_back(Edge(i,));
}
else{
char x[];
scanf("%s",x);
if(x[]=='x') ;
else{
G[j].push_back(Edge(i,atoi(x)));
G[i].push_back(Edge(j,atoi(x)));
}
}
printf("%d\n",dijkstra());
}
return ;
}
/*
ford 1502 Accepted 444K 0MS G++
*/
#include"cstdio"
#include"cstring"
#include"algorithm"
using namespace std;
const int MAXN=;
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=;i<=V;i++) d[i]=INF;
d[s]=; while(true)
{
bool update=false;
for(int i=;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=-;
for(int i=;i<=V;i++)
{
if(d[i]<INF) ans=max(ans,d[i]);
}
return ans;
}
int main()
{
while(scanf("%d",&V)!=EOF)
{
E=;
for(int i=;i<=V;i++)
for(int j=;j<i;j++)
{
char x[];
scanf("%s",x);
if(x[]!='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());
} return ;
}
/*
poj1502 spfa Accepted 232K 16MS C++
*/
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXN=;
const int INF=0x3fffffff;
struct Edge{
int to,w;
Edge(){}
Edge(int cto,int cw):to(cto),w(cw){}
};
vector<Edge> mp[MAXN];
int n;
int d[MAXN],vis[MAXN];
void spfa(int s)
{
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)
d[i]=INF;
queue<int> que;
d[s]=,vis[s]=;
que.push(s);
while(!que.empty())
{
int now=que.front();que.pop();
vis[now]=;
for(int i=;i<mp[now].size();i++)
{
Edge e=mp[now][i];
if(d[e.to]>d[now]+e.w)
{
d[e.to]=d[now]+e.w;
if(!vis[e.to])
{
vis[e.to]=;
que.push(e.to);
}
}
}
}
int res=;
for(int i=;i<=n;i++)
res=max(d[i],res);
printf("%d\n",res);
}
int main()
{
while(scanf("%d",&n)!=EOF)
{ for(int i=;i<=n;i++)
mp[i].clear();
for(int i=;i<=n;i++)
for(int j=;j<i;j++)
{
char x[]="\0";
scanf("%s",x);
if(x[]=='x') continue;
else
{
int l=atoi(x);
mp[i].push_back(Edge(j,l));
mp[j].push_back(Edge(i,l));
}
}
spfa();
}
}
/*
floyd 1502 Accepted 428K 0MS G++
*/
#include"cstdio"
#include"algorithm"
using namespace std;
const int MAXN=;
const int INF=0X3fffffff;
int mp[MAXN][MAXN];
int V;
int main()
{
while(scanf("%d",&V)!=EOF)
{
for(int i=;i<=V;i++)
for(int j=;j<=i;j++)
{
if(i==j){
mp[i][j]=;
continue;
}
char x[];
scanf("%s",x);
if(x[]!='x') mp[i][j]=mp[j][i]=atoi(x);
else mp[i][j]=mp[j][i]=INF;
} for(int k=;k<=V;k++)
for(int i=;i<=V;i++)
for(int j=;j<=V;j++)
mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);
int ans=-;
for(int i=;i<=V;i++)
if(mp[][i]<INF)
ans=max(mp[][i],ans); printf("%d\n",ans);
} return ;
}

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

  1. hdu 3695:Computer Virus on Planet Pandora(AC自动机,入门题)

    Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 256000/1280 ...

  2. poj 2524:Ubiquitous Religions(并查集,入门题)

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23997   Accepted:  ...

  3. poj 3984:迷宫问题(广搜,入门题)

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7635   Accepted: 4474 Description ...

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

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  5. poj 3254 状压dp入门题

    1.poj 3254  Corn Fields    状态压缩dp入门题 2.总结:二进制实在巧妙,以前从来没想过可以这样用. 题意:n行m列,1表示肥沃,0表示贫瘠,把牛放在肥沃处,要求所有牛不能相 ...

  6. zstu.4194: 字符串匹配(kmp入门题&& 心得)

    4194: 字符串匹配 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 206  Solved: 78 Description 给你两个字符串A,B,请 ...

  7. hrbustoj 1073:病毒(并查集,入门题)

    病毒Time Limit: 1000 MS Memory Limit: 65536 KTotal Submit: 719(185 users) Total Accepted: 247(163 user ...

  8. hdu 1465:不容易系列之一(递推入门题)

    不容易系列之一 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  9. hrbustoj 1545:基础数据结构——顺序表(2)(数据结构,顺序表的实现及基本操作,入门题)

    基础数据结构——顺序表(2) Time Limit: 1000 MS    Memory Limit: 10240 K Total Submit: 355(143 users) Total Accep ...

随机推荐

  1. linux 跟踪工具

    strace工具,进程诊断.排错.跟踪系统调用和信号量 每行输出都是一个系统调用,包括函数和返回值. strace是Linux环境下的一款程序调试工具,用来监察一个应用程序所使用的系统调用及它所接收的 ...

  2. 16 redis之sentinel运维监控

    一:sentinel运维监控 Sentinel不断与master通信,获取master的slave信息. 监听master与slave的状态 如果某slave失效,直接通知master去除该slave ...

  3. WCF服务返回XML或JSON格式数据

    第一种方式public string GetData( string format) { string res = null; Student stu = new Student { StuID = ...

  4. mac上利用minikube搭建kubernetes(k8s)环境

    友情提示:对于初次接触k8s的同学,强烈建议先看看本文最后的参考文章. 环境: mac os(Mojave) 前提:先安装好kubectl (brew install kubectl) .docker ...

  5. Brotli

    https://engineering.linkedin.com/blog/2017/05/boosting-site-speed-using-brotli-compression?utm_sourc ...

  6. CrystalReport runtime的下载地址

    SAP网站的东西实在太多了,找个CrytalReport都费劲.13.*版的可以通过下面的地址下载: SAP Crystal Reports, developer version for Micros ...

  7. zabbix性能优化等

    摘自: http://blog.sina.com.cn/s/blog_4cbf97060101fcfw.html 非常好的一篇,值得有用

  8. 鸟哥的linux私房菜 - 第三章 主机规划与磁盘分区

    各硬件装置在linux中的文件名 在linux系统中,每个装置都被当成一个档案来对待. 常见的装置与其在linux中的档名: 磁盘分区 磁盘链接的方式与装置文件名的关系 个人计算机常见的磁盘接口有两种 ...

  9. 谷歌新操作系统fuchsia

    开源地址: https://github.com/fuchsia-mirror

  10. 在CI框架中的配置整合amfphp

    之前做的项目用到CI框架和amfphp的整合,主要用于php与flex的交互,在此做一下记录: 一. 安装CI框架: 1.  搭建PHP运行环境,本人在WIN7下用WAMP作测试,安装目录:d:/wa ...