标签:add while col div too cst push floyd算法 cstring
Floyd算法:【佛楼医德】
// luogu-judger-enable-o2 #include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> using namespace std; const int N=205; int f[N][N]; int n; int main(){ //freopen("1359.in","r",stdin); //freopen("1359.out","w",stdout); scanf("%d",&n); for (int i=1; i<=n; i++){ for (int j=i+1; j<=n; j++){ scanf("%d",&f[i][j]); f[j][i]=2133333333; } } for(int k=1;k<=n;k++) for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) f[i][j]=min(f[i][j],f[i][k]+f[k][j]); printf("%d\n",f[1][n]); return 0; }
dijkstra算法:【DJ撕他】
#include<cstdio> #include<iostream> #include<cmath> #include<cstdlib> #include<cstring> #include<algorithm> typedef long long ll; using namespace std; const int oo=2147483647; int n,m,dis[5005],vis[5005],a[5005][5005]; int main(){ freopen("dijs.in","r",stdin); freopen("dijs.out","w",stdout); scanf("%d %d",&n,&m); int x,y,z; memset(a,-1,sizeof(a)); for(int i=1;i<=m;i++){ cin>>x>>y>>z; a[x][y]=z; a[y][x]=z; } for(int i=1;i<=n;i++){ dis[i]=oo; vis[i]=0; } dis[1]=0; for(int i=1;i<=n;i++){ int now=oo,ii; for(int j=1;j<=n;j++) if(vis[j]==0 && dis[j]<now){ ii=j; now=dis[j]; } vis[ii]=1; for(int j=1;j<=n;j++) if(a[ii][j]!=-1 && vis[j]==0 &&dis[ii]+a[ii][j]<dis[j]) dis[j]=dis[ii]+a[ii][j]; } for(int i=1;i<=n;i++) cout<<dis[i]<<endl; return 0; }
Bellman_Ford算法【贝尔曼福特】
#include<cstdio> #include<iostream> #include<cmath> #include<cstdlib> #include<cstring> #include<algorithm> typedef long long ll; using namespace std; const int oo=214748364; int n,m,dis[5005],vis[5005]; struct node{ int x,y,z; }a[10004]; int main(){ freopen("bell.in","r",stdin); freopen("bell.out","w",stdout); scanf("%d %d",&n,&m); //int x,y,z; //memset(a,-1,sizeof(a)); for(int i=1;i<=m;i++){ cin>>a[i].x>>a[i].y>>a[i].z; /*a[m+i].x=a[i].y; a[m+i].y=a[i].x; a[m+i].z=a[i].z;*/ } for(int i=1;i<=n;i++){ dis[i]=oo; // vis[i]=0; } dis[1]=0; int ans=0; for(int i=1;i<=n;i++){ int flag=0; for(int j=1;j<=m;j++){ int xx=a[j].x; int yy=a[j].y; int zz=a[j].z; if(dis[xx]+zz<dis[yy]){ dis[yy]=dis[xx]+zz; flag=1; } } if(flag==0){ ans=2; break; } } if(ans==0) cout<<-1;//判断负环 else{ for(int i=1;i<=n;i++) cout<<dis[i]<<endl; } return 0; }
SPFA算法
#include<cstdio> #include<iostream> #include<cmath> #include<cstdlib> #include<cstring> #include<algorithm> #include<queue> typedef long long ll; using namespace std; const int oo=21474836; int n,m,k,cnt,x,y,dis[10005],vis[10005]; struct node{ int to; int val; int next; }e[10005]; int head[10004]; void add(int a,int b,int c){ cnt++; e[cnt].to=b; e[cnt].val=c; e[cnt].next=head[a]; head[a]=cnt; } queue<int>q; int main(){ freopen("spfa.in","r",stdin); freopen("spfa.out","w",stdout); scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&k); for(int j=1;j<=k;j++){ scanf("%d %d",&x,&y); add(i,x,y); } } for(int i=1;i<=n;i++) dis[i]=oo; q.push(1); dis[1]=0; vis[1]=1; while(!q.empty()){ x=q.front(); q.pop(); vis[x]=0; for(int i=head[x];i!=0;i=e[i].next){ int too=e[i].to; if(dis[too]>dis[x]+e[i].val) { dis[too]=dis[x]+e[i].val; if(vis[too]==0){ vis[too]=1; q.push(too); } } } } for(int i=1;i<=n;i++) cout<<dis[i]<<endl; return 0; }
标签:add while col div too cst push floyd算法 cstring
原文地址:https://www.cnblogs.com/wuhu-JJJ/p/11133459.html