POJ-1502 MPI Maelstrom 迪杰斯特拉+题解
POJ-1502 MPI Maelstrom 迪杰斯特拉+题解
题意
题意:信息传输,总共有n个传输机,先要从1号传输机向其余n-1个传输机传输数据,传输需要时间,给出一个严格的下三角(其实就是对角线之下的不包括对角线的部分)时间矩阵,a[i][j]代表从i向j传输数据需要的时间,并规定数据传输之间并无影响,即第一个传输机可以同时向其余传输机传输数据。求所有所有的机器都收到消息(他们收到消息后也可以传输)所需的最短时间。
解题思路
这个可以用迪杰斯特拉来求第一台机器到其他所有机器传输消息的时间,然后答案就是到其他机器所需时间的最大值。
下面给了两种
代码实现
//n方,没有优化那种
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e2+7;
const int inf=0x3f3f3f3f;
int mp[maxn][maxn];
int dis[maxn];
int vis[maxn];
int n;
void init()
{
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
mp[i][j]= i==j? 0:inf;
fill(dis+1, dis+n+1, inf);
fill(vis+1, vis+n+1, 0);
}
void dij()
{
for(int i=1; i<=n; i++)
dis[i]=mp[1][i];
vis[1]=1;
for(int i=1; i<n; i++)
{
int tmp=inf, k;
for(int j=1; j<=n; j++)
{
if(!vis[j] && dis[j]<tmp)
{
tmp=dis[j];
k=j;
}
}
vis[k]=1;
for(int j=1; j<=n; j++)
{
if(!vis[j] && dis[j] > dis[k]+mp[k][j] )
{
dis[j]=dis[k]+mp[k][j];
}
}
}
}
int main()
{
while(scanf("%d", &n)!=EOF)
{
init();
char num[10];
int tmp, len;
for(int i=2; i<=n; i++)
{
for(int j=1; j<i; j++)
{
scanf("%s", num);
if(num[0]=='x') continue;
tmp=0;
len=strlen(num);
for(int k=0; k<len; k++)
{
tmp=tmp*10+num[k]-'0';
}
mp[i][j]=tmp;
mp[j][i]=tmp;
}
}
dij();
int ans=0;
for(int i=1; i<=n; i++)
{
ans=max(ans, dis[i]);
}
printf("%d\n", ans);
}
return 0;
}
//使用优先队列进行优化
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=1e3+7;
const int maxe=1e6+7;
struct headnode{
int d, u;
bool friend operator < (const headnode a, const headnode b)
{
return a.d > b.d;
}
};
struct edge
{
int to, cost;
};
int n;
int dis[maxn];
int vis[maxn];
vector<edge> g[maxn];
priority_queue<headnode> que;
void init()
{
for(int i=1; i<=n; i++)
{
dis[i]=inf;
vis[i]=0;
g[i].clear();
}
while(!que.empty()) que.pop();
}
void dij(int s)
{
edge e;
dis[s]=0;
headnode head={0, s}, tmp;
que.push(head);
while(!que.empty())
{
head=que.top();
que.pop();
if(vis[head.u]==1)continue;
vis[head.u]=1;
for(int i=0; i<g[head.u].size(); i++)
{
e=g[head.u][i];
if(dis[e.to] > dis[head.u]+e.cost)
{
dis[e.to]=dis[head.u]+e.cost;
tmp.d=dis[e.to];
tmp.u=e.to;
que.push(tmp);
}
}
}
}
int main()
{
while(scanf("%d", &n)!=EOF)
{
init();
int c;
edge e;
char str[10];
for(int i=2; i<=n; i++)
{
for(int j=1; j<i; j++)
{
scanf("%s", str);
if(str[0]=='x') continue;
c=atoi(str);
e.cost=c;
e.to=j;
g[i].push_back(e);
e.to=i;
g[j].push_back(e);
}
}
dij(1);
int ans=0;
for(int i=1; i<=n; i++)
ans=max(dis[i], ans);
printf("%d\n", ans);
}
return 0;
}
END
POJ-1502 MPI Maelstrom 迪杰斯特拉+题解的更多相关文章
- 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 2502 Subway(迪杰斯特拉)
Subway Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6692 Accepted: 2177 Descriptio ...
- POJ 1502 MPI Maelstrom(最短路)
MPI Maelstrom Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4017 Accepted: 2412 Des ...
- 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 路径传输Dij+sscanf(字符串转数字)
MPI Maelstrom BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odys ...
- POJ 1502 MPI Maelstrom [最短路 Dijkstra]
传送门 MPI Maelstrom Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5711 Accepted: 3552 ...
- 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 ...
随机推荐
- cocos随笔
随笔: [ 1 ] active能隐藏元素,且不占空间,但仍存在,索引时,仍能得到 [ 2 ] 碰到参数传不进去的时候可以试着用属性赋值的方法代替传参 [ 3 ] getChildByName是按照 ...
- Django【第26篇】:中介模型以及优化查询以及CBV模式
中介模型以及优化查询以及CBV模式 一.中介模型:多对多添加的时候用到中介模型 自己创建的第三张表就属于是中介模型 class Article(models.Model): ''' 文章表 ''' t ...
- 谷歌将用QUIC传输层技术加速互联网
安全这个话题,要感谢斯诺登,过去的安全就是攻和防之间的关系,即我们用一种什么样的体系.架构和模式去构建一个密不可破的安全系统.” 对IETF工作组忽视外部观察者看起来是一件甚么微不足道的事情的能力感到 ...
- 可决系数R^2和方差膨胀因子VIF
然而很多时候,被筛选的特征在模型上线的预测效果并不理想,究其原因可能是由于特征筛选的偏差. 但还有一个显著的因素,就是选取特征之间之间可能存在高度的多重共线性,导致模型对测试集预测能力不佳. 为了在筛 ...
- Java 8 Date常用工具类
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11983108.html Demo package org.fool.util; import java ...
- php array_flip()函数 语法
php array_flip()函数 语法 作用:用于反转/交换数组中所有的键名以及它们关联的键值.富瑞联华 语法:array_flip(array); 参数: 参数 描述 array 必需.规定需进 ...
- 支持向量机(一)----总述(点到平面的距离,Lagrange函数,Lagrange对偶)
概述:在对支持向量机的学习和使用过程中,遇到了许许多多的问题,通过查阅各种资料,也是逐一攻克了遇到的问题.感悟颇多,写此博文的目的是想以一个学习者的身份从一个刚接触支持向量机的角度去记录模型推到过程中 ...
- 6.25考试整理:江城唱晚&&不老梦&&棠梨煎雪——题解
按照旧例,先安利一下主要作者:一扶苏一 以及扶苏一直挂念的——银临姐姐:银临_百度百科 (滑稽) 好哒,现在步入正题: 先看第一题: 题解: 在NOIP范围内,看到“求方案数”,就说明这个题是一个计数 ...
- [CSP-S模拟测试]:d(贪心+树状数组)
题目传送门(内部题65) 输入格式 第一行,一个自然数$T$,代表数据组数.对于每组数据:第一行,一个正整数$n$,一个自然数$m$.接下来$n$行,每行两个正整数,$a_i,b_i$. 输出格式 对 ...
- 安装完Fedora 18后需要做的事情
折腾了好久,在网上查看了好多资料,总算吧安装好的Fedora 18配置得差不多了,现在将过程记录下来,供以后查看用,同时也许还能帮助到和我遇到同一问题的朋友们,以后再有什么再继续添加吧. 一.添加 y ...