Nyo J Cabling Problem (and check && graph theory)

Source: Internet
Author: User
Tags cmath

Describe the Nanyang Institute of Technology to carry out the transformation of power lines, the headmaster now asked designers to design a wiring method, the wiring method needs to meet the following conditions:
1, all the buildings are to power up.
2, the use of wire to spend the least

Input
The first line is an integer n indicating that there are n sets of test data. (n<5)
The first line of each set of test data is two integer v,e.
V indicates the total number of school buildings (v<=500)
In the subsequent E-line, there are three integers in each line a,b,c represents a and B if the build line costs C (c<=100). (If the two buildings do not specify the cost, it means that the direct connection between the two buildings is too expensive or impossible to connect)
In the subsequent 1 lines, there are v integers, of 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)
(Building number starting from 1), due to safety issues, can only choose one 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.
Sample input
14 61 2 102 3 103 1 101 4 12 4 13 4 11 3 5 6
Sample output
4
Source
[Zhang Yunzun] Original
Uploaded by
Zhang Yunzun

Your own definition of the sort function is not written in front of Runtime error multiple times ...

After sorting, use and check set can be,,, prim can also handle ....

And check the code:

1#include <vector>2#include <map>3#include <Set>4#include <algorithm>5#include <iostream>6#include <cstdio>7#include <cmath>8#include <cstdlib>9#include <string>Ten#include <cstring> One#include <queue> A using namespacestd; - #defineINF 0x3f3f3f3f - #defineMAX 1000000 the  - intv; - intdis[555]; - structnode{ +     intA,b,cost; -}c[555555]; +  A BOOLCMP (node X,node y) at { -     returnx.cost<Y.cost; - } -  - voidInit () - { in      for(intI=1; i<=v; i++){ -dis[i]=i; to     } + } -  the intFindintx) * { $     if(dis[x]!=x)Panax Notoginsengdis[x]=find (Dis[x]); -     returnDis[x]; the } +  A intMain () the { +     intn,e; -scanf"%d",&n); $      while(n--){ $         intmin=inf,m,sum=0; -scanf"%d%d",&v,&e); - init (); the          for(intI=0; i<e; i++){ -scanf"%d%d%d",&c[i].a,&c[i].b,&c[i].cost);Wuyi         } the          for(intI=0; i<v; i++){ -scanf"%d",&m); Wu             if(min>m) -min=m; About         } $Sort (c,c+e,cmp); -          for(intI=0; i<e; i++){ -             intx=find (C[I].A); -             inty=find (c[i].b); A             if(x!=y) { +dis[x]=y; thesum+=C[i].cost; -             } $         } theprintf"%d\n", sum+min); the     } the     return 0; the}

Prim code:

1#include <vector>2#include <map>3#include <Set>4#include <algorithm>5#include <iostream>6#include <cstdio>7#include <cmath>8#include <cstdlib>9#include <string>Ten#include <cstring> One#include <queue> A using namespacestd; - #defineINF 0x3f3f3f3f - #defineMAX 1000000 the  - intdis[555],vis[555],mp[555][555]; - intv; -  + intPrim () - { +Fill (dis,dis+v,inf); Amemset (Vis,0,sizeof(Vis)); at     intn=1, ans=0; -      for(intI=1; i<=v; i++){ -         if(i!=N) -dis[i]=Mp[n][i]; -     } -vis[1]=1; in      while(true){ -         intmin=INF; to          for(intj=1; j<=v; J + +){ +             if(!vis[j] && min>Dis[j]) { -min=Dis[j]; then=J; *             } $         }Panax Notoginseng         if(min==INF) -              Break; theans+=min; +vis[n]=1; A          for(intI=1; i<=v; i++){ the             if(!vis[i] && dis[i]>Mp[n][i]) +dis[i]=Mp[n][i]; -         } $     } $     returnans; - } -  the intMain () - {Wuyi     intN,e,i,x,y,cost; thescanf"%d",&n); -      while(n--){ Wu         intsum=0, min=inf,m; -memset (Mp,inf,sizeof(MP)); Aboutscanf"%d%d",&v,&e); $          for(i=0; i<e; i++){ -scanf"%d%d%d",&x,&y,&Cost ); -mp[x][y]=mp[y][x]=Cost ; -         } A          for(i=0; i<v; i++){ +scanf"%d",&m); the             if(min>m) -min=m; $         } thesum=min+Prim (); theprintf"%d\n", sum); the     } the}

Nyo J Cabling Problem (and check && graph theory)

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.