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中。

                     

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<algorithm>
  4 #include<cstring>
  5 #include<cmath>
  6 using namespace std;
  7 const int max_size=101;
  8 const int MAXINT=9999999;
  9 typedef struct arcNode{
 10     int node;
 11     int len;
 12     arcNode *next;
 13 }arcNode;
 14 typedef struct headNode{
 15     arcNode *firstArc;
 16 }adjList[max_size];
 17 typedef struct cmap{
 18     adjList map;
 19     int nodeNum, arcNum;
 20 }cmap;
 21 int n;
 22 int st,ed;
 23 int dis[max_size];
 24 bool vis[max_size];
 25 void initiate(cmap *c){
 26     for(int i=1;i<=c->nodeNum;i++){
 27         c->map[i].firstArc=NULL;
 28     }
 29 }
 30 void addEdge(cmap *c,int from,int to,int len){
 31     if(from==to) return;
 32     arcNode *arc=(arcNode *)malloc(sizeof(arcNode));
 33     arc->node=to;
 34     arc->len=len;
 35     arc->next=c->map[from].firstArc;
 36     c->map[from].firstArc=arc;
 37
 38     arcNode *arcc=(arcNode *)malloc(sizeof(arcNode));
 39     arcc->node=from;
 40     arcc->len=len;
 41     arcc->next=c->map[to].firstArc;
 42     c->map[to].firstArc=arcc;
 43
 44 }
 45 /*
 46 void print(cmap *c){
 47     for(int i=1;i<=c->nodeNum;i++){
 48         arcNode *p=c->map[i].firstArc;
 49         while(p){
 50             printf("(%d)%d ",p->len,p->node);
 51             p=p->next;
 52         }
 53         printf("\n");
 54     }
 55 }
 56 */
 57 void dijstra(cmap *c){
 58     int ans=-1;
 59     arcNode *p=c->map[st].firstArc;
 60     memset(dis,MAXINT,sizeof(dis));
 61     memset(vis,false,sizeof(vis));
 62     while(p){
 63         dis[p->node]=p->len;
 64         p=p->next;
 65     }
 66     dis[st]=0;
 67     vis[st]=true;
 68
 69     for(int i=1;i<=c->nodeNum-1;i++){
 70         int min_dist=9999999;
 71         int u;
 72         for(int j=1;j<=c->nodeNum;j++){
 73             if(!vis[j]&&dis[j]<min_dist){
 74                 min_dist=dis[j];
 75                 u=j;
 76             }
 77         }
 78         vis[u]=true;
 79         arcNode *p=c->map[u].firstArc;
 80         while(p){
 81             int len=p->len;
 82             int temp=p->node;
 83             if(!vis[temp]&&min_dist+len<dis[temp]){
 84                 dis[temp]=min_dist+len;
 85             }
 86             p=p->next;
 87         }
 88
 89     }
 90     for(int i=2;i<=c->nodeNum;i++){
 91         if(dis[i]>ans) ans=dis[i];
 92     }
 93     printf("%d\n",ans);
 94 }
 95 int main(){
 96     while(scanf("%d",&n)!=EOF){
 97         cmap *c=(cmap *)malloc(sizeof(cmap));
 98         c->nodeNum=n; c->arcNum=n*n/2;
 99         initiate(c);
100         st=1;
101         char tmp[20];
102         for(int i=1;i<=n-1;i++){
103             for(int j=1;j<=i;j++){
104                 scanf("%s",tmp);
105                 if(tmp[0]==‘x‘) addEdge(c,i+1,j,MAXINT);
106                 else addEdge(c,i+1,j,atoi(tmp));
107             }
108         }
109         //print(c);
110         dijstra(c);
111     }
112     return 0;
113 }

时间: 2024-10-07 23:18:30

POJ1502: MPI Maelstrom的相关文章

poj1502——MPI Maelstrom(dijkstra算法)

Description BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchical communication subsystem. Valentine McKee's research advisor, Jack Swigert, has asked her to be

POJ1502 MPI Maelstrom Dijkstra

题意 给出图,从点1出发,求到最后一个点的时间. 思路 单源最短路,没什么好说的.注意读入的时候的技巧. 代码 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int INF = 1000000000; const int maxn = 110; int n; int edge[maxn][maxn

poj1502 MPI Maelstrom,单源最短路的最长距离,dijkstra + 优先队列

点击打开链接 求顶点1到其他点的最短距离的最长距离.. 测试..dijkstra + 优先队列 #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <set> #include <map> #include <string> #include <sta

poj1502 MPI Maelstrom(单源最短路)

题意:表面乍一看output是输出最小值,但仔细研究可以发现,这个最小值是从点1到所有点所花时间的最小值,其实是访问这些节点中的最大值,因为只有访问了最长时间的那个点才算访问了所有点.所以求最短路之后求最大值. #include <iostream> #include <cstring> #include <cstdio> using namespace std; const int maxn = 100 + 5; const int inf = 0x3f3f3f3f;

MPI Maelstrom(poj1502)(迪杰斯特拉+atoi函数)

MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5637   Accepted: 3513 Description BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchic

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

MPI Maelstrom 总时间限制:  1000ms 内存限制:  65536kB 描述 BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchical communication subsystem. Valentine McKee's research advisor

poj 1502 MPI Maelstrom Dijkstra算法的简单运用 ,呵呵,,我估计有很多人都没看懂什么意思,我也看了很久

MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5831   Accepted: 3621 Description BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchic

POJ 1502 MPI Maelstrom (最短路)

MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6329   Accepted: 3925 Description BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchic

POJ 1052 MPI Maelstrom

MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5547   Accepted: 3458 Description BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchic