【解题思路】找最小生成树中权值最大的那条边输出,模板过的,出现了几个问题,开的数据不够大导致运行错误,第一次用模板,理解得不够深厚且模板有错误导致提交错误
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#define INF 1000000002
#define MAXN 2002
#define SIZE 20200 using namespace std; int eh[MAXN], tot, dist[MAXN];
bool visit[MAXN];
int n, m;
struct Edge{
int v, u, cost, next;
Edge(){}
Edge(int a, int b, int c, int d){
v = a, u = b, cost = c, next = d;
}
Edge(int a, int b){v = a, cost = b;}
bool operator < (const Edge &x) const{
return cost > x.cost;
}
};
struct Edge edge[SIZE]; int prim(int s)
{
for(int i = ; i <= n; ++i) visit[i] = false, dist[i] = INF;
dist[s] = ;
priority_queue<Edge> que;
que.push(Edge(s, ));
int ans = ;
while(!que.empty())
{
Edge tmp = que.top();
que.pop();
int u = tmp.v, cost = tmp.cost;
if(visit[u]) continue;
if(cost > ans) ans = cost;
visit[u] = true;
for(int i=eh[u]; i != -; i = edge[i].next)
{
int v = edge[i].u;
if(!visit[v] && dist[v] > edge[i].cost)
{
dist[v] = edge[i].cost;
que.push(Edge(v, dist[v]));
}
}
}
return ans;
} void addedge(int a, int b, int c)
{
Edge e = Edge(a, b, c, eh[a]);
edge[tot] = e;
eh[a] = tot++;
} void init()
{
tot = ;
memset(eh, -, sizeof(eh));
} int main()
{
scanf("%d%d", &n, &m);
init();
for(int i = ; i <= m; ++i)
{
int u, v, cost;
scanf("%d%d%d", &u, &v, &cost);
addedge(v, u, cost);
addedge(u, v, cost);
}
printf("%d\n", prim());
return ;
}