能够改变传统数字表示位置的模式用文字来描述城市求最短路
code:
#include#include "string.h"#define MAX 160#define INT_MAX 99999999char s[160][160]; //保存站点 int map[160][160];int Dijkstra(int from,int to,int n) // DIJ + 邻接矩阵 { int dis[MAX]; bool used[MAX]; memset(used,false,sizeof(used)); for(int i=0; i<=n; i++) dis[i] = INT_MAX; dis[from] = 0; used[from] = true; int now = from; for(int i=0; i dis[now] + map[now][k] ) dis[k] = dis[now] + map[now][k]; int min = INT_MAX; for(int k=0; k<=n; k++) if( !used[k] && dis[k] < min ) min = dis[now = k]; used[now] = true; } return dis[to]; }int main(int argc, char *argv[]){ int n; while(scanf("%d",&n)) { if(n==-1) { break; } //-------------map初始化------------- int i2,j2; for(i2=0;i2<160;i2++) { for(j2=0;j2<160;j2++) { map[i2][j2]=INT_MAX; if(i2==j2) { map[i2][j2]==0; } } } //--------------------------- char start[105],end[105]; scanf("%s%s",start,end); strcpy(s[0],start); strcpy(s[1],end); int j=2; int time; int i,k; for(k=0;k time) //确实双向时间 { map[p][q]=time; map[q][p]=time; } } if(strcmp(start,end)==0) { printf("0\n"); }else{ int result=INT_MAX; result=Dijkstra(0,1,j); if(result==INT_MAX ){ printf("-1\n"); }else printf("%d\n",result); } } return 0;}