﻿HDU 1233 还是畅通工程(Prime)-Ocrosoft

# 还是畅通工程

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

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.

```

Solution

```#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <ctime>
#include <string>
#include <cstdio>
#include <vector>
#include <cctype>
#include <climits>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define ms(a) memset(a,0,sizeof(a))
#define INF INT_MAX
#define MAXN 100+10
using namespace std;
int n,m;
int mp[MAXN][MAXN],vis[MAXN],lowcast[MAXN];
int prime(int u0)
{
int ans=0,pos=u0;//pos记录为起点
ms(vis),vis[u0]=1;//清空vis数组,标记起点
for(int i=1; i<=n; i++)//初始化lowcast数组
if(i!=pos)lowcast[i]=mp[pos][i];
for(int i=1; i<n; i++)
{
int minn=INF;
for(int j=1;j<=n;j++)//找到没访问过的而且最近的邻近点
if(!vis[j]&&minn>lowcast[j])minn=lowcast[j],pos=j;
if(minn==INF)return 0;
ans+=minn;
vis[pos]=1;//标记这个邻近点
for(int j=1;j<=n;j++)//更新lowcast数组
if(!vis[j]&&lowcast[j]>mp[pos][j])lowcast[j]=mp[pos][j];
}
return ans;
}
int main()
{
while(cin>>n&&n)
{
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)mp[i][j]=INF;
mp[i][i]=0;
}
for(int i=1; i<=n*(n-1)/2; i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
mp[u][v]=mp[v][u]=w;
}
int ans=prime(1);
printf("%d\n",ans);
}
return 0;
}
```