村镇修电路(ACM).doc_第1页
村镇修电路(ACM).doc_第2页
村镇修电路(ACM).doc_第3页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Build The Electric SystemTime Limit: 1 Second Memory Limit: 32768 KB In last winter, there was a big snow storm in South China. The electric system was damaged seriously. Lots of power lines were broken and lots of villages lost contact with the main power grid. The government wants to reconstruct the electric system as soon as possible. So, as a professional programmer, you are asked to write a program to calculate the minimum cost to reconstruct the power lines to make sure theres at least one way between every two villages. InputStandard input will contain multiple test cases. The first line of the input is a single integer T (1 = T = 50) which is the number of test cases. And it will be followed by T consecutive test cases. In each test case, the first line contains two positive integers N and E (2 = N = 500, N = E = N * (N - 1) / 2), representing the number of the villages and the number of the original power lines between villages. There follow E lines, and each of them contains three integers, A, B, K (0 = A, B N, 0 = K 1000). A and B respectively means the index of the starting village and ending village of the power line. If K is 0, it means this line still works fine after the snow storm. If K is a positive integer, it means this line will cost K to reconstruct. There will be at most one line between any two villages, and there will not be any line from one village to itself. OutputFor each test case in the input, theres only one line that contains the minimum cost to recover the electric system to make sure that theres at least one way between every two villages. Sample Input13 30 1 50 2 01 2 9Sample Output5#include /直接bfs无向图mstint a125000,b125000,k125000;int village501; /村庄的出现与否 -1 or 0void pai_xu(int e) /k 递增 int i,j,temp; for(i=0;ie-1;i+) for(j=i+1;jkj) temp=ki;ki=kj;kj=temp; temp=ai;ai=aj;aj=temp; temp=bi;bi=bj;bj=temp; int main() int i,t,j,h,sum,n,e; while(scanf(%d,&t)!=EOF) for(i=0;it;i+) scanf(%d%d,&n,&e); for(j=0;je;j+) scanf(%d%d%d,&aj,&bj,&kj); for(j=0;jn;j+) villagej=0; pai_xu(e); villagea0=villageb0=-1; sum=k0; for(j=1;jn;j+) for(h=1;he;h+) if(villageah!=-1&vil

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论