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 ...
随机推荐
- web页面调用app的方法
use_app_goto_page: (skip_type, skip_target) => { // Android App if (/android/i.test(navigator.use ...
- display:line-block
1.那是因为第二个标签是inline-block,它的对齐方式是基线对齐,对齐的是第一个元素里面字的下划线,所以第二个元素的下边缘对齐的是1的下划线,只要在第二个元素里面加内容或者加个空格( )就可以 ...
- rpm安装jdk
rpm安装jdk:(https://blog.csdn.net/daerzei/article/details/80136457) 1.卸载系统自带的JDK rpm -qa|grep java # x ...
- 【leetcode】1095. Find in Mountain Array
题目如下: (This problem is an interactive problem.) You may recall that an array A is a mountain array i ...
- Vue-Awesome-Swiper实现缩略图控制循环,循环背景图,显示多图轮播,点击左右滚动一张图
效果图: 本姐只展示关键代码哈 上代码:网站有完整代码,但是数据不是循环的.https://surmon-china.github.io/vue-awesome-swiper/ 循环数据的代码在此: ...
- CSS盒子模型中的Padding属性
CSS padding 属性 CSS padding 属性定义元素边框与元素内容之间的空白区域,不可见.如果想调整盒子的大小可以调整内容区,内边距,边框. CSS padding 属性定义元素的内边距 ...
- mysql FOREIGN KEY约束 语法
mysql FOREIGN KEY约束 语法 作用:一个表中的 FOREIGN KEY 指向另一个表中的 PRIMARY KEY. DD马达 说明:FOREIGN KEY 约束用于预防破坏表之间连接的 ...
- sh_08_买苹果改进
sh_08_买苹果改进 # 1. 提示用户输入苹果的单价 price = float(input("苹果的单价:")) # 2. 提示用户输入苹果的重量 weight = floa ...
- Solr集群环境搭建
一.准备工作 首先保证已经安装JDK工具包: [root@localhost opt]# java -version java version "1.8.0_144" Java(T ...
- twitter api的使用
1.用手机号注册推特账号 https://twitter.com/ 2.进入网站 https://apps.twitter.com/ 创建第一个app,填入基本信息 name写完会检测是否已经存在像我 ...