|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#define MAX 1000
|
|
|
|
|
typedef struct node {
|
|
|
|
|
char a[100];
|
|
|
|
|
struct node* next;
|
|
|
|
|
}edgenode;
|
|
|
|
|
typedef struct {
|
|
|
|
|
char name[100];
|
|
|
|
|
edgenode* firstnode;
|
|
|
|
|
}vexnode;
|
|
|
|
|
typedef struct {
|
|
|
|
|
vexnode adjust[7];
|
|
|
|
|
int e, n;
|
|
|
|
|
}list;
|
|
|
|
|
list nm;
|
|
|
|
|
|
|
|
|
|
char position[100]="address.txt";
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
char s[100];
|
|
|
|
|
}filed;
|
|
|
|
|
filed fi[100];
|
|
|
|
|
void createfile(){
|
|
|
|
|
int i;
|
|
|
|
|
FILE* op;
|
|
|
|
|
op=fopen(position,"ab");
|
|
|
|
|
if(op==NULL){
|
|
|
|
|
printf("该文件未找到");
|
|
|
|
|
return ;
|
|
|
|
|
}
|
|
|
|
|
char strs[100];
|
|
|
|
|
for(i=0;i<7;i++){
|
|
|
|
|
printf("请输入:\n");
|
|
|
|
|
scanf("%s",strs);
|
|
|
|
|
fprintf(op,"%s\n",strs);
|
|
|
|
|
}
|
|
|
|
|
fclose(op);
|
|
|
|
|
}
|
|
|
|
|
void creatnode() {//对节点进行初始化
|
|
|
|
|
nm.e = 7;//边数
|
|
|
|
|
nm.n = 7;//站点数
|
|
|
|
|
FILE* op;
|
|
|
|
|
op=fopen(position,"rb");
|
|
|
|
|
if(op==NULL) {
|
|
|
|
|
printf("文件不存在");
|
|
|
|
|
}
|
|
|
|
|
int i=0;
|
|
|
|
|
while(1){
|
|
|
|
|
char strs[100];
|
|
|
|
|
if(fscanf(op,"%s",strs)!=EOF){
|
|
|
|
|
//printf("%s\n",strs);
|
|
|
|
|
strcpy(nm.adjust[i].name,strs);
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
else break;
|
|
|
|
|
}
|
|
|
|
|
fclose(op);
|
|
|
|
|
for (i = 0; i < nm.n; i++) {
|
|
|
|
|
nm.adjust[i].firstnode = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void printff(){
|
|
|
|
|
for(int i=0;i<nm.n;i++){
|
|
|
|
|
printf("%s\n",nm.adjust[i].name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void createlist() {
|
|
|
|
|
struct node *p;
|
|
|
|
|
//下面是肇嘉浜路的邻接表
|
|
|
|
|
p = (edgenode*)malloc(sizeof(edgenode));
|
|
|
|
|
strcpy(p->a, "东安路");
|
|
|
|
|
p->next = nm.adjust[0].firstnode;
|
|
|
|
|
nm.adjust[0].firstnode = p;
|
|
|
|
|
p = (edgenode*)malloc(sizeof(edgenode));
|
|
|
|
|
strcpy(p->a, "嘉善路");
|
|
|
|
|
p->next = nm.adjust[0].firstnode;
|
|
|
|
|
nm.adjust[0].firstnode = p;
|
|
|
|
|
p = (edgenode*)malloc(sizeof(edgenode));
|
|
|
|
|
strcpy(p->a, "徐家汇");
|
|
|
|
|
p->next = nm.adjust[0].firstnode;
|
|
|
|
|
nm.adjust[0].firstnode = p;
|
|
|
|
|
//下面是东安路的邻接表
|
|
|
|
|
p = (edgenode*)malloc(sizeof(edgenode));
|
|
|
|
|
strcpy(p->a, "肇嘉浜路");
|
|
|
|
|
p->next = nm.adjust[1].firstnode;
|
|
|
|
|
nm.adjust[1].firstnode = p;
|
|
|
|
|
p = (edgenode*)malloc(sizeof(edgenode));
|
|
|
|
|
strcpy(p->a, "上海体育场");
|
|
|
|
|
p->next = nm.adjust[1].firstnode;
|
|
|
|
|
nm.adjust[1].firstnode = p;
|
|
|
|
|
p = (edgenode*)malloc(sizeof(edgenode));
|
|
|
|
|
strcpy(p->a, "大木桥路");
|
|
|
|
|
p->next = nm.adjust[1].firstnode;
|
|
|
|
|
nm.adjust[1].firstnode = p;
|
|
|
|
|
//下面是上海体育场的邻接表
|
|
|
|
|
p = (edgenode*)malloc(sizeof(edgenode));
|
|
|
|
|
strcpy(p->a, "东安路");
|
|
|
|
|
p->next = nm.adjust[2].firstnode;
|
|
|
|
|
nm.adjust[2].firstnode = p;
|
|
|
|
|
p = (edgenode*)malloc(sizeof(edgenode));
|
|
|
|
|
strcpy(p->a, "上海体育馆");
|
|
|
|
|
p->next = nm.adjust[2].firstnode;
|
|
|
|
|
nm.adjust[2].firstnode = p;
|
|
|
|
|
//下面是嘉善路的邻接表
|
|
|
|
|
p = (edgenode*)malloc(sizeof(edgenode));
|
|
|
|
|
strcpy(p->a, "大木桥路");
|
|
|
|
|
p->next = nm.adjust[3].firstnode;
|
|
|
|
|
nm.adjust[3].firstnode = p;
|
|
|
|
|
p = (edgenode*)malloc(sizeof(edgenode));
|
|
|
|
|
strcpy(p->a, "肇嘉浜路");
|
|
|
|
|
p->next = nm.adjust[3].firstnode;
|
|
|
|
|
nm.adjust[3].firstnode = p;
|
|
|
|
|
//下面是大木桥路
|
|
|
|
|
p = (edgenode*)malloc(sizeof(edgenode));
|
|
|
|
|
strcpy(p->a, "东安路");
|
|
|
|
|
p->next = nm.adjust[4].firstnode;
|
|
|
|
|
nm.adjust[4].firstnode = p;
|
|
|
|
|
p = (edgenode*)malloc(sizeof(edgenode));
|
|
|
|
|
strcpy(p->a, "嘉善路");
|
|
|
|
|
p->next = nm.adjust[4].firstnode;
|
|
|
|
|
nm.adjust[4].firstnode = p;
|
|
|
|
|
//下面是徐家汇
|
|
|
|
|
p = (edgenode*)malloc(sizeof(edgenode));
|
|
|
|
|
strcpy(p->a, "肇嘉浜路");
|
|
|
|
|
p->next = nm.adjust[5].firstnode;
|
|
|
|
|
nm.adjust[5].firstnode = p;
|
|
|
|
|
p = (edgenode*)malloc(sizeof(edgenode));
|
|
|
|
|
strcpy(p->a, "上海体育馆");
|
|
|
|
|
p->next = nm.adjust[5].firstnode;
|
|
|
|
|
nm.adjust[5].firstnode = p;
|
|
|
|
|
//下面是上海体育馆
|
|
|
|
|
p = (edgenode*)malloc(sizeof(edgenode));
|
|
|
|
|
strcpy(p->a, "徐家汇");
|
|
|
|
|
p->next = nm.adjust[6].firstnode;
|
|
|
|
|
nm.adjust[6].firstnode = p;
|
|
|
|
|
p = (edgenode*)malloc(sizeof(edgenode));
|
|
|
|
|
strcpy(p->a, "上海体育场");
|
|
|
|
|
p->next = nm.adjust[6].firstnode;
|
|
|
|
|
nm.adjust[6].firstnode = p;
|
|
|
|
|
}
|
|
|
|
|
int cont[100][100];//邻接矩阵
|
|
|
|
|
void aaa(){
|
|
|
|
|
int i,j;
|
|
|
|
|
for(i=0;i<nm.n;i++){
|
|
|
|
|
for(j=0;j<nm.n;j++) cont[i][j]=MAX;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void createjz(){//创建邻接矩阵函数
|
|
|
|
|
edgenode *p;
|
|
|
|
|
aaa();
|
|
|
|
|
int i;
|
|
|
|
|
for(i=0;i<nm.n;i++){
|
|
|
|
|
p=nm.adjust[i].firstnode;
|
|
|
|
|
while(p!=NULL){
|
|
|
|
|
int h;
|
|
|
|
|
for(h=0;h<nm.n;h++)
|
|
|
|
|
if(!strcmp(p->a,nm.adjust[h].name)) break;
|
|
|
|
|
cont[i][h]=1;
|
|
|
|
|
p=p->next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void print() {
|
|
|
|
|
edgenode* p;
|
|
|
|
|
int i;
|
|
|
|
|
for(i=0;i<nm.n;i++){
|
|
|
|
|
printf("[%s]",nm.adjust[i].name);
|
|
|
|
|
p=nm.adjust[i].firstnode;
|
|
|
|
|
while(p!=NULL){
|
|
|
|
|
printf("-->[%s]",p->a);
|
|
|
|
|
p=p->next;
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
int num2=0;
|
|
|
|
|
int money=0;
|
|
|
|
|
void Dijkstra(int v1,int v2){
|
|
|
|
|
int dist[MAX],s[MAX],path[MAX];//s判断v1是否已经判断最短路径,path前驱
|
|
|
|
|
int min,i,j,u,pre;
|
|
|
|
|
for(i=0;i<nm.n;i++){
|
|
|
|
|
dist[i]=cont[v1][i];
|
|
|
|
|
s[i]=0;
|
|
|
|
|
if(cont[v1][i]<MAX) path[i]=v1;
|
|
|
|
|
else path[i]=-1;
|
|
|
|
|
}//初始化操作
|
|
|
|
|
s[v1]=1;
|
|
|
|
|
path[v1]=0;
|
|
|
|
|
for(i=0;i<nm.n;i++){
|
|
|
|
|
min=MAX;
|
|
|
|
|
u=-1;
|
|
|
|
|
for(j=0;j<nm.n;j++){
|
|
|
|
|
if(s[j]==0&&dist[j]<min){
|
|
|
|
|
u=j;
|
|
|
|
|
min=dist[j];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(u!=-1){
|
|
|
|
|
s[u]=1;
|
|
|
|
|
for(j=0;j<nm.n;j++)
|
|
|
|
|
if(s[j]==0){
|
|
|
|
|
if(cont[u][j]<MAX&&dist[u]+cont[u][j]<dist[j]){
|
|
|
|
|
dist[j]=dist[u]+cont[u][j];
|
|
|
|
|
path[j]=u;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
printf("智能推荐路线如下(逆序):\n");
|
|
|
|
|
for(i=0;i<nm.n;i++){
|
|
|
|
|
if(i!=v1&&i==v2){
|
|
|
|
|
if(s[i]==1){
|
|
|
|
|
num2=dist[i];
|
|
|
|
|
pre=i;
|
|
|
|
|
while(pre!=v1){
|
|
|
|
|
printf("%s-->",nm.adjust[pre].name);
|
|
|
|
|
money++;
|
|
|
|
|
pre=path[pre];
|
|
|
|
|
}
|
|
|
|
|
printf("%s",nm.adjust[pre].name);
|
|
|
|
|
}
|
|
|
|
|
else printf("路径不存在!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|