标签:style blog http color os for
http://acm.hdu.edu.cn/showproblem.php?pid=1874
SPFA
#include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #define N 1000001 using namespace std; int n,m; int v[202],dis[202]; struct node { int x,y,z,next; }edge[20002]; int t; int q[20002]; int ss,ee; int head[202]; void init() { memset(head,-1,sizeof(head)); t=0; } void add(int x,int y,int z) { edge[t].x=x; edge[t].y=y; edge[t].z=z; edge[t].next=head[x]; head[x]=t++; } void SPFA() { memset(v,0,sizeof(v)); for(int i=0;i<n;i++) dis[i]=N; dis[ss]=0; v[ss]=1; int tt; int s=0,e=0; q[e++]=ss; while(s<e) { tt=q[s++]; v[tt]=0; for(int i=head[tt];i!=-1;i=edge[i].next) { if(dis[tt]+edge[i].z<dis[edge[i].y]) { dis[edge[i].y]=dis[tt]+edge[i].z; if(v[edge[i].y]==0) { if(dis[edge[i].y]>dis[q[s]]) q[e++]=edge[i].y; else q[--s]=edge[i].y; v[edge[i].y]=1; } } } } if(dis[ee]==N) printf("-1\n"); else printf("%d\n",dis[ee]); } int main() { int x,y,z; while(scanf("%d%d",&n,&m)!=EOF) { init(); while(m--) { scanf("%d%d%d",&x,&y,&z); add(x,y,z); add(y,x,z); } scanf("%d%d",&ss,&ee); SPFA(); } return 0; }
Floy
#include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #define N 1000001 using namespace std; int n,m; int ss,ee; int map[202][202]; void F() { for(int k=0;k<n;k++) { for(int j=0;j<n;j++) { for(int i=0;i<n;i++) { if(map[j][k]+map[k][i]<map[j][i]) map[j][i]=map[j][k]+map[k][i]; } } } if(map[ss][ee]==N) printf("-1\n"); else printf("%d\n",map[ss][ee]); } int main() { int x,y,z; while(scanf("%d%d",&n,&m)!=EOF) { for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { map[i][j]=N; map[j][i]=N; } map[i][i]=0; } while(m--) { scanf("%d%d%d",&x,&y,&z); if(map[x][y]>z) { map[x][y]=z; map[y][x]=z; } } scanf("%d%d",&ss,&ee); F(); } return 0; }
畅通工程续 (SPFA模板Floy模板),布布扣,bubuko.com
标签:style blog http color os for
原文地址:http://www.cnblogs.com/zhangmingcheng/p/3812598.html