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 ...
随机推荐
- 一步步学习NHibernate(5)——多对一,一对多,懒加载(2)
请注明转载地址:http://www.cnblogs.com/arhat 通过上一章的学习,我们建立了Student和Clazz之间的关联属性,并从Student(many)的一方查看了Clazz的信 ...
- pywinauto二次封装(pywinnat.py)
将pywinauto常用方法进行封装,使得pywinauto用起来更简单 #头文件的引入 from pywinauto import application from pywinauto import ...
- iOS多线程常用类说明--备用参考
iOS的多线程,涉及到如下一些类,这里集中做个介绍,免得混淆. 1.NSTimer 很显然,这是定时器类 2.NSTask iOS 不支持 NSTask 在很多并发操作的时候,多线程太耗资源,也太危险 ...
- Codeforces Round #327 div2
Problem_A(591A): 题意: 有一段长度为l的路,两个人分别在两个端点,1, l. 现在已知每个人的速度为p,q. 求第一个人(初始位置在1)在他们第二次相遇的时候的位置. 当他们相遇的时 ...
- SpringMVC的controller方法中注解方式传List参数使用@RequestBody
在SpringMVC控制器方法中使用注解方式传List类型的参数时,要使用@RequestBody注解而不是@RequestParam注解: //创建文件夹 @RequestMapping(value ...
- Spring中argNames的含义
最近学习Spring,一直不太明白Srping的切面编程中的的argNames的含义,经过学习研究后,终于明白,分享一下 先看一个例子: 需要监控的类: package bean; public cl ...
- w3c subscribe
http://www.w3.org/html/ig/zh/ http://blog.csdn.net/spring21st/article/details/6126537 http://www.w3c ...
- 一组神奇的 3D Gif 动图
本文由 极客范 - 黄利民 翻译自 mymodernmet.欢迎加入极客翻译小组,同我们一道翻译与分享.转载请参见文章末尾处的要求. 虽然 gif 动图/动画似乎是无处不在现在了,但有些聪明人已经把 ...
- QTY N.W G.W
QTY(Quantity) - 数量 [英文缩写]QTY[英文全称]Quantity[中文解释]数量 A.毛重:Gross Weight是指商品本身的重量加皮重(tare),也即商品连同包装的重量.有 ...
- PCB使用技巧
1.元器件标号自动产生或已有的元器件标号取消重来Tools 工具|Annotate…注释All Part:为所有元器件产生标号Reset Designators:撤除所有元器件标号2.单面板设置:De ...