POJ 1502 MPI Maelstrom( Spfa, Floyd, Dijkstra)
题目大意:
给你 1到n , n个计算机进行数据传输, 问从1为起点传输到所有点的最短时间是多少, 其实就是算 1 到所有点的时间中最长的那个点。
然后是数据
给你一个n 代表有n个点, 然后给你一个邻接矩阵, 只有一半,另一半自己补
下面是练习的代码。 分别用了Floyd 和 Dijkstra 还有 Spfa(邻接矩阵版)
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define INF 0xfffffff
#define maxn 1006
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
int n;
int maps[maxn][maxn], dist[maxn];
bool vis[maxn]; int Dijkstra()
{
dist[] = ;
for(int i=; i<=n; i++)
{
int index, Min = INF; for(int j=; j<=n; j++)
{
if(!vis[j] && Min > dist[j])
index = j, Min = dist[j];
} vis[index] = true; for(int j=; j<=n; j++)
{
if( !vis[j] )
dist[j] = min(dist[j], dist[index] + maps[index][j]);
}
}
int Max = ;
for(int i=; i<=n; i++)
Max = max(Max, dist[i]); return Max;
}
void ReadMaps()
{
char str[]; memset(vis,false,sizeof(vis)); for(int i=; i<=n; i++)
{
dist[i] = INF;
maps[i][i] = ;
for(int j=; j<i; j++)
{
scanf("%s", str); if(strcmp(str,"x") == )
maps[i][j] = INF;
else
sscanf(str,"%d", &maps[i][j]); maps[j][i] = maps[i][j];
}
}
}
int main()
{
while(cin >> n)
{
ReadMaps(); int ans = Dijkstra(); cout << ans << endl;
}
return ;
}
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define INF 0xfffffff
#define maxn 120 int G[maxn][maxn];
int n;
void Floyd();
void Slove();
void Init(); int main()
{
char str[];
int a;
while(cin>> n)
{
Init(); for(int i=; i<=n; i++)
{
for(int j=; j<i; j++)
{
scanf("%s", str); if(str[] == 'x')
a = INF;
else
sscanf(str,"%d", &a); G[i][j] = G[j][i] = a;
}
} Slove();
} return ;
}
void Floyd()
{
for(int k=; k<=n; k++)
{
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
G[i][j] = min(G[i][j],G[i][k] + G[k][j]);
}
}
}
}
void Slove()
{
Floyd();
int Max = ;
for(int i=; i<=n; i++)
{
Max = max(Max, G[][i]);
} cout << Max << endl;
}
void Init()
{
for(int i=; i<=n; i++)
{
for(int j=; j<i; j++)
G[i][j] = G[j][i] = INF;
G[i][i] = ;
}
}
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define INF 0xfffffff
#define maxn 120 int G[maxn][maxn];
int n, dist[maxn], vis[maxn];
void Spaf();
void Slove();
void Init(); int main()
{
char str[];
int a;
while(cin>> n)
{
Init(); for(int i=; i<=n; i++)
{
for(int j=; j<i; j++)
{
scanf("%s", str); if(str[] == 'x')
a = INF;
else
sscanf(str,"%d", &a); G[i][j] = G[j][i] = a;
}
} Slove();
} return ;
}
void Spfa()
{
int p;
queue<int> Q;
Q.push();
dist[] = ;
while( !Q.empty() )
{
p = Q.front();
Q.pop();
vis[p] = false;
for(int i=; i<=n; i++)
{
if(dist[i] > dist[p] + G[p][i])
{
dist[i] = dist[p] + G[p][i]; if( !vis[i] )
{
Q.push(i);
vis[i] = true;
}
}
}
}
}
void Slove()
{
Spfa();
int Max = ;
for(int i=; i<=n; i++)
{
Max = max(Max, dist[i]);
} cout << Max << endl;
}
void Init()
{
for(int i=; i<=n; i++)
{
dist[i] = INF, vis[i] = false;
for(int j=; j<i; j++)
G[i][j] = G[j][i] = INF;
G[i][i] = ;
}
}
POJ 1502 MPI Maelstrom( Spfa, Floyd, Dijkstra)的更多相关文章
- POJ 1502 MPI Maelstrom [最短路 Dijkstra]
传送门 MPI Maelstrom Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5711 Accepted: 3552 ...
- POJ 1502 MPI Maelstrom【floyd】
题目大意:求点1到所有点最短路径的最大值 思路:水题,单源最短路,网上解题清一色dijkstra,但是点数小于100显然floyd更简洁嘛 #include<cstdio> #includ ...
- 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
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: 6044 Accepted: 3761 Des ...
- POJ 1502 MPI Maelstrom(最短路)
MPI Maelstrom Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4017 Accepted: 2412 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)
题目链接:http://poj.org/problem?id=1502 题意是给你n个点,然后是以下三角的形式输入i j以及权值,x就不算 #include <iostream> #inc ...
- (简单) POJ 1502 MPI Maelstrom,Dijkstra。
Description BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odysse ...
随机推荐
- .Net Framework 4.0安装cmd命令
在安装系统以后和.Net FrameWork 后,通过cmd编译编写的程序时总是提示编译错误.可以通过cmd命令安装相应的.net framework版本. 具体步骤如下: 1.以管理员身份打开cmd ...
- ORACLE 11G EXP导出空表方法
EXP在导出11G的库的时候,与过去10G,9I的版本有很大的差别. 就是没有数据的表是不会分配空间的. 从Oracle 11.2.0.1版本开始,Oracle又提供了一种新的空间分配方法: Crea ...
- git使用介绍
Git简单介绍 参考网址: git使用简介 这个教程推荐使用:git教程 git和svn的差异 git和svn的最大差异在于git是分布式的管理方式而svn是集中式的管理方式.如果不习惯用代码管理工具 ...
- java生成Json工具之JsonSimple的使用
json-simple是由是Google开发的Java JSON解析框架,基于Apache协议.目前版本为1.1 项目主页:https://code.google.com/p/json-simple/ ...
- c++回调编程本质
1. boost:bind获得一个函数对象,就像函数指针一样,这个行为可以作为回调 2. bosot:bind的函数对象可以保存别的对象的引用,回调对象的成员函数 3. boost:function是 ...
- 佛祖保佑 永无BUG(网转 by atkfc)
// _ooOoo_ // o8888888o // 88" . ...
- Sql Server插入随机数
--处理性别随机select (case when round(rand()*10,0)>5 then '男' else '女' end), --处理时间段范围内随机select dateadd ...
- 分页技术之PageDataSource类
之前给大家介绍了分页技术之Gridview控件,今天给大家介绍另外一种分页技术,采用PageDataSource类 + Repeater控件来实现. 前台只需要拖出一个Repeater控件来绑定要显示 ...
- ASP.NET 开发学习视频教程大全(共800集)
ASP.NET是微软.NET平台的支柱之一,被广泛应用在WEB等互联网开发领域,因此它的强大性和适应性,可以使它运行在Web应用软件开发者的几乎全部的平台上.这里整理了最全的ASP.NET开发学习 ...
- Delphi PChar与String互转
1.String转化成PChar 例: var str: string; pStr:PChar; ... pStr := PChar(str); 2.PChar转String 例: var pStr: ...