POJ1502: MPI Maelstrom
红果果的dijstra算法应用,这里采用邻接表存储图
小插曲:while(scanf("%d",&n))提交时内存超限,改成while(scanf("%d",&n)!=EOF)就AC了,不知道为什么
dijstra算法应用:已知定点为输入,输入图中所有其他点到该定点的最短距离。
具体做法:
a.初始时,S只包含源点,即S={v},v的距离为0。U包含除v外的其他顶点,即:U={其余顶点},若v与U中顶点u有边,则<u,v>正常有权值,若u不是v的出边邻接点,则<u,v>权值为∞。
b.从U中选取一个距离v最小的顶点k,把k,加入S中(该选定的距离就是v到k的最短路径长度)。
c.以k为新考虑的中间点,修改U中各顶点的距离;若从源点v到顶点u的距离(经过顶点k)比原来距离(不经过顶点k)短,则修改顶点u的距离值,修改后的距离值的顶点k的距离加上边上的权。
d.重复步骤b和c直到所有顶点都包含在S中。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int max_size=;
const int MAXINT=;
typedef struct arcNode{
int node;
int len;
arcNode *next;
}arcNode;
typedef struct headNode{
arcNode *firstArc;
}adjList[max_size];
typedef struct cmap{
adjList map;
int nodeNum, arcNum;
}cmap;
int n;
int st,ed;
int dis[max_size];
bool vis[max_size];
void initiate(cmap *c){
for(int i=;i<=c->nodeNum;i++){
c->map[i].firstArc=NULL;
}
}
void addEdge(cmap *c,int from,int to,int len){
if(from==to) return;
arcNode *arc=(arcNode *)malloc(sizeof(arcNode));
arc->node=to;
arc->len=len;
arc->next=c->map[from].firstArc;
c->map[from].firstArc=arc; arcNode *arcc=(arcNode *)malloc(sizeof(arcNode));
arcc->node=from;
arcc->len=len;
arcc->next=c->map[to].firstArc;
c->map[to].firstArc=arcc; }
/*
void print(cmap *c){
for(int i=1;i<=c->nodeNum;i++){
arcNode *p=c->map[i].firstArc;
while(p){
printf("(%d)%d ",p->len,p->node);
p=p->next;
}
printf("\n");
}
}
*/
void dijstra(cmap *c){
int ans=-;
arcNode *p=c->map[st].firstArc;
memset(dis,MAXINT,sizeof(dis));
memset(vis,false,sizeof(vis));
while(p){
dis[p->node]=p->len;
p=p->next;
}
dis[st]=;
vis[st]=true; for(int i=;i<=c->nodeNum-;i++){
int min_dist=;
int u;
for(int j=;j<=c->nodeNum;j++){
if(!vis[j]&&dis[j]<min_dist){
min_dist=dis[j];
u=j;
}
}
vis[u]=true;
arcNode *p=c->map[u].firstArc;
while(p){
int len=p->len;
int temp=p->node;
if(!vis[temp]&&min_dist+len<dis[temp]){
dis[temp]=min_dist+len;
}
p=p->next;
} }
for(int i=;i<=c->nodeNum;i++){
if(dis[i]>ans) ans=dis[i];
}
printf("%d\n",ans);
}
int main(){
while(scanf("%d",&n)!=EOF){
cmap *c=(cmap *)malloc(sizeof(cmap));
c->nodeNum=n; c->arcNum=n*n/;
initiate(c);
st=;
char tmp[];
for(int i=;i<=n-;i++){
for(int j=;j<=i;j++){
scanf("%s",tmp);
if(tmp[]=='x') addEdge(c,i+,j,MAXINT);
else addEdge(c,i+,j,atoi(tmp));
}
}
//print(c);
dijstra(c);
}
return ;
}
POJ1502: MPI Maelstrom的更多相关文章
- POJ-1502 MPI Maelstrom 迪杰斯特拉+题解
POJ-1502 MPI Maelstrom 迪杰斯特拉+题解 题意 题意:信息传输,总共有n个传输机,先要从1号传输机向其余n-1个传输机传输数据,传输需要时间,给出一个严格的下三角(其实就是对角线 ...
- POJ1502 MPI Maelstrom Dijkstra
题意 给出图,从点1出发,求到最后一个点的时间. 思路 单源最短路,没什么好说的.注意读入的时候的技巧. 代码 #include <cstdio> #include <cstring ...
- poj1502 MPI Maelstrom(单源最短路)
题意:表面乍一看output是输出最小值,但仔细研究可以发现,这个最小值是从点1到所有点所花时间的最小值,其实是访问这些节点中的最大值,因为只有访问了最长时间的那个点才算访问了所有点.所以求最短路之后 ...
- MPI Maelstrom(East Central North America 1996)(poj1502)
MPI Maelstrom 总时间限制: 1000ms 内存限制: 65536kB 描述 BIT has recently taken delivery of their new supercom ...
- 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 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(最短路)
MPI Maelstrom Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4017 Accepted: 2412 Des ...
- POJ - 1502 MPI Maelstrom 路径传输Dij+sscanf(字符串转数字)
MPI Maelstrom BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odys ...
随机推荐
- hdu 3746 Cyclic Nacklace KMP循环节
Cyclic Nacklace 题意:给一个长度为Len( 3 <= Len <= 100000 )的英文串,问你在字符串后面最少添加几个字符可以使得添加后的串为周期串? Sample I ...
- Shell面试题
1.用Shell编程,判断一文件是不是块或字符设备文件,如果是将其拷贝到 /dev 目录下. #!/bin/bash#1.sh#判断一文件是不是字符或块设备文件,如果是将其拷贝到 /dev 目录下#f ...
- const用法
一.const作用 二.const用法 1.修饰一般常量 修饰符const可以用在类型说明符前,也可以用在类型说明符后. 例如: ; ; 2.修饰常数组 修饰符const可以用在类型说明符前,也 ...
- js 拼接 三列做为一行
function Ajax_GetCourseAndResource(data) { $(".ol-course-list").empty(); var html = " ...
- [转载]C# HashTable 遍历与排序
private void Form1_Load(object sender, EventArgs e) { Hashtable ht = new Hashtable(); ht.Add("j ...
- 升级mac中的系统之后,给PHP安装扩展常出现问题
(1)在装mcrypt插件时报错,提示:mcrypt fatal error: 'php.h' file not found,然后又仔细操作了一次在输完phpize回车时就已经开始出错了,出错信息如下 ...
- 玩玩EXPRESSJS
呵呵,反正有的是学习时间哈哈, 这个EXPRESSJS,看看实现的大约. 看的是http://www.expressjs.com.cn/ 代码A: var express = require('exp ...
- jar包中的类如何读取包内和包外的配置文件
最近将代码打包成jar包,关于如何处理读取配置文件的问题特此记录一下. out.properties a.jar -com -a.class -in.properties 如上所示,out.prope ...
- UIcollectionView的使用(首页的搭建4)
2.5 头部视图
- 【DataStructure In Python】Python实现各种排序算法
使用Python实现直接插入排序.希尔排序.简单选择排序.冒泡排序.快速排序.归并排序.基数排序. #! /usr/bin/env python # DataStructure Sort # Inse ...