首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

ACM题,数据测试正确,但下交异常,求指导

2013-03-20 
ACM题,数据测试正确,但上交错误,求指导Build The Electric SystemTime Limit: 2 SecondsMemory Limit: 655

ACM题,数据测试正确,但上交错误,求指导
Build The Electric System
Time Limit: 2 Seconds      Memory Limit: 65536 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 there's at least one way between every two villages.

Input

Standard 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.

Output

For each test case in the input, there's only one line that contains the minimum cost to recover the electric system to make sure that there's at least one way between every two villages.

Sample Input

1
3 3
0 1 5
0 2 0
1 2 9
Sample Output

5


#include<stdio.h>
#include<stdio.h>
#include<string.h>
int father[505];
typedef struct
{
int begin;
int end;
int value;
}path;

void sort(int n,path line[505])
{
int i,j,team;
for(i=0;i<n-1;i++)
for(j=0;j<n-1-i;j++)
{
if(line[j].value>line[j+1].value)
{
team=line[j].value;
line[j].value=line[j+1].value;
line[j+1].value=team;
team=line[j].begin;


line[j].begin=line[j+1].begin;
line[j+1].begin=team;
team=line[j].end;
line[j].end=line[j+1].end;
line[j+1].end=team;
}
}
}

int find(int x)
{
if(father[x]==x)
{
return x;
}
else
return father[x]=find(father[x]); 
}
int main()
{

int t;
int i,j;
int n,e;
int x,y;
path line[505];
int s;

scanf("%d",&t);
while(t--)
{
s=0;
scanf("%d%d",&n,&e);
memset(line,0,sizeof(line));
for(i=0;i<n;i++)
{
father[i]=i;
}
for(i=0;i<e;i++)
{
scanf("%d%d%d",&line[i].begin,&line[i].end,&line[i].value);
}
sort(e,line);
for(i=0;i<e;i++)
{
x=find(line[i].begin);
y=find(line[i].end);
if(x!=y) 
{
s+=line[i].value;
father[x]=y;
}
}
printf("%d\n",s);
}
return 0;
}


[解决办法]
好好看e的范围有多大……

热点排行