首页 > 代码库 > 杭电1233还是畅通project
杭电1233还是畅通project
还是畅通project
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25177 Accepted Submission(s): 11174
当N为0时,输入结束,该用例不被处理。
3 1 2 1 1 3 2 2 3 4 4 1 2 1 1 3 4 1 4 1 2 3 3 2 4 2 3 4 5 0
3 5
这道题和继续畅通project是一样的,代码仅仅修改了一点点,注意题意中的N(N-1)/2,这个代表的是(N*(N-1))/2,而那道继续畅通project是N*((N-1)/2)
代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
#define INF 1 << 30
int map[1001][1001] ;
int dis[1001] ;
int used[1001] ;
void Prim(int N)
{
int i = 0 ,j = 0 ;
int c = 0 ;
int sum = 0 ;//用来记录最后所须要的花费
dis[1] = 0 ;
for( i = 1 ; i <= N ; i++)
{
int min = INF ;
for( j = 1 ; j <= N ; j++)
{
if(!used[j] && dis[j] < min)
{
min = dis[j] ;
c = j ;
}
}
used[c] = 1 ;
for(j = 1 ; j <= N ; j++)
{
if(!used[j] && dis[j] > map[c][j])
dis[j] = map[c][j] ;
}
}
for(i = 1 ; i <= N ; i++)
sum += dis[i] ;
printf("%d\n",sum);
}
int main()
{
int N = 0 ;
while(~scanf("%d",&N))
{
if(N == 0)
break ;
int a = 0 , b = 0 , c = 0 ;
int i = 0 , j = 0 ;
for(i = 1 ; i <= N ; i++)
{
for(j = 1 ; j <= N ; j++)
map[i][j] = INF ;
dis[i] = INF ;
used[i] = 0 ;
}
int m = 0 ;
m = (N * (N-1)) / 2;
for( i = 0 ; i < m ; i++)
{
scanf("%d%d%d" , &a , &b , &c );
//推断是否会有重边
if(map[a][b] > c)
map[a][b] = map[b][a] = c ;
}
Prim( N ) ;
}
return 0 ;
}
杭电1233还是畅通project