图的应用
学校要建立一个娱乐中心,设计该娱乐中心的位置,使得各部门到中心的距离较近。
基本要求:
(1)顶点表示各部门,顶点之间连线上的权值表示两个部门的距离;
(2)采用邻接表存储图;
(3)采用Flody算法求最短路径;
(4)输出各路径及其距离。
测试数据要求:
输入表示权值的整数必须是正整数。
/*
(1)顶点表示各部门,顶点之间连线上的权值表示两个部门的距离;
(2)采用邻接表存储图;
(3)采用Flody算法求最短路径;
(4)输出各路径及其距离。
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#define N 100
#define inf 0x3f3f3f3f
#define VertexType int
using namespace std;
typedef struct ArcNode{
int adjvex;
int weight;
struct ArcNode *next;
ArcNode(int a=0,int w=0,struct ArcNode *Next=NULL):
adjvex(a),weight(w),next(Next){}
}ArcNode ;
typedef struct VNode{
VertexType data;
ArcNode *first;
}VNode;
typedef struct{
VNode v[N];
int n,m;
}ALGraph;
ALGraph G;
int a[N][N],path[N][N],n,m;
int LocateVex(ALGraph G,VertexType v){
for(int i=0;i<G.n;i++){
if(G.v[i].data==v){
return i;
}
}
}
void CreateGraph(ALGraph &G){
int u,v,i,j,k,w;
scanf("%d%d",&G.n,&G.m);
for(int i=0;i<G.n;i++){
//scanf("%d",&G.v[i].data);
G.v[i].data=i;
G.v[i].first=NULL;
}
for(int k=0;k<G.m;k++){
scanf("%d%d%d",&u,&v,&w);
i=LocateVex(G,u);
if(i==-1){ printf("error\n"); return ;}
j=LocateVex(G,v);
if(j==-1){ printf("error\n"); return ;}
ArcNode * s=new ArcNode(G.v[j].data,w,NULL);
//s->adjvex=G.v[j].data;
//s->weight=w;
//s->next=NULL;
if(!G.v[i].first){
G.v[i].first=s;
}else{
ArcNode * p = G.v[i].first;
while(p->next){
p=p->next;
}
p->next=s;
}
}
for(int i=0;i<=G.n;i++){
for(int j=0;j<=G.n;j++){
a[i][j]=inf;
}
}
for(int i=0;i<G.n;i++){
for(ArcNode *p=G.v[i].first; p;p=p->next){
a[G.v[i].data][p->adjvex]=p->weight;
a[p->adjvex][G.v[i].data]=p->weight;
}
}
for(int i=0;i<=G.n;i++){
for(int j=0;j<=G.n;j++){
printf("(%d,%d)=%d\n",i,j,a[i][j]);
}
}
n=G.n;m=G.m;
}
void input(){
int u,v,w;
scanf("%d%d",&n,&m);
for(int i=0;i<=n;i++){
for(int j=0;j<=n;j++){
a[i][j]=inf;
}
}
for(int i=1;i<=m;i++){
scanf("%d%d%d",&u,&v,&w);
a[u][v]=a[v][u]=w;
}
}
void Flody(){
int d[N][N];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
d[i][j]=a[i][j];
if(i!=j&&a[i][j]<inf) path[i][j]=i;
else path[i][j]=-1;
}
}
for(int k=0;k<n;k++){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(d[i][k]+d[k][j]<d[i][j]){
d[i][j]=d[i][k]+d[k][j];
path[i][j]=path[k][j];
}
}
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i!=j){
printf("起点: %d , 终点: %d\n",i,j);
int p=path[i][0];
if(p==-1){
printf("无路径\n");
}else{
int m=0,b[N]={0};
b[m++]=j;
while(p!=i){
b[m++]=p;p=path[i][p];
}
b[m]=i;
for(int k=m;k>0;k--){
if(k==1&&b[0]==b[k]){
break;
}
printf("%d->",b[k]);
}
printf("%d 长度为: %d\n",b[0],d[i][j]);
}
}
}
}
}
int main()
{
CreateGraph(G);
//input();
Flody();
}
/*
4 5
0 1 4
0 2 1
0 3 4
1 2 2
2 3 2
*/
修正版:
string把地点对应的名字存下来了。
/*
(1)顶点表示各部门,顶点之间连线上的权值表示两个部门的距离;
(2)采用邻接表存储图;
(3)采用Flody算法求最短路径;
(4)输出各路径及其距离。
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#define N 100
#define inf 0x3f3f3f3f
#define VertexType string
using namespace std;
//邻结点
typedef struct ArcNode{
int adjvex;
int weight;
struct ArcNode *next;
ArcNode(int a=0,int w=0,struct ArcNode *Next=NULL):
adjvex(a),weight(w),next(Next){}
}ArcNode ;
//顶点的头节点
typedef struct VNode{
VertexType data;
ArcNode *first;
}VNode;
//邻接表
typedef struct{
VNode v[N];
int n,m;
}ALGraph;
int a[N][N];
string Map[N];
//输入的v查找对应的位置 下标
int LocateVex(ALGraph G,VertexType v){
for(int i=0;i<G.n;i++){
if(G.v[i].data==v){
return i;
}
}
return -1;
}
//用户输入图的信息
void CreateGraph(ALGraph &G){
string u,v;
//int u,v;
int i,j,k,w;
cin>>G.n>>G.m;
//scanf("%d%d",&G.n,&G.m);
for(int i=0;i<G.n;i++){
cin>>G.v[i].data;
Map[i]=G.v[i].data;
//scanf("%d",&G.v[i].data);
//G.v[i].data='0'+i;
G.v[i].first=NULL;
}
for(int k=0;k<G.m;k++){
cin>>u>>v>>w;
//cout<<u<<v<<w<<endl;
//scanf("%d%d%d",&u,&v,&w);
i=LocateVex(G,u);
if(i==-1){ printf("error\n"); return ;}
j=LocateVex(G,v);
if(j==-1){ printf("error\n"); return ;}
ArcNode * s=new ArcNode(j,w,NULL);//G.v[j].data
//s->adjvex=G.v[j].data;
//s->weight=w;
//s->next=NULL;
if(!G.v[i].first){
G.v[i].first=s;
}else{
ArcNode * p = G.v[i].first;
while(p->next){
p=p->next;
}
p->next=s;
}
}
for(int i=0;i<=G.n;i++){
for(int j=0;j<=G.n;j++){
a[i][j]=inf;
}
}
for(int i=0;i<G.n;i++){
for(ArcNode *p=G.v[i].first; p;p=p->next){
a[i][p->adjvex]=p->weight;//G.v[i].data
a[p->adjvex][i]=p->weight;//G.v[i].data
}
}
/*
for(int i=0;i<=G.n;i++){
for(int j=0;j<=G.n;j++){
printf("(%d,%d)=%d\n",i,j,a[i][j]);
}
}
n=G.n;m=G.m;
*/
}
//跑Flody 计算每两点间最短距离,并且记录路径
void Flody(ALGraph &G){
int d[N][N],n=G.n,m=G.m,path[N][N];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++){
d[i][j]=a[i][j];
if(i!=j&&a[i][j]<inf) path[i][j]=i;
else path[i][j]=-1;
}
for(int k=0;k<n;k++)
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(d[i][k]+d[k][j]<d[i][j]){
d[i][j]=d[i][k]+d[k][j];
path[i][j]=path[k][j];
}
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(i!=j){
cout<<"起点:\t"<<Map[i]<<"\t,终点:\t"<<Map[j]<<endl;
//printf("起点: %d , 终点: %d\n",i,j);
int p=path[i][0];
if(p==-1){
printf("无路径\n");
}else{
int m=0,b[N]={0};
b[m++]=j;
while(p!=i){
b[m++]=p;p=path[i][p];
}
b[m]=i;
for(int k=m;k>0;k--){
if(k==1&&b[0]==b[k]){
break;
}
cout<<Map[b[k]]<<" -> ";
//printf("%d->",Map[b[k]]);
}
cout<<Map[b[0]]<<"\t长度为:\t"<<d[i][j]<<endl<<endl;
//printf("%d 长度为: %d\n",Map[b[0]],d[i][j]);
}
}
}
int main()
{
ALGraph G;
CreateGraph(G);
Flody(G);
return 0;
}
/*
//数字输入
4 5
0 1 2 3
0 1 4
0 2 1
0 3 4
1 2 2
2 3 2
//文字输入
4 5
一餐 宿舍 交院 逸夫楼
一餐 宿舍 4
一餐 交院 1
一餐 逸夫楼 4
宿舍 交院 2
交院 逸夫楼 2
*/