C-Language summary

Source: Internet
Author: User

#include <stdio.h>#include<stdlib.h>#include<stdafx.h>#include<malloc.h>#defineINFINITY 9999/* Max ∞*/#defineMax_vex_num 30 * * Maximum number of vertices */graphtypedefstruct{    intVexnum;/*The number of current vertices and arcs (edges) of the graph*/    intAdj[max_vex_num][max_vex_num];/*adjacency Matrix*/}adjgraph;intCreategraph (Adjgraph *g,int*start,int*end)/*read data, set up a graph*/{    intN, M, I, J, K, S, Count; intTemp[max_vex_num]; printf ("Enter the number of tourist attractions and bus lines: \ n"); scanf ("%d%d", &n, &m); if(n<=1|| m<1)return-1; printf ("Enter the starting attraction number and stop attraction number: \ n"); scanf ("%d%d", Start,end); if(*start<0|| *start>n-1|| *end<0|| *end>n-1)return-1; G->vexnum=N;  for(i=0; i<n;i++)         for(j=0; j<n;j++) G->adj[i][j]=infinity;/*adjacency Matrix Initialization*/     for(s=0; s<m;s++) {printf ("Please enter number%d bus routes via the attractions: \ n", s+1); scanf ("%d",&k); Count=0;  while(k!=-1) {Temp[count++]=k; scanf"%d",&k); }         for(i=0; i<count-1; i++)             for(j=i+1; j<count;j++)/*The current line, from T[i] to T[J] has a direct bus*/G->adj[temp[i]][temp[j]]=1; }    return 0;}intFindminmum (Adjgraph G,intStartintEnd/*Find and return the shortest path from vertex start to end in the graph*//*Length (minimum number of boarding times)*/{    intS[max_vex_num];/*S[i]==1 says it has found the minimum number of boarding times to arrive at attraction I*/    inti,j,u,*Dist; intmin; if(Start==end)return 0; Dist=(int*) malloc (sizeof(int));/*Store the minimum number of boarding times for start to various attractions*/     for(i=0; i<g.vexnum;i++) {Dist[i]=g.adj[start][i];/*pick-up times from start-up attractions 1*/S[i]=0;/*minimum number of boarding times for all attractions I have not been found*/} S[start]=1;/*the minimum number of boarding times has been found to arrive at the attraction start*/Dist[start]=0;/*minimum number of boarding times from attraction start to start equals 0*/     for(i=0; i<g.vexnum;i++) {min=INFINITY; U=start;  for(j=0; j<g.vexnum;j++)            if(s[j]==0&&dist[j]<min) {min=DIST[J]; U=j; }/*You are the least able to get to all the sights you can reach from start*/S[u]=1;/*have found the minimum number of boarding times from the spot start to u, adding u to the set S*/         for(j=0; j<g.vexnum;++j)/*Update the minimum number of boarding times for other attractions in the current situation*/            if(s[j]==0&&min+G.adj[u][j]<Dist[j]) dist[j]=min+G.adj[u][j]; }    returnDist[end];/*returns the minimum number of cars from the spot start to the attraction end*/}intMain () {intstart,end,m,forshow;    Adjgraph G; if(Creategraph (&g,&start,&end) ==-1) {printf ("failed to create a map! \ n"); return-1; } m=findminmum (G,start,end);/*minimum number of boarding times from start to end*/    if(m==0) printf ("from attraction%d to attraction%d do not need a ride \ n", Start,end); Else if(m<INFINITY) printf ("the minimum number of transfers from attraction%d to attraction%d is:%d\n", start,end,m-1); Elseprintf ("No solution!\n"); scanf ("%d",&forshow); return 0; }

C-Language summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.