C++-POJ1502-MPI Maelstrom-[最短路][spfa][栈优化]

我不理解为什么写dijkska就WA呢?

atoi()是个好东西,给你个颜色,自己体会

疑惑!疑惑!疑惑!

 1 #include <queue>
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5 const int INF=1e9,MAXN=105,MAXM=10010;
 6 int w(){char s[15];scanf("%s",s);return s[0]==‘x‘?INF:atoi(s);}
 7 struct edge{
 8     int v,w,next;
 9     edge(int v=0,int w=0,int next=0):v(v),w(w),next(next){}
10 };
11 edge E[MAXM];int head[MAXN],cnt;
12 void add(int u,int v,int w){
13     E[++cnt]=edge(v,w,head[u]),head[u]=cnt;
14     E[++cnt]=edge(u,w,head[v]),head[v]=cnt;
15 }
16 int dis[MAXN],vis[MAXN],stack[MAXN],top;
17 int spfa(int s,int n){
18     for(int i=1;i<=n;i++)vis[i]=0,dis[i]=INF;
19     top=0,stack[++top]=1,vis[s]=1,dis[s]=0;
20     for(int u;top>=0;){
21         u=stack[top--],vis[u]=0;
22         for(int i=head[u],v,w;v=E[i].v,w=E[i].w,i;i=E[i].next)
23             if(dis[v]>dis[u]+w){
24                 dis[v]=dis[u]+w;
25                 if(!vis[v])stack[++top]=v,vis[v]=1;
26             }
27     }
28     int ans=0;
29     for(int i=1;i<=n;i++)ans=max(ans,dis[i]);
30     return ans;
31 }
32 int main(){
33     int n;scanf("%d",&n);
34     for(int i=2;i<=n;i++)for(int j=1;j<i;j++)add(i,j,w());
35     printf("%d\n",spfa(1,n));
36     return 0;
37 } 

原文地址:https://www.cnblogs.com/JasonCow/p/12369492.html

时间: 2024-11-07 21:11:04

C++-POJ1502-MPI Maelstrom-[最短路][spfa][栈优化]的相关文章

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 1502 MPI Maelstrom [最短路 Dijkstra]

传送门 MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5711   Accepted: 3552 Description BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierar

poj1502 MPI Maelstrom(单源最短路)

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

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(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

红果果的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>正常有权值,若

最短路--spfa+队列优化模板

spfa普通版就不写了,优化还是要的昂,spfa是可以判负环,接受负权边和重边的,判断负环只需要另开一个数组记录每个结点的入队次数,当有任意一个结点入队大于点数就表明有负环存在 1 #include<stdio.h> //spfa基本上要这些头文件 2 #include<string.h> 3 #include<queue> 4 using namespace std; 5 6 void spfa(int s,int p){ 7 memset(vis,0,sizeof(

poj 1502 MPI Maelstrom(最短路)

poj 1502 MPI Maelstrom 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 Swige