Dijkstra 单源最短路
- #include<iostream>
- #include<stdlib.h>
- #include<string>
- using namespace std;
- #define OVERFLOW -1
- #define INFINITY 10000
- typedef char VertexType;
- typedef int* PIter;
- typedef string* PathMatrix;
- typedef int* ShortPathTable;
- typedef struct{
- VertexType *vexs;
- int **arcs;
- int vexnum,arcnum;}Graph;
- int LocateVex(Graph G,VertexType v)
- {
- for(int i=0;i<G.vexnum;i++)
- if(G.vexs[i]==v) return i;
- return G.vexnum;
- }
- void CreateGraph(Graph &G)
- {
- cin>>G.vexnum>>G.arcnum;
- G.vexs=new VertexType[G.vexnum];
- G.arcs=new PIter[G.vexnum];
- if(!G.vexs||!G.arcs) exit(OVERFLOW);
- int i,j,k;
- for(i=0;i<G.vexnum;i++)
- {
- if(!(G.arcs[i]=new int[G.vexnum])) exit(OVERFLOW);
- for(j=0;j<G.vexnum;j++)
- if(i!=j) G.arcs[i][j]=INFINITY;
- else G.arcs[i][j]=0;
- }
- VertexType v1,v2;
- for(i=0;i<G.vexnum;i++)
- cin>>G.vexs[i];
- for(k=0;k<G.arcnum;k++)
- {
- do{
- cin>>v1>>v2;
- i=LocateVex(G,v1);
- j=LocateVex(G,v2);
- cin>>G.arcs[i][j];
- }while(i==G.vexnum||j==G.vexnum);
- }
- }
- void OutputPath(Graph G,int v0,PathMatrix P,int *cost)
- {
- for(int i=0;i<G.vexnum;i++)
- {
- if(v0!=i)
- {
- cout<<G.vexs[v0]<<"->"<<G.vexs[i]<<":";
- if(!P[i].empty())
- cout<<G.vexs[v0]<<P[i]<<" "<<cost[i]<<endl;
- else cout<<"NO PATH!!"<<endl;
- }
- }
- }
- void ShortestPath_DIJ(Graph G,int v0,PathMatrix &P,ShortPathTable &D,int *cost)
- {
- bool *final=new bool[G.vexnum];
- D=new int[G.vexnum];
- P=new string[G.vexnum];
- cost=new int[G.vexnum];
- if(!final||!D||!P||!cost) exit(OVERFLOW);
- int v,w,i,min;
- for(v=0;v<G.vexnum;v++)
- {
- final[v]=false,D[v]=G.arcs[v0][v];
- if(D[v]<INFINITY) P[v]=G.vexs[v],cost[v]=D[v];
- else cost[v]=INFINITY;
- }
- D[v0]=0,final[v0]=true;
- for(i=1;i<G.vexnum;i++)
- {
- min=INFINITY;
- for(w=0;w<G.vexnum;w++)
- if(!final[w])
- if(D[w]<min) v=w,min=D[w];
- final[v]=true;
- for(w=0;w<G.vexnum;w++)
- if(!final[w]&&(min+G.arcs[v][w]<D[w]))
- {
- D[w]=min+G.arcs[v][w];
- P[w]=P[v]+G.vexs[w];
- cost[w]=D[w];
- }
- }
- OutputPath(G,v0,P,cost); delete[] P;delete[] D;delete cost;
- }
- int main()
- {
- Graph G;
- PathMatrix P;
- ShortPathTable D;
- int *cost;
- CreateGraph(G);
- ShortestPath_DIJ(G,0,P,D,cost);
- system("pause");
- return 0;
- }
- 输入
- 6 8
- a b c d e f
- a c 10
- a e 30
- a f 100
- b c 5
- c d 50
- d f 10
- e d 20
- e f 60
- 输出
- a->b:NO PATH!!
- a->c:ac 10
- a->d:aed 50
- a->e:ae 30
- a->f:aedf 60
- 请按任意键继续. . .
Floyd
- #include<iostream>
- #include<stdlib.h>
- #include<string>
- using namespace std;
- #define OVERFLOW -1
- #define INFINITY 10000
- typedef char VertexType;
- typedef int* PIter;
- typedef string* PStr;
- typedef string** PathMatrix;
- typedef int** DistancMatrix;
- typedef struct{
- VertexType *vexs;
- int **arcs;
- int vexnum,arcnum;}Graph;
- int LocateVex(Graph G,VertexType v)
- {
- for(int i=0;i<G.vexnum;i++)
- if(G.vexs[i]==v) return i;
- return G.vexnum;
- }
- void CreateGraph(Graph &G)
- {
- cin>>G.vexnum>>G.arcnum;
- G.vexs=new VertexType[G.vexnum];
- G.arcs=new PIter[G.vexnum];
- if(!G.vexs||!G.arcs) exit(OVERFLOW);
- int i,j,k;
- for(i=0;i<G.vexnum;i++)
- {
- if(!(G.arcs[i]=new int[G.vexnum])) exit(OVERFLOW);
- for(j=0;j<G.vexnum;j++)
- if(i!=j) G.arcs[i][j]=INFINITY;
- else G.arcs[i][j]=0;
- }
- VertexType v1,v2;
- for(i=0;i<G.vexnum;i++)
- cin>>G.vexs[i];
- for(k=0;k<G.arcnum;k++)
- {
- do{
- cin>>v1>>v2;
- i=LocateVex(G,v1);
- j=LocateVex(G,v2);
- cin>>G.arcs[i][j];
- }while(i==G.vexnum||j==G.vexnum);
- }
- }
- void OutputPath(Graph G,PathMatrix P,DistancMatrix D)
- {
- for(int i=0;i<G.vexnum;i++)
- for(int j=0;j<G.vexnum;j++)
- {
- if(j!=i)
- {
- cout<<G.vexs[i]<<"->"<<G.vexs[j]<<":";
- if(!P[i][j].empty())
- cout<<G.vexs[i]<<P[i][j]<<" "<<D[i][j]<<endl;
- else cout<<"NO PATH!!"<<endl;
- }
- }
- }
- void ShortestPath_FLOYD(Graph G,PathMatrix &P,DistancMatrix &D)
- {
- P=new PStr[G.vexnum];
- D=new PIter[G.vexnum];
- int v,w,u;
- for(v=0;v<G.vexnum;v++)
- {
- P[v]=new string[G.vexnum];
- D[v]=new int[G.vexnum];
- for(w=0;w<G.vexnum;w++)
- {
- D[v][w]=G.arcs[v][w];
- if(D[v][w]<INFINITY) P[v][w]=G.vexs[w];
- }
- }
- for(u=0;u<G.vexnum;u++)
- for(v=0;v<G.vexnum;v++)
- for(w=0;w<G.vexnum;w++)
- if(D[v][u]+D[u][w]<D[v][w])
- {
- D[v][w]=D[v][u]+D[u][w];
- P[v][w]=P[v][u]+P[u][w];
- }
- OutputPath(G,P,D); delete[] P;delete[] D;
- }
- int main()
- {
- Graph G;
- CreateGraph(G);
- PathMatrix P;
- DistancMatrix D;
- ShortestPath_FLOYD(G,P,D);
- return 0;}
- 输入
- 3 5
- a b c
- a b 4
- a c 11
- b a 6
- b c 2
- c a 3
- 输出
- a->b:ab 4
- a->c:abc 6
- b->a:bca 5
- b->c:bc 2
- c->a:ca 3
- c->b:cab 7
- 请按任意键继续. . .
6万+

被折叠的 条评论
为什么被折叠?



