POJ-1502-MPI Maelstrom-dijkstra+输入处理
``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 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
Sample Input
5
50
30 5
100 20 50
10 x x 10
Sample Output
35 题意:
一个信息站需要往其他信息站传输信息。当一个站接受到信息之后马上向跟它相连的信息站传输信息。已知每两个相连信息站之间传输时间。
求:信息从第一个站传到每个站的最短时间。
思路:将传输代价作为路径长度,即求第一个信息站到其它所有信息站最短路径的最大值。 题意比较难读懂,做题还用到了一个新函数atoi atoi用法:把字符串型转化成整型
头文件#include<stdlib.h>
注意:atoi()函数只能转换为整数,如果一定要写成浮点数的字符串,它会发生强制转换
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std; int main()
{
char ss[];
int aa=;
scanf("%s",&ss);
int dd=atoi(ss);
printf("**%d**\n",aa+dd);
return ;
}
测试数据输出

题目代码如下:
#include<iostream>
#include<string.h>
#include<cmath>
#include<stdio.h>
#include<algorithm>
#include<stdlib.h>
#include<iomanip>
#define inf 0x3f3f3f3f
#define ios() std::ios::sync_with_stdio(false)
#define cin() cin.tie(0)
#define cout() cout.tie(0)
#define mem1(a) memset(a,0,sizeof(a))
#define mem2(b) memset(b,'\0',sizeof(b))
using namespace std; int a[][];
int dis[],book[];
int n;
char s[]; void init()
{
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
if(i==j)
a[i][j]=;
else
a[i][j]=inf;
}
}
return ;
} int dijkstra()
{
mem1(book);
for(int i=; i<=n; i++)
dis[i]=a[][i];
book[]=;
int u;
for(int i=; i<=n-; i++)
{
int minn=inf;
for(int j=; j<=n; j++)
{
if(book[j]==&&dis[j]<minn)
{
minn=dis[j];
u=j;
}
}
book[u]=;
for(int k=; k<=n; k++)
{
if(a[u][k]<inf)
{
if(dis[k]>dis[u]+a[u][k])
{
dis[k]=dis[u]+a[u][k];
}
}
}
}
int maxx=-inf;
for(int i=; i<=n; i++)
maxx=max(maxx,dis[i]);
return maxx;
} int main()
{
// ios();
// cin();
// cout(); cin>>n;
init();
//?数组a开到110即可,x的ASCII是120,不会和其他重复
for(int i=; i<=n; i++)
{
for(int j=; j<i; j++)
{ // cin>>a[i][j];
// if(a[i][j]=='x')
// {
// a[i][j]=inf;
// }
// a[j][i]=a[i][j];
93 scanf("%s",s);
94 if(s[0]=='x')
95 a[i][j]=a[j][i]=inf;
96 else
97 a[i][j]=a[j][i]=atoi(s);//注意输入
}
}
cout<<dijkstra()<<endl;
return ;
}
一般写法,不用函数:(由于数据比较小,所以可以利用五行代码三层for循环来写)
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
using namespace std;
#define inf 0x3f3f3f3f int e[][];
char a[]; int main()
{
int n;
scanf("%d",&n);
memset(e,,sizeof(e));
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
if(i==j)
e[i][j]=;
else
e[i][j]=inf;
}
} int sum;
for(int i=; i<=n; i++)
{
for(int j=; j<=i-; j++)
{
scanf("%s",a);
int len=strlen(a);
if(a[]=='x')
sum=inf;
else
{
sum=;
for(int k=; k<len; k++)
{
sum=sum*+(a[k]-'');
}
}
e[i][j]=e[j][i]=sum;
}
} for(int k=; k<=n; k++)
{
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
e[i][j]=min(e[i][j],e[i][k]+e[k][j]);
}
}
} int ans=-inf;
for(int i=;i<=n;i++)
ans=max(ans,e[][i]); printf("%d\n",ans);
return ;
}
POJ-1502-MPI Maelstrom-dijkstra+输入处理的更多相关文章
- POJ 1502 MPI Maelstrom (Dijkstra)
题目链接:http://poj.org/problem?id=1502 题意是给你n个点,然后是以下三角的形式输入i j以及权值,x就不算 #include <iostream> #inc ...
- POJ 1502 MPI Maelstrom / UVA 432 MPI Maelstrom / SCU 1068 MPI Maelstrom / UVALive 5398 MPI Maelstrom /ZOJ 1291 MPI Maelstrom (最短路径)
POJ 1502 MPI Maelstrom / UVA 432 MPI Maelstrom / SCU 1068 MPI Maelstrom / UVALive 5398 MPI Maelstrom ...
- POJ 1502 MPI Maelstrom [最短路 Dijkstra]
传送门 MPI Maelstrom Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5711 Accepted: 3552 ...
- POJ 1502 MPI Maelstrom
MPI Maelstrom Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other) Total ...
- POJ 1502 MPI Maelstrom(最短路)
MPI Maelstrom Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4017 Accepted: 2412 Des ...
- POJ 1502 MPI Maelstrom (最短路)
MPI Maelstrom Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6044 Accepted: 3761 Des ...
- POJ - 1502 MPI Maelstrom 路径传输Dij+sscanf(字符串转数字)
MPI Maelstrom BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odys ...
- (简单) POJ 1502 MPI Maelstrom,Dijkstra。
Description BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odysse ...
- POJ 1502 MPI Maelstrom( Spfa, Floyd, Dijkstra)
题目大意: 给你 1到n , n个计算机进行数据传输, 问从1为起点传输到所有点的最短时间是多少, 其实就是算 1 到所有点的时间中最长的那个点. 然后是数据 给你一个n 代表有n个点, 然后给你一 ...
- POJ 1502 MPI Maelstrom(模板题——Floyd算法)
题目: BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distri ...
随机推荐
- 网址URL知识
URL由三部分组成:资源类型.存放资源的主机域名.资源文件名. URL的一般语法格式为: (带方括号[]的为可选项): protocol :// hostname[:port] / path / [; ...
- spark运行任务报错:Container [...] is running beyond physical memory limits. Current usage: 3.0 GB of 3 GB physical memory used; 5.0 GB of 6.3 GB virtual memory used. Killing container.
spark版本:1.6.0 scala版本:2.10 报错日志: Application application_1562341921664_2123 failed 2 times due to AM ...
- 修改Ubuntu16.04默认主题标题栏的颜色
默认主题为Ambiance: sudo gedit /usr/share/themes/Ambiance/gtk-3.0/gtk-main.css 将: @define-color dark_bg_c ...
- javafx教程大全
链接: https://www.yiibai.com/javafx
- HDU6315 Naive Operations 线段树
目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:Portal传送门 原题目描述在最下面. Solution ...
- 去掉Word 标题编号变成黑框
问题: 在使用Word编写文档时,提前拟好的标题编号会突然变成黑框(黑色的方框,黑色的矩形),如下图 解决方案: 1.将光标定位到标题中,紧邻黑框的右侧 2.按键盘左方向键使方框变成黑色 3.按键盘的 ...
- i++ 和 ++i 的区别
先说运算规则吧. i++ 是先赋值后自增:++i 是先自增后赋值. 以下是代码示例: int a=0; int b=0; int i=0; a=i++; System.out.println(&quo ...
- JAVA利用JXL导出 EXCEL (在原有的excel模板上把数据导到excel上)
添加依赖 <dependency> <groupId>net.sourceforge.jexcelapi</groupId> <artifactId>j ...
- Soci介绍
soci是一个用C++封装的数据库访问库,目前通过 “前端(应用程序)/核心/后端(数据库适配)”模式支持firebird,mysql,sqlite3,oracle,postgresql,odbc多种 ...
- VMware 安装android-x86系统。
首先先安装 VMware 虚拟机,并下载 android-x86_64-8.1-r2.iso 系统. VMware安装完成后,打开VMware Workstation,单击“创建新的虚拟机”,或者在菜 ...