首先最简单的存图方式:邻接矩阵
mp[i][j]表示从i->j有一条边权为mp[i][j]的边//如果是无限大的话就是算作没有边
e.g.http://218.5.5.242:9018/JudgeOnline/problem.php?id=1144
这里mp[xx][yy]=(xx和yy的距离)
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #define inf 2000022800.00 using namespace std; int n,m; double x[105],y[105]; double a[105][105]; double mp[105][105]; double MIN(double x,double y) { if(x>y)return y; else return x; } int main() { scanf("%d",&n); // memset(mp,127/3,sizeof(mp)); for(int i=1;i<=n;++i)for(int j=1;j<=n;++j)mp[i][j]=inf; for(int i=1;i<=n;++i) { mp[i][i]=0; scanf("%lf%lf",&x[i],&y[i]); } cin>>m; for(int i=1;i<=m;++i) { int xx,yy; scanf("%d%d",&xx,&yy); mp[xx][yy]=sqrt((x[xx]-x[yy])*(x[xx]-x[yy])+(y[xx]-y[yy])*(y[xx]-y[yy])); } int xx,yy; cin>>xx>>yy; for(int k=1;k<=n;++k) { for(int i=1;i<=n;++i) { for(int j=1;j<=n;++j) { mp[i][j]=MIN(mp[i][j],mp[i][k]+mp[k][j]); } } } printf("%.2lf",mp[xx][yy]); puts(""); return 0; }
时间: 2024-10-12 16:46:04