The diagram of data structure experiment six: Village Road

Source: Internet
Author: User
Tags time limit
the diagram of data structure experiment six: Village RoadTime limit:1000ms Memory limit:65536kb Submit statistic problem DescriptionThe current rural road construction is in full swing, a township government decided to achieve the village road, engineers existing villages between the original road statistics table, the table lists the villages can build road between the cost of several roads, your task is based on the data sheet given, Each village has the lowest cost required for road connectivity. InputConsecutive sets of data input, each group of data including the number of villages N (n <= 1000) and the number of roads to choose m (M <= 3000), followed by M-Line M road, each row gives 3 positive integers, respectively, the road is directly connected to the number of two villages and the budget cost of building the road, The village is numbered from the 1~n. OutputThe output allows each village to have the minimum cost of road connectivity, and if the input data does not allow all villages to flow, the output is 1, indicating that there is no road connection between some villages. Example Input
5 8
1 2
1 3 9
1 4 each 1 5 3
2 3 6
2 4 9
3 4 4
4 5 6
Example Output
19
This topic uses the PRIMM algorithm, that is, from all the points connected to the current node, to find a minimum cost, followed by the idea of adding points.
#include <bits/stdc++.h> using namespace std;          #define INF 0x3f3f3f3f//Infinity constant int gra[1005][1005];             The adjacency matrix of graphs is stored, and the storage weights bool visit[1005];            Mark Int lowcost[1005];                
   Record the cost required to connect to the current node int sum, flag;                   int m, n;
    Node count edge number void prime () {int k, temp;                            Visit[1] = true;
    Starting from 1 nodes for (int i = 1; I <= m; i++)//record the cost required for nodes connected to 1 nodes lowcost[i] = gra[i][1];                             
        for (int i = 2; I <= m; i++) {//from 2 to M temp = INF; for (int j = 1; j <= M; j + +) {//Find a node that has not been traversed and is the least expensive if (!visit[j] && lowcost[j] < Temp
                ) {temp = Lowcost[j];
            K = J;
            }} if (temp = = inf) {//If TEMP is also equal to INF then it is not a connected graph flag = 1;
        Break
        } Visit[k] = true;
        sum + = temp; for (int j = 1; J <= M; 
                J + +) {//from the found K node start record the cost required for the node connected with K to execute all nodes sequentially if (!visit[j]&&lowcost[j] > Gra[j][k])
        LOWCOST[J] = gra[j][k];
    }}} int main () {int u, V, cost;               while (cin>>m>>n) {memset (GRA, INF, sizeof (GRA));
        Initialized to infinity is actually a non-net right is not connected to the value of Infinity memset (visit, false, sizeof (visit));
        Flag = 0;
        sum = 0;
            for (int i = 0; i < n; i++) {//Enter a connected node cin>>u>>v>>cost;
            GRA[U][V] = cost;
        Gra[v][u] = cost;
        } prime ();
        if (!flag) cout<<sum<<endl;
    Else cout<< "-1" <<endl;
} return 0;
 }


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.