标签:
继续屯代码
floyed
1 #include<cstdio> 2 #include<cmath> 3 #include<cstring> 4 double map[101][101],x[101],y[101]; 5 int n,m,u,v; 6 double dis(int u,int v){ 7 return (sqrt((x[u]-x[v])*(x[u]-x[v])+(y[u]-y[v])*(y[u]-y[v]))); 8 } 9 10 11 int main(){ 12 scanf("%d",&n); 13 for (int i=1;i<=n;i++) scanf("%lf%lf",&x[i],&y[i]); 14 scanf("%d",&m); 15 for (int i=0;i<=n;i++) for (int j=0;j<=n;j++) map[i][j]=100000; 16 for (int i=0;i<m;i++){ 17 scanf("%d%d",&u,&v); 18 map[u][v]=dis(u,v); 19 map[v][u]=map[u][v]; 20 } 21 for (int k=1;k<=n;k++) 22 for (int i=1;i<=n;i++) 23 for (int j=1;j<=n;j++) if (map[i][k]+map[k][j]<map[i][j]) map[i][j]=map[i][k]+map[k][j]; 24 scanf("%d%d",&u,&v); 25 printf("%.2lf",map[u][v]); 26 }
dij
1 #include<cstring> 2 #include<cstdio> 3 #include<vector> 4 using namespace std; 5 const int MAXN=10000; 6 vector<int> W[MAXN],E[MAXN]; 7 int dis[MAXN],ans=0,n,m,map[MAXN][MAXN]; 8 bool vis[MAXN]={0}; 9 void add(int u,int v,int w1){ 10 E[u].push_back(v); 11 W[u].push_back(w1); 12 } 13 14 void dij(){ 15 memset(dis,127,sizeof(dis)); 16 dis[1]=0; 17 for (int i=0;i<n;i++){ 18 int min=100000,minn; 19 for (int j=1;j<=n;j++){ 20 if (dis[j]<min&&!vis[j]){ 21 min=dis[j]; 22 minn=j; 23 } 24 } 25 vis[minn]=1; 26 ans+=min; 27 for (int j=0;j<E[minn].size();j++){ 28 if (W[minn][j]+dis[minn]<dis[E[minn][j]]){ 29 dis[E[minn][j]]=W[minn][j]+dis[minn]; 30 } 31 } 32 } 33 } 34 35 36 int main(){ 37 scanf("%d",&n); 38 int u,v,w1; 39 for (int i=1;i<=n;i++){ 40 for (int j=1;j<=n;j++) scanf("%d",&map[i][j]); 41 } 42 43 for (int i=1;i<=n;i++) 44 for (int j=1;j<i;j++) { 45 add(j,i,map[i][j]); 46 add(i,j,map[i][j]); 47 } 48 dij(); 49 printf("%d",dis[n]); 50 }
标签:
原文地址:http://www.cnblogs.com/wuminyan/p/5079146.html