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 ...
随机推荐
- Eureka中的三种角色分别是什么?
Eureka中的三种角色分别是什么? 1.Eureka Server 通过Register.Get.Renew等接口提供服务的注册和发现. 2.Application Service (Service ...
- cehsi
weibo https://oapi.dingtalk.com/robot/send?access_token=8c9ef96c99925383347c5f9f733ad6b8579c3f8ad072 ...
- C++11的闭包(lambda、function、bind)
c++11开始支持闭包,闭包:与函数A调用函数B相比较,闭包中函数A调用函数B,可以不通过函数A给函数B传递函数参数,而使函数B可以访问函数A的上下文环境才可见(函数A可直接访问到)的变量:比如: 函 ...
- [原创] delphi KeyUp、KeyPress、Keydown区别和用法,如何不按键盘调用事件
KeyPress (Sender: TObject; var Key: Char); 当用户按下键盘上的字符键(字母,数字) 会触发该事件,功能键则不会(F1-F12,Ctrl,Alt,Shift ...
- 我们能从java的HelloWorld学到什么?
这是每个Java程序员都知道的.虽然简单,但是从一个简单的问题可以引入更深的思考.在这篇文章中,我们将讨论这个简单的程序.如果能更多的帮到你,请留下宝贵的意见. HelloWorld.java pub ...
- iptables开通某些端口
#!/bin/bash #define all variance or parameter WAH_INT="eth0" WAH_INT_IP="222.222.101. ...
- 常用Git命令以及出现的状况ing
(有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 我的GitHub: Cwolf9 下面是我学习Git时了解到的一些命令和状况经验. 把它们记下来免得忘了.就算忘了也有地方看... 状 ...
- Async_Study
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- class7_Checkbutton 勾选项
最终的运行效果(程序见序号3): #!/usr/bin/env python# -*- coding:utf-8 -*-# ------------------------------------ ...
- git学习记录1(本地库管理)
学习参考地址:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 本编随笔只是自己对 ...