Problem Description
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.
题意:
有n个处理器,根据题目要求求从第1个处理器开始,到信息传播到其余所有处理器所需要的耗费。
思路:
Dijkstra算法,起始点必须是从第一个点开始。
import java.util.Scanner;
public class MainD1003 {
class graph{
int [][] adj;
Path dist[];
void setV0(int v0){
dist[v0].length = 0;
dist[v0].prevex = 0;
adj[v0][v0] = 1;
}
public graph(int n) {
// TODO 自动生成的构造函数存根
adj = new int[n][n];
for(int i =0;i<n;i++){
for(int j=0;j<n;j++){
if(i!=j){
adj[i][j]=Integer.MAX_VALUE;
}else{
adj[i][j]=0;
}
}
}
}
}
class Path{
int length;
int prevex;
public Path(int len,int pre) {
// TODO 自动生成的构造函数存根
length = len;
prevex = pre;
}
public Path() {
// TODO 自动生成的构造函数存根
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
MainD1003 demo = new MainD1003();
int n;
int i,j,k,q;
String temp;
int t;
int mv,min=Integer.MAX_VALUE,minw;
int max[];
n = scanner.nextInt();
int graph[][] = new int[n][n]; //生成题目给出的邻接矩阵
for(i =0;i<n;i++){
for(j=0;j<n;j++){
if(i!=j){
graph[i][j]=Integer.MAX_VALUE;
}else{
graph[i][j]=0;
}
}
}
for(i=1;i<n;i++){
for(j=0;j<i;j++){
temp = scanner.next();
if(!temp.equals("x")){
t = Integer.parseInt(temp);
graph[i][j] = t;
graph[j][i] = t;
}
}
}
max = new int[n]; //存放各点分别到其他点一共需要的时间
for(i=0;i<1;i++){
graph gdemo = demo.new graph(n); //生成求第i个点到其他各点需要的时间时的操作矩阵
gdemo.dist = new Path[n];
for(k=0;k<n;k++){
for(q=0;q<n;q++){
gdemo.adj[k][q] = graph[k][q];
}
}
for(j=0;j<n;j++){
gdemo.dist[j] = demo.new Path();
}
gdemo.setV0(i);
for(j=0;j<n;j++){ //存放第i各点到其余各点距离的数组dist
gdemo.dist[j].length = gdemo.adj[i][j];
if(gdemo.dist[j].length!=Integer.MAX_VALUE){
gdemo.dist[j].prevex = i;
}else{
gdemo.dist[j].prevex = -1;
}
}
for(k=0;k<n;k++){
minw = Integer.MAX_VALUE;
mv = -1;
for(j=0;j<n;j++ ){
if(gdemo.adj[j][j]==0&&gdemo.dist[j].length<minw){
mv = j;
minw = gdemo.dist[j].length;
}
}
if(mv==-1){
break;
}
gdemo.adj[mv][mv] = 1;
for(j=0;j<n;j++){
if(gdemo.adj[j][j]==0&&gdemo.dist[j].length>gdemo.dist[mv].length+gdemo.adj[mv][j]){
if(gdemo.dist[mv].length+gdemo.adj[mv][j]>0){
gdemo.dist[j].prevex = mv;
gdemo.dist[j].length = gdemo.dist[mv].length + gdemo.adj[mv][j];
}
}
}
}
for(j=0;j<n;j++){
if(max[i]<gdemo.dist[j].length){
max[i] = gdemo.dist[j].length;
}
}
}
for(i=0;i<1;i++){
if(min>max[i]){
min = max[i];
}
}
System.out.println(min);
}
}