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 迪杰斯特拉+题解的更多相关文章

  1. 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 ...

  2. POJ 2502 Subway(迪杰斯特拉)

    Subway Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6692   Accepted: 2177 Descriptio ...

  3. POJ 1502 MPI Maelstrom(最短路)

    MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4017   Accepted: 2412 Des ...

  4. POJ 1502 MPI Maelstrom

    MPI Maelstrom Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) Total ...

  5. POJ 1502 MPI Maelstrom (最短路)

    MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6044   Accepted: 3761 Des ...

  6. POJ - 1502 MPI Maelstrom 路径传输Dij+sscanf(字符串转数字)

    MPI Maelstrom BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odys ...

  7. POJ 1502 MPI Maelstrom [最短路 Dijkstra]

    传送门 MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5711   Accepted: 3552 ...

  8. POJ 1502 MPI Maelstrom (Dijkstra)

    题目链接:http://poj.org/problem?id=1502 题意是给你n个点,然后是以下三角的形式输入i j以及权值,x就不算 #include <iostream> #inc ...

  9. (简单) POJ 1502 MPI Maelstrom,Dijkstra。

    Description BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odysse ...

随机推荐

  1. jquery 对于新插入的节点 的操作绑定(点击事件,each等)

    因为最近项目遇到这个问题,下面给大家带来一篇Jquery对新插入的节点 获取并对这个节点绑定事件失效的解决方法.我觉得挺不错的,大家也可以参考一下: 对于绑定事件来讲:       方法一:使用liv ...

  2. re模块下的的常用方法

    引入模块: import re 1.查找findall   匹配所有,每一项都是列表中的一个元素 ret=re.findall("\d+","sjkhk172按实际花费9 ...

  3. 设计模式Design Pattern(2)--单例模式

    单例顾名思义就是一个实例.类只有唯一一个实例,并提供给全局使用.解决了全局使用的类频繁地创建与销毁带了的消耗. 单例模式常用简单,但细究却又不简单,且往下看. 单例模式又可以分为 (1)懒汉式:需要使 ...

  4. sqlserver数据库脱机和分离的区别

    脱机和分离的区别: 分离和脱机都可以使数据库不能再被使用,但是分离后需要附加才能使用,而脱机后只需联机就可以用了. 附加数据库报错: 无法打开物理文件 XXX.mdf".操作系统错误 5:& ...

  5. sigmoid和softmax的应用意义区别

    转载自:https://baijiahao.baidu.com/s?id=1636737136973859154&wfr=spider&for=pc写的很清楚,并举例佐证,容易理解,推 ...

  6. CSS盒模型面试知识点

    一.基本概念 1.基本概念:标准盒模型+怪异盒模型(IE模型) 基本组成:由margin.padding.content组成. 2.标准盒模型和IE模型的区别 标准盒模型中width指的是内容区域co ...

  7. PHP入门培训教程 PHP变量的使用

      很多朋友在编写PHP程序的时候有时候对变量总有着不能确定的问题,而且也有很多问题就是因为变量的处理不当所造成的.这里兄弟连PHP培训 小编,就PHP变量系统说一下. PHP的变量分为全局变量与局部 ...

  8. Codeforces Round #351(Div 2)

    熬到半夜打结果前一个小时提交代码的页面根本加载不进去,D题写完还因为小点没判被hack掉了... rating什么的都是身外之物了,于是乎喜闻乐见地又掉回绿名了. A题: *题目描述: Bear看一场 ...

  9. 【BZOJ2200】道路和航线(并查集,拓扑排序,最短路)

    题意:n个点,有m1条双向边,m2条单向边,双向边边长非负,单向边可能为负 保证如果有一条从x到y的单项边,则不可能存在从y到x的路径 问从S出发到其他所有点的最短路 n<=25000,n1,m ...

  10. Linux内核源码分析

    Linux源码下载: https://www.kernel.org/ https://git.kernel.org/ Linux内核源码阅读以及工具(转): https://blog.csdn.net ...