标签:
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 27506 | Accepted: 12562 |
Description
Input
Output
Sample Input
1 3 0 990 692 990 0 179 692 179 0
Sample Output
692
Hint
Source
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 505;
const int Inf = 0x3f3f3f;
int map[maxn][maxn];
int dis[maxn];
int vis[maxn];
int n,m;
int t;
void init()
{
for(int i=0;i<n;i++)
{
dis[i] = map[i][0];
vis[i] = 0;
}
}
int prim()
{
init();
dis[0] = 0;
vis[0] = 1;
int ans = 0;
int max = -1;
for(int i=0;i<n-1;i++)
{
int min = Inf;
int temp;
for(int j=0;j<n;j++)
{
if(!vis[j] && dis[j] < min)
{
temp = j;
min = dis[j];
}
}
vis[temp] = 1;
ans += dis[temp];
if(dis[temp] > max)
max = dis[temp];
for(int j=0;j<n;j++)
{
if(!vis[j] && dis[j] > map[temp][j])
dis[j] = map[temp][j];
}
}
return max;
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
scanf("%d",&map[i][j]);
}
int ans = prim();
printf("%d\n",ans);
}
return 0;
}
/*
1
3
0 990 692
990 0 179
692 179 0
*/
标签:
原文地址:http://blog.csdn.net/sara_yf/article/details/51354548