# 还是畅通工程

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 22101    Accepted Submission(s): 9871

Problem Description

Input

Output

Sample Input

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

Sample Output

3 5

Hint

Hint

Huge input, scanf is recommended.

``` 1 #include<stdio.h>
2 #include<string.h>
3 const int inf=0x3f3f3f3f;
4 int vis[101],lowc[101];
5 int sta[101][101];
6 int prim(int cost[][101], int n)
7 {
8     int i,j,p;
9     int minc,res=0;
10     memset(vis , 0 , sizeof(vis));
11     vis[0] = 1;
12         for(i=1;i<n;i++)
13         {
14             lowc[i] = cost[0][i];
15         }
16     for(i=1 ; i<n ; i++)
17     {
18         minc=inf ; //<初始化一个较大的数>
19         p=-1;
20         for(j=0 ; j<n ; j++)
21         {
22             if(0==vis[j] && minc > lowc[j] )
23             {
24                 minc = lowc[j];
25                 p = j;
26             }
27         }
28         if(inf == minc) return -1;  //原图不联通
29         res += minc ;
30         vis[p] = 1;
31         for( j=0 ; j<n ; j++)
32         {
33             if(0==vis[j] && lowc[j] > cost[p][j])
34                 lowc[j] = cost[p][j];
35         }
36     }
37     return res ;
38 }
39 int main( void )
40 {
41     int n,i,a,b,c,j;
42     while(scanf("%d",&n),n)
43     {
44         for(i=0;i<n;i++)
45         {
46             for(j=0;j<n;j++)
47             {
48                 sta[i][j]=inf;
49             }
50         }
51         for(i=0 ; i<n*(n-1)/2 ; i++)
52         {
53             scanf("%d%d%d",&a,&b,&c);
54             sta[a-1][b-1]=sta[b-1][a-1]=c;
55         }
56         printf("%d\n",prim(sta,n));
57     }
58     return 0;
59 }```

657 篇文章64 人订阅

0 条评论

## 相关文章

41190

32740

### FZU 2099 魔法阵（计算几何）

Problem 2099 魔法阵 Accept: 120    Submit: 289 Time Limit: 1000 mSec    Memory L...

29430

344100

### Leetcode 150 Evaluate Reverse Polish Notation

Evaluate the value of an arithmetic expression in Reverse Polish Notation. Val...

213100

16310

10920

18870

29160

30440