【POJ - 1502】MPI Maelstrom(Dijkstra单源最短路--求一点到其余个点的最小值的最大值)

题干:

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

题目大意:

给你一个不完全的矩阵(下三角形),数字表示权值,x表示两点间不可达
由于自身到自身花费的时间为0,所以没有给出,由于i到j和j到i距离相同,互达时间相同
所以只给出了一半的临界矩阵。
根据给你的这个临界矩阵,让你来求从点1到其他点所花费最短时间集里面的的最大值。
其实这是一个很直接的最短路

解题报告:

     用o(n^2)的方法,邻接矩阵储存图,给一个下三角形矩阵,字符串读入,用get函数处理一下成int型权值,然后跑一边Dijkstra,再看从点1可以跑到那个点是最大值即可。

 

AC代码:

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
const int MAX = 100000 + 5;
const int INF = 0x3f3f3f3f ;
int vis[MAX];
int maze[105][105];
int n,m;
int dis[MAX];//表示从出发点开始到该点的最短距离。 void Dijkstra(int u,int v) {dis[u] = 0;int all = n,minw = INF,minv;//这里的初始化还是有讲头的。 for(int k = 1;k<= n;k++) {minw = INF;for(int i = 1; i<=n; i++) {if(vis[i] ) continue;if(dis[i] < minw) {minv = i; minw = dis[i];}}vis[minv] = 1;
//		if(minv == v) break;for(int i = 1; i<=n; i++) {if(vis[i]) continue;if(maze[minv][i] == 0) continue;dis[i] = min(dis[i], dis[minv] + maze[minv][i]);}}
} 
int get(char *s) {if(s[0] == 'x') return INF;int len = strlen(s);int ans = 0;for(int i = 0; i<len; i++) {ans = ans*10 + s[i]-'0';} return ans;
}
int main()
{int a,b;char w[20];while(~scanf("%d",&n) ) {memset(dis,0x3f3f3f3f,sizeof(dis) ) ;memset(maze,0,sizeof(maze) );memset(vis,0,sizeof(vis) ) ;for(int i = 2; i<=n; i++) {for(int j = 1; j<i; j++) {scanf("%s",w);maze[i][j] = maze[j][i] = get(w);}}Dijkstra(1,n); int ans = *max_element(dis+1,dis+n+1);printf("%d\n",ans);}return 0 ;} 

 

 总结:

     只要改一下模板,不让他在(minv == v) 的时候break掉就可以。并且此时传入的v没有任何卵用了。。。

     即 其实你传入的v也只有在这个判断的时候有用啊,其他时候你看模板里面都没有v出现。所以针对这个题我们只需要不让他在minv==v的时候停止,而是继续跑程序,就可以了。最后输出最大值。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441968.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java 强制清除缓存_IDEA强制清除Maven缓存的方法示例

重新导入依赖的常见方式下面图中的刷新按钮&#xff0c;在我的机器上&#xff0c;并不能每次都正确导入pom.xml中写的依赖项&#xff0c;而是导入之前pom.xml的依赖(读了缓存中的pom.xml)。当然除了这些&#xff0c;还可以下面这样&#xff1a;存在的问题上面虽然是重新导入Mav…

ACM算法--spfa算法--最短路算法

求单源最短路的SPFA算法的全称是&#xff1a;Shortest Path Faster Algorithm。 SPFA算法是西南交通大学段凡丁于1994年发表的。 从名字我们就可以看出&#xff0c;这种算法在效率上一定有过人之处。 很多时候&#xff0c;给定的图存在负权边&#xff0c;这时类似…

knn算法python理解与预测_理解KNN算法

KNN主要包括训练过程和分类过程。在训练过程上&#xff0c;需要将训练集存储起来。在分类过程中&#xff0c;将测试集和训练集中的每一张图片去比较&#xff0c;选取差别最小的那张图片。如果数据集多&#xff0c;就把训练集分成两部分&#xff0c;一小部分作为验证集(假的测试…

【POJ-3259】 Wormholes(判负环,spfa算法)

题干&#xff1a; While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Eac…

【HihoCoder - 1550】顺序三元组(思维)

题干&#xff1a; 给定一个长度为N的数组A[A1, A2, ... AN]&#xff0c;已知其中每个元素Ai的值都只可能是1, 2或者3。 请求出有多少下标三元组(i, j, k)满足1 ≤ i < j < k ≤ N且Ai < Aj < Ak。 Input 第一行包含一个整数N 第二行包含N个整数A1, A2, ... …

joptionpane java_Java JOptionPane

Java JOptionPane1 Java JOptionPane的介绍JOptionPane类用于提供标准对话框&#xff0c;例如消息对话框&#xff0c;确认对话框和输入对话框。这些对话框用于显示信息或从用户那里获取输入。JOptionPane类继承了JComponent类。2 Java JOptionPane的声明public class JOptionPa…

【POJ - 3268 】Silver Cow Party(Dijkstra最短路+思维)

题干&#xff1a; One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; roa…

【HDU - 3342】Legal or Not(拓扑排序)

题干&#xff1a; ACM-DIY is a large QQ group where many excellent acmers get together. It is so harmonious that just like a big family. Every day,many "holy cows" like HH, hh, AC, ZT, lcc, BF, Qinz and so on chat on-line to exchange their ideas.…

java 股票 代码_Java中利用散列表实现股票行情的查询_java

---- 在java中&#xff0c;提供了一个散列表类Hashtable&#xff0c;利用该类&#xff0c;我们可以按照特定的方式来存储数据&#xff0c;从而达到快速检索的目的。本文以查询股票的收盘数据为例&#xff0c;详细地说明java中散列表的使用方法。一、散列表的原理---- 散列表&am…

deepin部署python开发环境_deepin系统下部署Python3.5的开发及运行环境

deepin系统下部署Python3.5的开发及运行环境1 概述本人小白一枚&#xff0c;由于最近要学习python接口自动化测试&#xff0c;所以记录一下相关学习经过及经验&#xff0c;希望对跟我一样小白的朋友可以有所帮助。2 下载在python官网下载指定平台下的python3.5的环境wget https…

【HDU - 2066】:一个人的旅行(Dijkstra算法)

题干&#xff1a; 虽然草儿是个路痴&#xff08;就是在杭电待了一年多&#xff0c;居然还会在校园里迷路的人&#xff0c;汗~),但是草儿仍然很喜欢旅行&#xff0c;因为在旅途中 会遇见很多人&#xff08;白马王子&#xff0c;^0^&#xff09;&#xff0c;很多事&#xff0c;还…

for相关 java_Java学习之for循环相关知识梳理

for循环是编程语言中一种循环语句&#xff0c;是Java程序员日常工作中的重要组成部分。循环语句由循环体及循环的判定条件两部分组成&#xff0c;其表达式为&#xff1a;for(单次表达式;条件表达式;末尾循环体){中间循环体&#xff1b;}。拉勾IT课小编为大家分析如何使用这一属…

【HDU - 3790】最短路径问题(DIjkstra算法 双权值)

题干&#xff1a; 给你n个点&#xff0c;m条无向边&#xff0c;每条边都有长度d和花费p&#xff0c;给你起点s终点t&#xff0c;要求输出起点到终点的最短距离及其花费&#xff0c;如果最短距离有多条路线&#xff0c;则输出花费最少的。 Input 输入n,m&#xff0c;点的编号…

【POJ - 3037】Skiing (Dijkstra算法)

题干&#xff1a; Bessie and the rest of Farmer Johns cows are taking a trip this winter to go skiing. One day Bessie finds herself at the top left corner of an R (1 < R < 100) by C (1 < C < 100) grid of elevations E (-25 < E < 25). In or…

java web权限设计数据权限范围_JavaWeb 角色权限控制——数据库设计

相信各位读者对于角色权限管理这个需求并不陌生。那么是怎么实现的呢&#xff1f;今天小编来说道说道&#xff01;1、首先我们来进行数据库的设计&#xff0c;如何设计数据库是实现权限控制的关键&#xff1a;1)用户表&#xff1a;id&#xff1a;主键、自增、intname&#xff1…

modbus与硬件对接Java_java中modbus协议连接

modbus在java中的使用&#xff0c;首先maven的pom中引入modbus4j包com.infiniteautomationmodbus4j3.0.32. 我们创建类&#xff1a;ModBus4JTCPClient&#xff0c;创建ModbusMaster连接对象&#xff0c;以及读取寄存器方法package io.powerx.test;import org.apache.commons.la…

【51Nod-1100】 斜率最大(贪心)☆双排序

题干&#xff1a; 平面上有N个点&#xff0c;任意2个点确定一条直线&#xff0c;求出所有这些直线中&#xff0c;斜率最大的那条直线所通过的两个点。 &#xff08;点的编号为1-N&#xff0c;如果有多条直线斜率相等&#xff0c;则输出所有结果&#xff0c;按照点的X轴坐标排…

【HDU - 3714 】Error Curves (三分)

题干&#xff1a; Josephina is a clever girl and addicted to Machine Learning recently. She pays much attention to a method called Linear Discriminant Analysis, which has many interesting properties. In order to test the algorithms efficiency, she colle…

java中JLabel添加监听事件_[求助]关于JLabel添加监听器的问题。请各位帮忙!!

[求助]关于JLabel添加监听器的问题。请各位帮忙&#xff01;&#xff01;如图&#xff0c;我想在左边的JLabel上添加事件监听器&#xff0c;然后再去右边的JPane上进行绘制图形&#xff0c;请问这个事件监听器改怎么加&#xff0c;好象不能加ActionListener&#xff0c;要加什么…

指数循环节证明

还有关键的一步忘写了phi(m)>r的注意因为ma^r*m‘’所以phi(m)>phi(a^r)>r,所以就相当于phi(m)为循环节&#xff0c;不过如果指数小于phi(m)只能直接算了。。 注意这里的m与a^r是互质的上面忘写了。。 转自https://blog.csdn.net/guoshiyuan484/article/details/787…