红果果的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的更多相关文章

  1. POJ-1502 MPI Maelstrom 迪杰斯特拉+题解

    POJ-1502 MPI Maelstrom 迪杰斯特拉+题解 题意 题意:信息传输,总共有n个传输机,先要从1号传输机向其余n-1个传输机传输数据,传输需要时间,给出一个严格的下三角(其实就是对角线 ...

  2. POJ1502 MPI Maelstrom Dijkstra

    题意 给出图,从点1出发,求到最后一个点的时间. 思路 单源最短路,没什么好说的.注意读入的时候的技巧. 代码 #include <cstdio> #include <cstring ...

  3. poj1502 MPI Maelstrom(单源最短路)

    题意:表面乍一看output是输出最小值,但仔细研究可以发现,这个最小值是从点1到所有点所花时间的最小值,其实是访问这些节点中的最大值,因为只有访问了最长时间的那个点才算访问了所有点.所以求最短路之后 ...

  4. MPI Maelstrom(East Central North America 1996)(poj1502)

    MPI Maelstrom 总时间限制:  1000ms 内存限制:  65536kB 描述 BIT has recently taken delivery of their new supercom ...

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

  6. POJ 1502 MPI Maelstrom

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

  7. POJ 1502 MPI Maelstrom (最短路)

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

  8. POJ 1502 MPI Maelstrom(最短路)

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

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

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

随机推荐

  1. 微信开发之开发环境搭建( visual studio 2015we + IIS express + ngrok)

    1. 申请个人测试使用的微信订阅号 https://mp.weixin.qq.com 可注册微信订阅号. 不会?请自行百度. 2. 安装 ngrok 微信开发首先要解决如何让微信链接到本地开发环境.有 ...

  2. sharepoint online

    http://office.microsoft.com/en-001/sharepoint/sharepoint-online-online-collaboration-software-FX1037 ...

  3. JAVA自学之-----FileInputStream类

    1, FileInputStream类函数创建: package coreJava; import java.io.FileInputStream; import java.io.IOExceptio ...

  4. 【win8技巧】win8快速切换后台应用

    今天闲着没事来介绍下win8的使用技巧,不得不说win8把PC带入了Pad时代. 第一招:Win + Tab 在屏幕的最左边就会出现我们想要的后台应用,类似安卓的长按Home的最近任务. 第二招:Al ...

  5. gcc和g++的区别

    参考What is the difference between g++ and gcc? 1.The actual compiler is "cc1" for C and &qu ...

  6. 类型<T> where T:class的用法

    public void Delete<T>(List<T> EntityList) where T : class, new() 就是说T必须是一个类(class)类型,不能是 ...

  7. 过生日,也要学学哈,这次是SHELL的GETOPTS

    今天是WEBSOCKET,, 先完成一个SHELL的GETOPS,周一就用得着. #!/bin/bash echo "usage: ./$0 -t (prism|opscripts)&quo ...

  8. memcached远程 telnet 无法连接,解决方案

    因为默认的Memcached配置,使用了本机ip:127.0.0.1 ,此时利用VI修改下配置 vi /etc/memcached.conf 文件打开后,修改下,把-l前面加入#号注释掉,重启服务器就 ...

  9. easyui源码翻译1.32--Slider(滑动条)

    前言 使用$.fn.slider.defaults重写默认值对象.下载该插件翻译源码 滑动条允许用户从一个有限的范围内选择一个数值.当滑块控件沿着轨道移动的时候,将会显示一个提示来表示当前值.用户可以 ...

  10. 一个CLI的 的例子

    1)这是CLI 调用HTTPOST例子 #using <System.dll> using namespace System;using namespace System::Net;usi ...