您的位置:首页 > 其它

poj 2485 Highways(最小生成树,prim)

2014-07-30 16:35 573 查看
小记:求最小生成树里最大的边,

思路:在之前求和的基础上,将和改成判断保存最大的那条边即可。

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <string>

using namespace std;

#define mst(a,b) memset(a,b,sizeof(a))
#define REP(a,b,c) for(int a = b; a < c; ++a)
#define eps 10e-8

const int MAX_ = 550;
const int N = 100010;
const int INF = 0x7fffffff;

int n;

struct node{
int s, t;
};

int g[MAX_][MAX_];
int d[MAX_];
bool vis[MAX_];
int m, cnt;

int prim(int start)
{
REP(i, 0, n){
d[i] = INF;
vis[i] = 0;
}
int sum = -1;
d[start] = 0;

REP(i, 0, n){
int mmin = INF, k;
REP(j, 0, n){
if(!vis[j] && d[j] < mmin){
mmin = d[j];
k = j;
}
}
if(mmin == INF)break;
vis[k] = 1;
//if(mmin)
sum = max(sum,mmin);
REP(j, 0, n){
if(!vis[j] && g[k][j] != -1 && d[j] > g[k][j]){
d[j] = g[k][j];
}
}
}
return sum;
}

int main(){
int T, ss, tt;
char str[10];
scanf("%d", &T);

while(T-- && scanf("%d", &n)){

mst(g, -1);

REP(i, 0, n)REP(j, 0, n){
scanf("%d", &g[i][j]);

}
/*
REP(i, 0, n)REP(j, 0, n){
printf("%d ", g[i][j]);
if(j == n-1)printf("\n");
}*/

int ans = prim(0);

printf("%d\n", ans);

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: