Messenger--Graph theory algorithm

Source: Internet
Author: User

Title Description Description

during the war, there were n posts on the front line, and each post might have communication links with several other posts. The messenger is responsible for transmitting information between posts, which, of course, takes a certain amount of time (in days). The command is located at the first outpost. When command issued an order, command sent a number of messengers to the command-linked posts. When a post is received, the messengers in the outpost also send letters to other posts in the same way. It was not until all n posts had been ordered that the messenger was successful. Because of the adequacy of the preparations, sufficient messengers are arranged in each post (if a post is communicated with other K-posts, at least a K-Messenger will be provided in the outpost).
Now the commander-in-chief asked you to compile a program that calculates the shortest time required to complete the entire Messenger process. input/output format input/output Input Format:

Input file msner.in, the 1th line has two integers n and m, in the middle with 1 spaces separated, respectively, there are n posts and m lines of communication. 1<=n<=100. Lines 2nd to m+1: Three integers I, j, K per line, separated by 1 spaces in the middle, indicating that there is a communication line between the first and the J posts, and that the line takes K days.

output Format:
The output file, Msner.out, is an integer that represents the shortest time to complete the entire Messenger process. If not all posts can receive the letter, output-1. input and Output sample sample Input/output sample Test point # #

Input Sample:

4 4
1 2 4
2 3 7
2 4 1
3 4 6

Sample Output :

11

Idea: This problem just started I want to use the shortest path to do, the results found to completely traverse each point, and the shortest distance, it is difficult to determine the location of the starting point, and many of the online analysis is to use Dixtra or Freud shortest path, may be my technology is not enough, only think of the smallest tree to do, because the shortest branch, And each branch can touch each leaf, found the effect is very good, below I say my process:

This problem can be directly called Primm Prim algorithm can be implemented, just at the beginning of the array to do some processing (see Code), and then use a variable minx statistics of the minimum spanning tree each edge of the sum of the value of the

The code is as follows:

1#include <stdio.h>2 #defineMAXV 5//maximum number of vertices3 #defineINF 32767//inf indicates ∞ Infinity4typedefstruct                  //definition of Diagram5 {      6     intEDGES[MAXV][MAXV];//adjacency Matrix7        intN,e;//number of vertices, number of arcs8} mgraph;//adjacency matrix type of graphs9 intminx=0;//Record AnswersTen voidPrim (Mgraph G,intv) One { A     intLOWCOST[MAXV];//whether vertex i is in U -     intmin; -     intclosest[maxv],i,j,k; the      for(i=0; i<g.n;i++)//Set initial values for lowcost[] and closest[] -     {     -lowcost[i]=G.edges[v][i]; -closest[i]=v; +     } -      for(i=1; i<g.n;i++)//Find a n-1 vertex +     { Amin=INF; at          for(j=0; j<g.n;j++)//in (v-u), find the nearest vertex k from U -             if(lowcost[j]!=0&& lowcost[j]<min) -             { -min=Lowcost[j]; -K=j;//K Record the number of the nearest vertex -             } inminx+=min; -lowcost[k]=0;//Mark K already joined U to          for(j=0; j<g.n;j++)//modifying arrays lowcost and closest +             if(g.edges[k][j]!=0&&g.edges[k][j]<Lowcost[j]) -             { thelowcost[j]=G.edges[k][j]; *closest[j]=K; $             }Panax Notoginseng     } - } the intMain () + { A     inti,j; the     intaa,b,z; +     inta[5][5]; - mgraph G; $scanf"%d%d",&g.n,&g.e); $      for(i=0; i<g.n;i++)//all assigned to infinity, the input will overwrite the INF, no input will not be overwritten, indicating that the i-j is not connected -     { -          for(j=0; j<g.n;j++) the         { -g.edges[i][j]=INF;Wuyi         } the     } -      for(i=0; i<g.n;i++)//The diagonal is assigned a value of 0 (itself to itself is 0) Wu     { -g.edges[i][i]=0; About     }     $      for(i=0; i<g.n;i++) -     { -scanf"%d%d%d", &aa,&b,&z);//Enter the weight of the aa-b -g.edges[aa-1][b-1]=Z; Ag.edges[b-1][aa-1]=Z; +     } thePrim (G,0);//calling the Primm algorithm -printf"%d\n", Minx);//sum of weights for each edge of the output minimum spanning tree $}

Messenger--Graph theory algorithm

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.