Red de conocimiento informático - Conocimiento del nombre de dominio - Solicitud urgente, programa de problemas de diseño de rutas, ¡puntuación alta! ¡Entran los expertos en programación! ! lenguaje c!

Solicitud urgente, programa de problemas de diseño de rutas, ¡puntuación alta! ¡Entran los expertos en programación! ! lenguaje c!

Este es un problema del camino más corto. Tratas el peso de cada camino como 1.

El problema final es encontrar el camino más corto (los puntos de la ciudad pasados ​​en este momento corresponden a la longitud del camino).

Finalmente ingresa al camino.

No puedo darte una implementación específica por el momento. Si es necesario, la escribiré cuando esté libre.

Ahora sólo te doy una referencia para encontrar el camino más corto.

Puedes entrar a mi blog para verlo.

¿Existe una implementación específica (www.ourys.com/sword)?

El programa es el siguiente:

//*****Implementación de pila

# include?lt;stdio.hgt;

#include?lt;stdlib.hgt;

#define?STACK_INIT_SIZE?100

#define?STACKINCREMENT ?10

typedef?int?Estado;

typedef?int?QElemType; typedef?struct?SqStack{ QElemType?*base; QElemType?*top; ;

void?InitStack(SqStack?*S)

{ (*S).base?=?(QElemType?*)malloc(STACK_INIT_SIZE?*?sizeof(QElemType)) ; si (!(*S).base)?exit(0); (*S).top?=?(*S).base (*S).stacksize?=?STACK_INIT_SIZE;

}

void?DestroyStack(SqStack?*S)

{ gratis((*S).base);

}

void ?ClearStack(SqStack?*S)

{ (*S).top?=?(*S).base;

}

Estado ?StackEmpty (SqStack?*S)

{ if((*S).top?==?(*S).base)?return?1; else?return?0;

}

Estado?StackLength(SqStack?*S)

{ return?((*S).top?-?(*S).base);

}

QElemType?GetTop(SqStack?*S, QElemType?e)

{ if((*S).top?!=?(*S) .base ) { e?=?*((*S).top?-?1); return?e } else?printf("ERROR");

}

void ?Push(SqStack?*S, QElemType?e)

{ if((*S).top?-?(*S).base?gt;=?(*S).stacksize ) { (*S).base?=?(QElemType?*)realloc((*S).base, ((*S).stacksize? ?STACKINCREMENT?*?sizeof(QElemType)); S) .base)?exit(0); (*S).top?=?(*S).base?(*S).stacksize; (*S).stacksize? S) .top ?=?e;

}

QElemType?Pop(SqStack?*S)

{ if((*S).top? == ?(*

S).base)?printf("ERROR"); else { QElemType?e?=?*?--(*S).top; p>//End?Stack

typedef?struct?graph

{ int?vexnum; int?arcnum; QElemType?*vex;**AdjMatrix;

}graph;

void?CreateUDN(graph?*G)

{ printf("Ingrese el número de vértices y aristas del gráfico:\n"); (" dd",amp;((*G).vexnum),amp;((*G).arcnum)); QElemType?v1,v2; ?= ?(QElemType?*)malloc((*G).vexnum?*?sizeof(QElemType)); (*G).AdjMatrix?=?(int?**)malloc((*G).vexnum?* ?sizeof (int?*)); for(i?=?0;i?lt;?(*G).vexnum;i ) (*G).AdjMatrix[i]?=?(int?*)malloc( (* G).vexnum?*?sizeof(int)); for(i?=?0;i?lt;?(*G).vexnum;i ) for(j?=?0;j?lt;? (* G).vexnum; j )

(*G).AdjMatrix[i][j]?=?10000; //Usa números grandes para simular el infinito printf("Ingresa el significado de cada vértice (como 1 representa el segundo vértice, 2 representa el segundo):\n"); for(i?=?0;i?lt;?(*G).vexnum;i)

scanf("d" , amp; ((*G).vex[i])); printf("Ingrese los dos puntos donde se adjunta cada borde del gráfico no dirigido:\n"); ;i?lt;?( *G).arcnum; i ) {

scanf("dd",amp;v1,amp;v2);

for(j?= ?0;j?lt; ?(*G).vexnum;j )

{

if(v1?==?(*G).vex[j])? m?=?j;

if(v2?==?(*G).vex[j])?n?=?j;

}

(*G).AdjMatrix[m][n]?=?(*G).AdjMatrix[n][m]?=?1; void?Shortestpath_DIJ(graph?* gph, QElemType?data1, QElemType?data2)

{ int?i, j, v, u, k, min SqStack?S; int?*spath?=? (int?*)malloc((*gph).vexnum?*?sizeof(int)); int?*pathrecord?=?(int?*)malloc((*gph).vexnum? *?sizeof(int)) ; bool?*visitado?=?(bool?*)malloc((*gph).vexnum

?*?sizeof(bool)); for(i?=?0;i?lt;?(*gph).vexnum;i )?visited[i]?=?false; ?lt;?(*gph).vexnum;i ) { if(data1?==?(*gph).vex[i])?v?=?i; if(data2?==?(*gph). vex[i])?u?=?i } for(i?=?0;i?lt;?(*gph).vexnum;i ) {

spath[i]?=? (*gph).AdjMatrix[v][i];

pathrecord[i]?=?v; } spath[v]?=?0; visitado[v]?=?true; v]?=?-1; para(i?=?1;i?lt;?(*gph).vexnum;i ) {

min?=?10000;

for(j?=?0;j?lt;?(*gph).vexnum;j )

{

if(!visited[j])

{ if(spath[j]?lt;?min)?{min?=?spath[j];k?=?j;}

}

}

visitó[k]?=?true;

for(j?=?0;j?lt;?(*gph).vexnum;j )

if(!visited[j]?amp;amp;?(*gph).AdjMatrix[k][j]?lt;?10000?amp;amp;?spath[k] (*gph).AdjMatrix[k ][j]?lt;?spath[j])

{ spath[j]?=?spath[k] (*gph).AdjMatrix[k][j]; =?k;

} } gratis(visitado); Push(amp; S, u); for(v?=?pathrecord[u]; v?!=?-1; v?=? pathrecord[v])

Push(amp; S, v); while(!StackEmpty(amp; S)) {

i?=?Pop(amp; S);

printf("d?", (*gph).vex[i]); } printf("\n"); DestroyStack(amp;S); libre(spath); ;

}

void?main()

{ graph?G; CreateUDN(amp;G); printf("Ingrese el punto inicial y final: \n "); QElemType?data1, data2; scanf("dd", amp; data1, amp; data2); printf ("Los puntos de paso son los siguientes:\n"); Shortestpath_DIJ(amp; G, data1, amp ; data2);

}

La simulación se ejecuta de la siguiente manera:

Ingrese el número de vértices y aristas 4, 6

Ingrese los datos de los vértices: (por ejemplo, 1 significa Shenyang, 2 significa Dalian, establecido por usted mismo) 1, 2, 3, 4

Ingrese los dos puntos adjuntos a cada lado: (1, 2) , (1, 4), (1, 3), (2 ,3),

(2, 4), (3, 4)

Ingrese el punto inicial y el punto final: 1?3

¿Puede obtener el número mínimo de vértices que desea pasar (incluidos ¿El punto de partida y el punto final)?

Esto se puede ejecutar en VC6.0. Si tu compilador no funciona, házmelo saber