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 ...
随机推荐
- 【原创】一起学C++ 之enum ---------C++ primer plus(第6版)
枚举 定义:在默认情况下讲整数值赋给枚举量,第一个枚举量的值为0,第二个枚举量的值为1,依次+1 一.定义一个枚举,枚举类型,枚举量 *与C#相比个人认为C++的enum不好一点是不能通过枚举名点其中 ...
- hdu 2243 考研路茫茫——单词情结 ac自动机+矩阵快速幂
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2243 题意:给定N(1<= N < 6)个长度不超过5的词根,问长度不超过L(L <23 ...
- websphere6.1安装与配置
http://www.blogjava.net/103335460/articles/220935.html 一.本人开发环境: jdk1.5.0_11 , eclipse3.2 , MyEclips ...
- 10 Best Responsive HTML5 Frameworks and Tools
http://designinstruct.com/roundups/html5-frameworks/
- 常用的四种CSS样式表格
1. 单像素边框CSS表格 这是一个很常用的表格样式. [html] <style type="text/css"> table.gridtable { font-fa ...
- C# and JSON
JQuery Parse JSON var obj = $.parseJSON(data); C# creates JSON string: public static class JSONHelpe ...
- Java之向左添加零(000001)
int i_m = 270000 ; String str_m = String.valueOf(i_m); String str ="000000"; str_m=str.sub ...
- 导出Excel文件
/// <summary> /// 类说明:Assistant /// 更新网站:[url=http://www.sufeinet.com/thread-655-1-1.html]http ...
- XSS之学习误区分析
有段时间没写东西了, 最近看到zone里出现了很多“XSS怎么绕过某某符号的帖子”,觉得很多新手在寻找XSS时走进了一些误区,比如:专门想着怎么去“绕过”.这里做个总结,希望对大家有所帮助. 1. 误 ...
- android activity在横竖屏切换的时候不重新调用onCreate方法
在安卓系统中,横竖屏切换会默认重新调用onCreate等生命周期方法,如果此时有一些临时数据没有保存下来,很有可能会导致该数据丢失. 因此我们可以进行以下设置,来避免恒切换时重新调用onCreate方 ...