最短路径

继续屯代码

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 }

 

迪杰斯塔拉

 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 }

优先队列优化后

 1 #include<cstring>
 2 #include<cstdio>
 3 #include<vector>
 4 #include<functional>
 5 #include<queue>
 6 using namespace std;
 7 const int MAXN=10000;
 8 vector<int> W[MAXN],E[MAXN];
 9 int dis[MAXN],ans=0,n,m,map[MAXN][MAXN];
10 bool vis[MAXN]={0};
11 struct dott{int num,v;
12 };
13 void add(int u,int v,int w1){
14     E[u].push_back(v);
15     W[u].push_back(w1);
16 }
17 
18 struct cmp{
19     bool operator()(dott &a,dott&b){
20         return a.v>b.v;
21     }
22 };
23 
24 dott made(int x,int y){
25     dott mid;
26     mid.num=x;
27     mid.v=y;
28     return mid;
29 }
30 
31 void dij(){
32     priority_queue<dott,vector<dott>,cmp >q;
33     memset(dis,127,sizeof(dis));
34     dis[1]=0;
35     q.push(made(1,0));
36     while(!q.empty()){
37         dott mid=q.top();
38         q.pop();
39         if (mid.v>dis[mid.num]) continue;
40         int minn=mid.num;
41         for (int j=0;j<E[minn].size();j++){
42             if (W[minn][j]+dis[minn]<dis[E[minn][j]]){
43                 dis[E[minn][j]]=W[minn][j]+dis[minn];
44                 q.push(made(E[minn][j],dis[E[minn][j]]));
45             }    
46         }
47     }
48 }
49 
50 
51 int main(){
52     scanf("%d",&n);
53     int u,v,w1;
54     for (int i=1;i<=n;i++){
55         for (int j=1;j<=n;j++) scanf("%d",&map[i][j]);
56     }
57 
58     for (int i=1;i<=n;i++)
59         for (int j=1;j<i;j++) {
60             add(j,i,map[i][j]);
61             add(i,j,map[i][j]);
62         }
63     dij();
64     printf("%d",dis[n]);
65 }

 

 

 

SPFA

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<vector>
 4 using namespace std;
 5 const int MAXN=100;
 6 vector<int> E[MAXN],V[MAXN];
 7 bool vis[MAXN];
 8 int s=1,u,v,w,n,m,dis[MAXN],b[MAXN*2];
 9 void add(int u,int v,int w){
10     E[u].push_back(v);
11     V[u].push_back(w);
12 }
13 
14 void spfa(){
15     int h=0,t=1;
16     memset(dis,127,sizeof(dis));
17     dis[s]=0;
18     b[t]=s;
19     while (h<t){
20         h++;
21         int now=b[h];
22         vis[now]=0;
23         for (int i=0;i<E[now].size();i++){
24             int mid=E[now][i];
25             if (dis[mid]>dis[now]+V[now][i]){
26                 dis[mid]=dis[now]+V[now][i];
27                 if (!vis[mid]){
28                     t++;
29                     b[t]=mid;
30                     vis[t]=1;
31                 }
32             } 
33         }
34     }
35 }
36 
37 int main(){
38     scanf("%d%d",&n,&m);
39     for (int i=0;i<m;i++){
40         scanf("%d%d%d",&u,&v,&w);
41         add(u,v,w);
42         add(v,u,w);
43     }
44     spfa();
45     printf("%d",dis[n]);
46     return 0;
47 }

 

posted @ 2015-12-26 21:57  Alisahhh  阅读(205)  评论(0编辑  收藏  举报