NYOJ wiring problems and nyoj wiring problems

Source: Internet
Author: User

NYOJ wiring problems and nyoj wiring problems
Wiring Problem time limit: 1000 MS | memory limit: 65535 KB difficulty: 4

Description
Nanyang institute of technology needs to transform the power line. The principal now asks the designer to design a wiring method which must meet the following requirements:
1. Power up all the buildings.
2. Minimum Cost of wires used
Input
The first line is an integer n, indicating that there are n groups of test data. (N <5)
The first line of each group of test data is two integers v, e.
V indicates the total number of school buildings (v <= 500)
In the subsequent line e, each line has three integers a, B, and c, which indicate that the cost of laying a line between a and B is c (c <= 100 ). (If no cost is specified for the two buildings, it indicates that the direct connection between the two buildings is too costly or impossible)
In the next line, there is a v integer, in which the I number represents the cost required to connect the wiring from building I to the external power supply facilities. (0 <e <v * (v-1)/2)
(The building number starts from 1). Due to security issues, you can only select one building to connect to the external power supply device.
There is at least one solution for data assurance to meet the requirements.
Output
Each group of test data outputs a positive integer, indicating the minimum cost of laying a line that meets the requirements of the principal.
Sample Input
14 61 2 102 3 103 1 101 4 12 4 13 4 11 3 5 6
Sample output
4
#include<cstdio>#include<cstdlib>#include<cstring>#define inf 0x3f3f3f3fusing namespace std;int low[510],map[510][510],vis[510],v;int prime(){int min,pos,i,j,k,result=0;memset(vis,0,sizeof(vis));pos=1;vis[1]=1;for(i=1;i<=v;++i){if(i!=pos)low[i]=map[pos][i];}for(j=1;j<v;++j){min=inf;for(i=1;i<=v;++i){if(!vis[i]&&min>low[i]){min=low[i];pos=i;}}result+=min;vis[pos]=1;for(i=1;i<=v;++i){if(!vis[i]&&low[i]>map[pos][i])low[i]=map[pos][i];}}return result;}int main(){int n,k,i,j,a,b,min,e,c;scanf("%d",&k);while(k--){memset(map,0x3f,sizeof(map));scanf("%d%d",&v,&e);while(e--){scanf("%d%d%d",&a,&b,&c);map[a][b]=map[b][a]=c;}min=inf;for(i=0;i<v;++i){scanf("%d",&a);if(a<min)min=a;}min=min+prime();printf("%d\n",min);}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.