Timus 1982 Electrification Plan (minimum spanning tree)

Source: Internet
Author: User

Electrification Plantime limit:0.5 Second
Memory limit:64 Mbsome Country has NCities. The government have decided to electrify all these cities. At first, power stations in kDifferent cities were built. The other cities should is connected with the power stations via power lines. For any cities I, JIt's possible to Builda power line between them in C IJRoubles. The country is in crisis after a civil war and so the government decided to build has only a few power lines. Of course from every city there must is a path along the lines to some city with a power station. Find the minimum possible cost-to-build all necessary power lines. Inputthe First line contains integers Nand k(1≤ kN≤100). The second line contains kDifferent integers that is the numbers of the cities with power stations. The next NLines contain an NX NTable of integers { C IJ} (0≤ C IJ≤105). It's Guaranteed that C IJ= C ji, C IJ> 0 for IJ, C II= 0.OutputOutput the minimum cost to electrify all the cities. Sample
input Output
4 21 40 2 4 32 0 5 24 5 0 13 2 1 0
3
problem Author:Mikhail Rubinchik "Analysis" assigns the distance between any two cities with a power station to 0, and then prim the minimum spanning tree. (Such a simple idea I actually did not think, or the seniors taught me).
#include <iostream>#include<cstring>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<time.h>#include<string>#include<map>#include<stack>#include<vector>#include<Set>#include<queue>#defineINF 10000000#defineMoD 10000typedefLong Longll;using namespacestd;Const intn=6005;Const intm=50000;intPowerintAintBintc) {intans=1; while(b) {if(b%2==1) {ans= (ans*a)%c;b--;} B/=2; a=a*a%c;}returnans;}intA[n],w[n][n],vis[n],lowcost[n];intn,m,k;voidPrim () {intsum=0; lowcost[1]=-1;  for(intI=2; i<=n;i++) {Lowcost[i]=w[1][i]; }     for(intI=1; i<n;i++){        intminn=inf,k;  for(intj=1; j<=n;j++){            if(lowcost[j]!=-1&&lowcost[j]<Minn) {k=j;minn=Lowcost[j]; }} sum+=Minn; LOWCOST[K]=-1;  for(intj=1; j<=n;j++) {Lowcost[j]=min (lowcost[j],w[k][j]); }} printf ("%d\n", sum);}intMain () {scanf ("%d%d",&n,&m);  for(intI=1; i<=m;i++) scanf ("%d",&A[i]);  for(intI=1; i<=n;i++) for(intj=1; j<=n;j++) scanf ("%d",&W[i][j]);  for(intI=1; i<=m;i++) for(intj=1; j<=m;j++) w[a[i]][a[j]]=0;    Prim (); return 0;}
View Code

Timus 1982 Electrification Plan (minimum spanning tree)

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.