MPI Maelstrom (Dijstra+atoi函数转换整数)
``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 这个题目比较长,不太好读,但是理解后题意比较简单,就是告诉你下三角的邻接矩阵的关系,值代表权值,然后问你1到其他点的最短时间,此题的一个不太好做的点在于怎么读图,因为既有数字也有字符串,只能用字符串,不然x没法读然后转换成整数就用atoi函数就行了,其余就是裸的Dijstra了
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#define Inf 0x3f3f3f3f using namespace std; int map[][];
char G[][]; int vis[],dis[];
int n,m;//n个点,m条边 void Init ()
{
memset(map,Inf,sizeof(map));
for(int i=;i<=n;i++)
{
map[i][i]=;
}
} void Getmap()
{
int u,v,w;
for(int t=;t<(n)*(n-)/;t++)
{
scanf("%s",G[t]);
}
int cnt=;
for(int t=;t<=n;t++)
{
for(int j=;j<t;j++)
{
if(G[cnt][]=='x')
{
w=Inf;
}
else
{
w=atoi(G[cnt]);
}
cnt++;
if(map[t][j]>w)
{
map[t][j]=w;
map[j][t]=w;
}
}
} } void Dijkstra(int u)
{
memset(vis,,sizeof(vis));
for(int t=;t<=n;t++)
{
dis[t]=map[u][t];
}
vis[u]=;
for(int t=;t<n;t++)
{
int minn=Inf,temp;
for(int i=;i<=n;i++)
{
if(!vis[i]&&dis[i]<minn)
{
minn=dis[i];
temp=i;
}
}
vis[temp]=;
for(int i=;i<=n;i++)
{
if(map[temp][i]+dis[temp]<dis[i])
{
dis[i]=map[temp][i]+dis[temp];
}
}
} } int main()
{
cin>>n;
Init();
Getmap();
Dijkstra();
int maxx=;
for(int t=;t<=n;t++)
{
maxx=max(dis[t],maxx);
}
cout<<maxx<<endl;
//printf("%d\n",dis[1]); return ;
}
MPI Maelstrom (Dijstra+atoi函数转换整数)的更多相关文章
- 【LeetCode】String to Integer (atoi)(字符串转换整数 (atoi))
这道题是LeetCode里的第8道题. 题目要求: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们 ...
- 【LeetCode】8. String to Integer (atoi) 字符串转换整数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...
- C语言atoi()函数:将字符串转换成int(整数)
头文件:#include <stdlib.h> atoi() 函数用来将字符串转换成整数(int),其原型为:int atoi (const char * str); [函数说明]atoi ...
- C语言itoa()函数和atoi()函数详解(整数转字符C实现)
1.int/float to string/array: C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串,下面列举了各函数的方法及其说明. ● itoa():将 ...
- C语言itoa()函数和atoi()函数详解(整数转字符)
http://c.biancheng.net/cpp/html/792.html C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. 以下是用itoa()函数将整 ...
- 17、字符串转换整数 (atoi)
17.字符串转换整数 (atoi) 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们寻找到的第一个非 ...
- [LeetCode] 8. 字符串转换整数 (atoi)
题目链接:https://leetcode-cn.com/problems/string-to-integer-atoi/ 题目描述: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先 ...
- 字符串转换整数 (atoi)
题目: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们寻找到的第一个非空字符为正或者负号时,则将该 ...
- LeetCode--008--字符串转换整数 (atoi)(java)
请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之 ...
随机推荐
- RabbitMq之消息确认
最近阅读了rabbitmq的官方文档,然后结合之前面试时被问到关于消息队列的问题来探索一下关于消息队列的消息确认机制. 其实消息确认就是消费者确认消息被消费了, 生产者确认消息已经发送到了消息队列中了 ...
- Qt之先用了再说系列-多线程方式1
Qt 多线程的用法还是比较简单的,也比较好用,接下来我们就分析分析如何使用. 说起Qt 线程的使用方式,一般有2种使用方式,具体哪种比较好看自己心情了,现在有官方的推荐用法,用不用还是看你心情的 好, ...
- python中1 is True 的结果为False,is判断与==判断的区别
python中1 is True 的结果为False,而1 == True的结果为True. python中True的数值就是1,那为什么1 is True 的结果为False呢? 因为is判断和== ...
- 2020-06-01:百万级int数据量的一个array求和。
福哥答案2020-06-01: fork/join. 对于百万级长度的数组求和,单线程和多线程下区别不大.对于千万级长度的数组求和,多线程明显变快,大概是单线程的2-3倍. go语言测试代码如下: p ...
- 2018-04-19:innodb和myisam区别
福哥答案2020-04-19:
- C#LeetCode刷题-数学
数学篇 # 题名 刷题 通过率 难度 2 两数相加 29.0% 中等 7 反转整数 C#LeetCode刷题之#7-反转整数(Reverse Integer) 28.6% 简单 8 字符串转整数 ...
- JDK8 String类知识总结
一.概述 java的String类可以说是日常实用的最多的类,但是大多数时候都只是简单的拼接或者调用API,今天决定深入点了解一下String类. 要第一时间了解一个类,没有什么比官方的javaDoc ...
- Mybatis 和 Solon 勾搭在一起,也是个漂亮组合
故事相关的源码 https://gitee.com/noear/solon_demo/tree/master/demo08.solon_mybatis 故事开讲 Solon 是Java世界里一个新的极 ...
- Java并发--三大性质
一.多线程的三大性质 原子性:可见性.有序性 二.原子性 原子性介绍 原子性是指:一个操作时不可能中断的,要么全部执行成功要么全部执行失败,有着同生共死的感觉.即使在多线程一起执行的时候,一个操作一旦 ...
- SpringMVC使用Session
Session在用户登录,一些特殊场合在页面间传递数据的时候会经常用到 @ 目录 修改IndexController check.jsp 效果 修改IndexController 映射 /check ...