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 ...
随机推荐
- 前端之script标签注意事项
在一对script 标签中一旦有错误,其后续的代码都不会执行 一对script标签有问题,不会影响其他script标签代码的执行 当一对script标签的作用是引入外部的js文件的时候,就不要在其内部 ...
- 通过js渲染高层级DOM实现网页加水印
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- java----int,string 转化为long
String: 1.返回Long包装类型: String str = "aaa"; long l = Long.parseLong([str]); 2.返回long基本数据类型: ...
- mysql在win系统dos 安装版配置步骤详解
1.准备工作 下载mysql的最新免安装版本mysql-noinstall-5.1.53-win32.zip,解压缩到相关目录,如:d:\ mysql-noinstall-5.1.53-win32.这 ...
- params拦截器
1. params拦截器首先给action中的相关参数赋值,如id 2. prepare拦截器执行prepare方法,prepare方法中会根据参数,如id,去调用业务逻辑,设置model对象 ...
- Python-爬虫-爬取知乎的标题和当页显示的文字
# coding:utf-8 import requests from bs4 import BeautifulSoup quesNumStr = str(input("请输入搜索关键字:& ...
- 关于group by的用法
重新回顾并理解group by. 首先设计一张表,表名为test 然后执行以下SQL语句: select name from test group by name 获得执行结果: 可是为了能够更好的理 ...
- 网络安全系列 之 TLS/SSL基本原理
1. TLS/SSL基本工作方式: TLS/SSL的功能实现主要依赖于三类基本算法(参见"网络安全系列 之 密码算法"): 非对称加密算法:实现身份认证和密钥协商 对称加密算法: ...
- 1.springboot+ActiveMQ
1.项目结构如下 pom.xml文件如下 <dependencies> <dependency> <groupId>junit</groupId> &l ...
- npm -v 报错:cannot find module 'core-util-is'
今天想打开之前的项目运行看看,结果报错:cannot find module 'core-util-is',以为只是缺少模块core-util-is,然后npm install --save core ...