您的位置:首页 > 其它

【图论--Dijkstra】POJ 1502 MPI Maelstrom

2018-03-10 21:29 330 查看

 

 POJ 1502 MPI Maelstrom

Description 题目链接->点击打开链接
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 Input5
50
30 5
100 20 50
10 x x 10Sample Output
35

Problem Idea 解题思路

  【题意】  整个第一段题意描述都是无关的话,可以直接看Input.    题目要求概述一下则是:输入一个节点数为n的无向图的邻接矩阵的下三角部分.要求你输出从第0个点到所有其他点的距离的最大值
 【类型】  Dijkstra算法求单源最短路径 【分析】  模板题,入门 【时间复杂度&&优化】  O(nlog n)  Source Code#include <iostream>
#include <queue>
#include <vector>
#include <cstring>
#include <cstdio>

const int nmax=100+5;
#define INF 1e8
int n;
using namespace std;

struct HeapNode{
int d,u;//d为s到各个节点的距离,u为起点
HeapNode(){}
HeapNode(int d,int u):d(d),u(u){}
bool operator <(const HeapNode &rhs)const{//自定义greater算子,优先输出d小的节点
return d>rhs.d;
}
};
struct Edge{
int from,to,dist;
Edge(){}
Edge(int f,int t,int d):from(f),to(t),dist(d){}
};
struct Dijkstra{
int n,m; //n为点数,m为边数
vector<Edge> edges;//边列表,存储各边的编号
vector<int>G[nmax];//邻接表,存储每个节点出发的边编号(从0开始编号),G[u][i]为起点u到节点i的边的编号
bool done[nmax];//是否已永久标号
int d[nmax];//源点s到各个节点的距离,id[i]为源点s到节点i的距离
int p[nmax];//最短路中的上一条弧,p[i]为源点s到节点i的最短路中的最后一条边的编号
Dijkstra(){} //记得写个空的构造函数,要不然DJ会报错

void init(int n){
this->n=n;
for(int i=0;i<n;i++){
G[i].clear();//清空邻接表
}
edges.clear();//清空边列表
}

void AddEdge(int from,int to,int dist){//如果是无向图,每条无向边调用两次AddEdge
edges.push_back(Edge(from,to,dist));//边列表增加一条边
m=edges.size();//边列表中有几条边
G[from].push_back(m-1);//邻接表中的节点数目(下标从0开始)
}

void dijkstra(int s){//求s到所有点的距离
for(int i=0;i<n;i++) d[i]=INF;
d[s]=0;//记得源点从0开始,不是1开始
memset(done,0, sizeof(done));

priority_queue<HeapNode>q;
q.push(HeapNode(0,s));

while(!q.empty()){
//在所有未标号的节点中,选出d值最小的节点
HeapNode x=q.top();//找到d值最大的节点
q.pop();//弹出队头
int u=x.u;//得到d值最大的节点的起点
//给节点x标记
if(done[u]) continue; //如果当前节点已经标号,则continue
done[u]=1; //若未标号,则标记
//遍历从x出发的所有边(x,y),更新d[y]=min{d[y],d[x]+w[x,y]}
for(int i=0;i<G[u].size();i++){
Edge& e=edges[G[u][i]];//得到从u起点出发到i节点的一条边e
if(d[e.to]>d[u]+e.dist){
d[e.to]=d[u]+e.dist;
p[e.to]=G[u][i];
q.push(HeapNode(d[e.to],e.to));//添加节点y
}
}
}
}
}DJ;
int main() {
while(cin>>n){//输入节点数
char str[10];
int d;
DJ.init(n);//切记要初始化,清空边列表和邻接表
for(int i=1;i<n;i++){
for(int j=0;j<i;j++){
scanf("%s",str);
if(str[0]!='x'){
sscanf(str,"%d",&d);//字符串转整数
DJ.AddEdge(i,j,d);
DJ.AddEdge(j,i,d);
}
}
}
DJ.dijkstra(0);//得到节点0到其他节点的最短路径长度
int max_v=-1;
for(int i=0;i<n;i++){
max_v=max(max_v,DJ.d[i]);//得到节点0到其他节点的最短路径长度的最大值
}
cout<<max_v<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: