注意:这个题目最终使得1和n连通即可,之前理解错了题目,一直做错,-_-|| 代码附在下面,主要思路就是最小生成树(Kruskal算法),只不过最后不是求和而是求最大值
#include <cstdio> #include <algorithm> #include <iostream> #include <queue> using namespace std; const int maxn(1e5 + 10); int father[maxn]; struct edge{ int src, dest; int dis; edge(int src, int dest, int dis){ this->src = src; this->dest = dest; this->dis = dis; } edge(){} }; inline bool operator < (const edge &first, const edge &second){ return (first.dis > second.dis); } int n, m; void init(); int find(int); int main() { while(scanf("%d %d",&n, &m) != EOF) { init(); priority_queue <edge> pq; int rest = n - 1; int src, dest, dis; int ans = 0; for(int i = 0; i < m; i++){ scanf("%d %d %d",&src,&dest,&dis); pq.push(edge(src,dest,dis)); } while(rest && !pq.empty() && find(1) != find(n)){ edge cur = pq.top(); pq.pop(); if(find(cur.src) != find(cur.dest)){ rest--; father[find(cur.src)] = find(cur.dest); ans = max(ans,cur.dis); } } printf("%d\n",ans); } return 0; } void init(){ for(int i = 1; i <= n; i++){ father[i] = i; } } int find(int index){ if(father[index] == index){ return index; } return (father[index] = find(father[index])); }