红果果的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. MJRefreshFooterView

    实例化header和footer _header = [MJRefreshHeaderView header]; _header.scrollView = _tableView; 设置header和f ...

  2. Linux fdisk 命令

    Linux fdisk 命令 fdisk                         fdisk功能说明:磁盘分区.语 法:fdisk [-b <分区大小>][-uv][外围设备代号] ...

  3. bzoj 1314: River过河 优先队列

    1314: River过河 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 26  Solved: 10[Submit][Status][Discuss ...

  4. [转载]MongoDB学习 (四):创建、读取、更新、删除(CRUD)快速入门

    本文介绍数据库的4个基本操作:创建.读取.更新和删除(CRUD). 接下来的数据库操作演示,我们使用MongoDB自带简洁但功能强大的JavaScript shell,MongoDB shell是一个 ...

  5. tomcat 端口被占用

    找到占用8080端口的是PID为 2392的进程,于是 ctrl +shift+esc ,然后将这个进程结束掉.

  6. HDU 5015 233 Matrix

    题意:给定一个矩阵的第0列的第1到n个数,第一行第1个数开始每个数分别为233, 2333........,求第n行的第m个数. 分析: 其实也没那么难,自己想了半天还没往对的方向想,m最大1e9,应 ...

  7. dropdownlist无刷新传值

    既然局部刷新,其实没有必要用服务器控件,即便用了服务器控件,也不应该将AutoPostBack="true" ,这将导致页面回发并刷新,因此去掉下拉框的该属性 至于局部改变div的 ...

  8. [jobdu]丑数

    由于思维的惯性,用了queue.后来发现一要注意要用集合判重,二是每次往queue里放的多,后来溢出了,要用long long.但这样要用数组,集合,队列,内存多.效率是O(n*logn)的. #in ...

  9. UL LI 布局 TAB 切换条

    web页面实现tab的功能有几种实现方式,下面是使用UL LI DIV方式实现的tab. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tr ...

  10. VC++下封装ADO类以及使用方法

    操作系统:windows 7软件环境:visual studio 2008 .Microsoft SQL 2005本次目的:介绍一个已经封装的ADO类,简单说明怎么导入使用 首先声明一下,这个封装的A ...