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. CSP2019 前 随感

    因为博主并没有任何的 oi 水平.文化课水平以及作文水平,下面的东西都是对辞藻和古诗词的堆砌. 不知不觉又到了新一年的 noip 了. 好像是去年的双十一的晚上,noip 考挂的我绝望地写了 bzoj ...

  2. vue 报错 :属性undefined(页面成功渲染)

    vue 报错:Cannot read property 'instrumentId' of undefined" 相关代码如下: <template> ... <span& ...

  3. 【leetcode】1124. Longest Well-Performing Interval

    题目如下: We are given hours, a list of the number of hours worked per day for a given employee. A day i ...

  4. Java各种锁机制简述

    线程安全是多线程领域的问题,线程安全可以简单理解为一个方法或者一个实例可以在多线程环境中使用而不会出现问题. 在 Java 多线程编程当中,提供了多种实现 Java 线程安全的方式: 最简单的方式,使 ...

  5. Xcode 代码注释

    /** * 生成二维码 * * @param data 二维码数据 * @param size 二维码大小 * @param color 二维码颜色 * @param backgroundColor ...

  6. 关了浏览器再开,怎么session还在?

    关了浏览器session当然仍然存在,因为session是储存在服务器端的,而服务器是不可能知道你有没有关掉浏览器. 服务器只是简单的保持session接受用户请求,只有当session一段时间没有被 ...

  7. CF D. Labyrinth 01BFS

    由于上下走不限制,所以按照贪心,我们应该尽可能走上下方向. 我们可以开一个双端队列,并认为每次提取队首的时候得到的是到达该点的最优策略.(这个一定是唯一的,因为不可能向右走几格,然后再退回去. ) 那 ...

  8. @media兼容iphone4、5、6

    在网页中,pixel与point比值称为device-pixel-ratio,普通设备都是1,iPhone 4是2,有些Android机型是1.5. 那么-webkit-min-device-pixe ...

  9. 1.Windows下安装nginx

    1.  到nginx官网http://nginx.org/上下载相应的安装包 下载进行解压,将解压后的文件放到自己心仪的目录下,我的解压文件放在了d盘根目录下,如下图所示:   进入window的cm ...

  10. IDEA 创建spring boot 的Hello World 项目

    1.Open IDEA,choose "New-->Project" 2.Choose "Spring Initializr" 3. Choose jav ...