MPI Maelstrom POJ - 1502 ⭐⭐ 【Dijkstra裸题】
MPI Maelstrom POJ - 1502
实验室有很多台计算机,由于每个人计算机的性能不同,导致计算机之间发送信息的速度不同,所以花费时间不同。
消息从第一台电脑发送到第二台电脑后,这两台电脑能再向其他电脑发送消息,就是一种类似二叉树的结构。
当然并不是真正的二叉树——我们的计算机有一些特殊的特性,我们应该加以利用。
我们的计算机允许同时向连接到它的任意数量的其他计算机发送消息。
然而,消息不一定同时到达目的地——这涉及到计算机配置。
一般来说,我们需要考虑到拓扑结构中每个计算机的时间成本,并相应地计划将消息传播所需的总时间降到最低。
涂爷需要经常给其他人发消息,她想要知道如果她发出一个消息,至少要等多久全部的人才可以都收到通知。
Input
第一行输入一个n,表示有多少台计算机(涂爷当然是一号机啦~)。
随后n-1行,以邻接矩阵的形式给出计算机(1~n)之间通讯需要的时间。
由于两台计算机相互通信时间相等,且计算机自己和自己不需要通信,所以只给出了矩阵的下三角。
ps:x表示由于部分计算机之间存在特殊的磁场并不能通信。
ps2:该题所有数据范围0~100。
Output
输出一行表示涂爷需要等待的时间。.
Examples
Sample Input
5
50
30 5
100 20 50
10 x x 10
Sample Output
35
数据只有100,所以便利使用Floyd也是可以的
Floyd
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
const int maxn = 110;
const int inf = 0x3f3f3f3f;
int e[maxn][maxn];
int n;
void floyd() {
for (int k = 1; k <= n; ++k)
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
if (e[i][j] > e[i][k] + e[k][j])
e[i][j] = e[i][k] + e[k][j];
}
int main() {
//freopen("in.txt","r",stdin);
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n; char c[20];
for (int i = 1; i <= n; ++i)for (int j = 1; j <= n; ++j)e[i][j] = inf;
for (int i = 1; i <= n; ++i)e[i][i] = 0;
for(int i = 2;i<=n;++i)
for (int j = 1; j <= i - 1; ++j) {
cin >> c;
if (c[0] == 'x')continue;
e[i][j] = e[j][i] = atoi(c);
}
floyd();
int ans = 0;
for (int i = 2; i <= n; i++) {
if (e[1][i] != inf)
ans = max(ans, e[1][i]);
}
cout << ans << endl;
}
堆优化的Dijkstra
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
typedef pair<int, int> PII;
const int inf = 0x3f3f3f3f;
const int maxn = 110;
int w[maxn][maxn];
int dis[maxn];
bool book[maxn];
int n;
void Dijkstra() {
priority_queue<PII, vector<PII>, greater<PII> > q;
memset(book, false, sizeof book);
for (int i = 2; i <= n; ++i)dis[i] = inf;
dis[1] = 0; book[0] = true;
q.push({ 0,1 });
while (q.size()) {
PII cur = q.top(); q.pop();
int u = cur.second;
if (book[u])continue;
book[u] = true;
for (int v = 1; v <= n; ++v) {
if (dis[u] + w[u][v] < dis[v]) {
dis[v] = dis[u] + w[u][v];
q.push({ dis[v], v });
}
}
}
}
int Stoi(string s) {
int ret = 0;
for (int i = s.length() - 1, j = 1; i >= 0; --i, j *= 10)
ret += (s[i] - '0') * j;
return ret;
}
int main() {
string input;
fill(w[0], w[0] + maxn * maxn, inf);
cin >> n;
for (int i = 2; i <= n; ++i)
for (int j = 1; j < i; ++j) {
cin >> input;
if (input != "x") w[i][j] = w[j][i] = Stoi(input);
}
Dijkstra();
int ans = 0;
for (int i = 2; i <= n; ++i)
ans = max(ans, dis[i]);
cout << ans << endl;
return 0;
}
MPI Maelstrom POJ - 1502 ⭐⭐ 【Dijkstra裸题】的更多相关文章
- kuangbin专题专题四 MPI Maelstrom POJ - 1502
题目链接:https://vjudge.net/problem/POJ-1502 dijkstra板子题,题目提供下三角情况,不包含正对角线,因为有题意都为0,处理好输入,就是一个很水的题. #inc ...
- MPI Maelstrom POJ - 1502 floyd
#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> usin ...
- POJ 1502 MPI Maelstrom [最短路 Dijkstra]
传送门 MPI Maelstrom Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5711 Accepted: 3552 ...
- POJ 1502 MPI Maelstrom( Spfa, Floyd, Dijkstra)
题目大意: 给你 1到n , n个计算机进行数据传输, 问从1为起点传输到所有点的最短时间是多少, 其实就是算 1 到所有点的时间中最长的那个点. 然后是数据 给你一个n 代表有n个点, 然后给你一 ...
- POJ 2387 Til the Cows Come Home(dijkstra裸题)
题目链接:http://poj.org/problem?id=2387 题目大意:给你t条边(无向图),n个顶点,让你求点1到点n的最短距离. 解题思路:裸的dijsktra,注意判重边. 代码: # ...
- hdu 2544 hdu 1874 poj 2387 Dijkstra 模板题
hdu 2544 求点1到点n的最短路 无向图 Sample Input2 1 //结点数 边数1 2 3 //u v w3 31 2 52 3 53 1 20 0 Sample Output32 ...
- POJ 1330 LCA裸题~
POJ 1330 Description A rooted tree is a well-known data structure in computer science and engineerin ...
- POJ 3264 RMQ裸题
POJ 3264 题意:n个数,问a[i]与a[j]间最大值与最小值之差. 总结:看了博客,记下了模板,但有些地方还是不太理解. #include<iostream> #include&l ...
- POJ 1502:MPI Maelstrom Dijkstra模板题
MPI Maelstrom Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6499 Accepted: 4036 Des ...
- 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 ...
随机推荐
- 谷歌浏览器和火狐浏览器如何查看HTTP协议?
按F12
- [ABC262E] Red and Blue Graph
Problem Statement You are given a simple undirected graph with $N$ vertices and $M$ edges. The verti ...
- C++学习笔记八:极限和数学运算<limits><cmath>
1) <limits>库: 1.1 源文档: https://en.cppreference.com/w/cpp/types/numeric_limits #include <lim ...
- NetSuite 开发日记:解决导入 CSV 与 Excel 文件时中文乱码问题
Backgroud 在使用SheetJS库导入带有中文的CSV文件时,中文被解析为了乱码 Analysis 乱码肯定是编码问题 确定CSV的编码,可使用VS Code.记事本来查看 修改CSV文件编码 ...
- 信创选国产,Solon v2.6.3 发布
Solon 是什么框架? Java 新的"生态级"应用开发框架.从零开始构建,有自己的标准规范与开放生态(历时六年,具备全球第二级别的生态规模). 相对于 Spring,有什么特点 ...
- selenium之鼠标键盘操作
鼠标操作 1.引入ActionChains类 2.定位相关元素 3.在ActionChains().调用相关鼠标操作方法 from selenium.webdriver.common.action_c ...
- 从零玩转系列之微信支付实战PC端支付微信退款接口搭建 | 技术创作特训营第一期
一.前言 从零玩转系列之微信支付实战PC端支付微信退款接口搭建 | 技术创作特训营第一期 继前文章取消订单接口和查询订单接口此篇为申请退款流程,此篇文章过长我将分几个阶段的文章发布(项目源码都有,小程 ...
- CodeForces 1030E Vasya and Good Sequences 位运算 思维
原题链接 题意 目前我们有一个长为n的序列,我们可以对其中的每一个数进行任意的二进制重排(改变其二进制表示结果的排列),问我们进行若干次操作后得到的序列,最多能有多少对 \(l, r\) 使得 \([ ...
- 斐波那契数Fibonacci
509. 斐波那契数 斐波那契数,通常用 F(n) 表示,形成的序列称为斐波那契数列.该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和.也就是: F(0) = 0, F(1) = ...
- JVM学习-类加载机制
文章原文:https://gaoyubo.cn/blogs/4b481fd7.html 一.类加载机制 在JVM学习-Class文件结构中,讲了Class文件存储格式的具体细节.虽然Class文件中描 ...