Nyoj 38 Cabling Problem _ (Solution 1 Kruskal algorithm)

Source: Internet
Author: User

Time limit: +Ms | Memory Limit:65535KB Difficulty:4
Descriptive narrative
Nanyang Institute of Technology to carry out the transformation of power lines. Now the headmaster asked the designer to design a wiring method, the wiring must meet the following conditions:
1, all the buildings are for electricity.
2, the use of wire to spend the least
Input
The first line is an integer n indicating that there is n set of test data.

(n<5)
The first line of the test data for each group is two integer v,e.
V indicates the total number of school buildings (v<=500)
In the subsequent E-line, there are three integers per line a,b,c for the assumption that the construction line between A and B costs C (c<=100). (The two buildings assume that the cost is not specified, which means that the direct connection between the two buildings is too expensive or impossible to connect)
In the next 1 lines. There is a v integer in which the number of I represents the cost of wiring from the I building to the external power supply facility. (0<e<v* (V-1)/2)
(The building number starts from 1), because of security problems, only can choose a building connected to the external power supply equipment.


Data guarantees that there is at least one scenario that satisfies the requirements.

Output
each set of test data outputs a positive integer that represents the minimum cost of laying the line that meets the headmaster's requirements.
Example input
14 61 2 102 3 103 1 101 4 12 4 13 4 11 3 5 6
Example output
4


Code such as the following

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int maxn = 505; struct Arcnode{int v1,v2;//v1, V2 indicates the interconnecting building int cost;//cost represents the cost of connecting V1, v2};int FATHER[MAXN],ADD[MAXN ];int V,e,s;bool cmp (const arcnode &LHS, const Arcnode &RHS) {return lhs.cost < rhs.cost;} void Kruskal (Arcnode *node) {int I,j,k,x,y;i=j=0;s=0;while (J<v-1) {x=father[node[i].v1-1];y=father[node[i].v2-1] if (x!=y) {for (k=0;k<v;k++) if (father[k]==y) father[k]=x;s+=node[i].cost;++j;} ++i;}} int main () {int n;int i;scanf ("%d", &n), while (n--) {scanf ("%d%d", &v,&e); Arcnode *node=new arcnode[e];for (i=0;i<e;i++) scanf ("%d%d%d", &node[i].v1,&node[i].v2,&node[i].cost ); sort (node,node+e,cmp); for (i=0;i<v;i++) {scanf ("%d", &add[i]); father[i]=i;} Sort (add,add+v); Kruskal (node);//Use Kruskal algorithm to find the least cost of connected graph printf ("%d\n", S+add[0]);d elete[] node;node=null;} return 0;}



Nyoj 38 Cabling Problem _ (Solution 1 Kruskal 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.