Driving route calculation fatigue value shortest path

Insert picture description here
Insert picture description here

#include <iostream>
#include <windows.h>
#include<bits/stdc++.h>
using namespace std;
typedef struct{
    
    
int vexs[100];
int arcs[100][100];
int type[100][100];
int vnum;
int arcnum;
}MGraph;
int path[100];
int d[100];
void goxy(int x,int y)
{
    
    

HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos = {
    
     0 };
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(handle, pos);
}
void setcolor(unsigned short ForeColor,unsigned short BackGroundColor)
{
    
    
HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle,ForeColor+BackGroundColor*0x10);
}
void caidan()
{
    
    

    goxy(30,10);
    printf("************************************");
     goxy(30,12);
    printf("欢迎进入求出最小疲劳值的小程序");
    goxy(30,14);
    printf("************************************");
    goxy(30,16);
    printf("请选择");
    goxy(30,18);
    printf("1.获取疲劳值");
     goxy(30,20);
    printf("2.退出");
    goxy(30,22);
}
void  creatAdjmatrix(MGraph &g){
    
    
int n,m,i,j;
cout<<"请输入两个整数分别代表路口数量和道路数量N,M"<<endl;
cin>>n>>m;
g.vnum=n;
g.arcnum=m;
for(i=1;i<=n;i++){
    
    
    g.vexs[i]=i;
     for(j=1;j<=n;j++){
    
    
    g.arcs[i][j]=1000;
    g.type[i][j]=999;
}
}
cout<<"请输入一个N*M的矩阵"<<endl;
for(int k=1;k<=g.arcnum;k++){
    
    
    cin>>m>>i>>j>>n;
    g.arcs[i][j]=n;
    g.type[i][j]=m;
}

}

void ShortestPath_DIJ(MGraph g,int v){
    
    
   bool s[100];
   for(int i=1;i<=g.vnum;i++){
    
    
    s[i]=false;
    if(g.type[v][i]!=1)
    d[i]=g.arcs[v][i];
   else if(g.type[v][i]==1)
    d[i]=g.arcs[v][i]*g.arcs[v][i];

    if(d[i]<1000&&g.type[v][i]==1) path[i]=g.arcs[v][i];
   else if(d[i]<1000&&g.type[v][i]==0) path[i]=0;
    if(d[i]==1000) path[i]=-1;
   }
   s[v]=true;
   d[v]=0;
   for(int i=1;i<g.vnum;i++){
    
    
        int min=1000;
        int ve=1;
   for(int j=1;j<=g.vnum;j++){
    
    

    if(!s[j]&&d[j]<min){
    
    
        ve=j;min=d[j];
    }

   }

   s[ve]=true;

   for(int j=1;j<=g.vnum;j++){
    
    
       if(g.type[ve][j]==0){
    
    
           if(!s[j]&&d[ve]+g.arcs[ve][j]<d[j]){
    
    
        d[j]=d[ve]+g.arcs[ve][j];
        path[j]=0;
        }
       }else if(g.type[ve][j]==1){
    
    
           if(path[ve]==0){
    
    
            if(!s[j]&&d[ve]+g.arcs[ve][j]*g.arcs[ve][j]<d[j]){
    
    
            d[j]=d[ve]+g.arcs[ve][j]*g.arcs[ve][j];
            path[j]=g.arcs[ve][j];
                                               }
           }else {
    
    
                if(!s[j]&&(path[ve]+g.arcs[ve][j])*(path[ve]+g.arcs[ve][j])<d[j]){
    
    
            d[j]=(path[ve]+g.arcs[ve][j])*(path[ve]+g.arcs[ve][j]);
            path[j]=path[ve]+g.arcs[ve][j];
                                               }

           }

       }


   }

   }
}
int main()
{
    
    
    MGraph g;

    setcolor(7,2);
    system("cls");
    int i;
    t:caidan();
    scanf("%d",&i);
    switch (i){
    
    
    case 1:system("cls");creatAdjmatrix(g);ShortestPath_DIJ(g,1);cout<<"最小疲劳值为:"<<d[g.vnum];system("pause");system("cls");goto t; break;
    case 2:exit(1);break;
    default:system("cls");goto t;
    }


    //ShortestPath_DIJ(g,1);cout<<"最小疲劳值为:"<<d[g.vnum];
    return 0;
}
/*
6 7
1 1 2 3
1 2 3 2
0 1 3 30
0 3 4 20
0 4 5 30
1 3 5 6
1 5 6 1

5 4
1 1 2 2
1 2 3 2
0 3 4 2
1 4 5 2
*/

Guess you like

Origin blog.csdn.net/changbaishannefu/article/details/111354009