仔细一看,就是把时间排序,然后把根据时间推进把这些点都连起来,那就是并查集问题,刚写完交上去是90分,加了一个优化变成100了。
#include <bits/stdc++.h> #define testa using namespace std; int n,m; class road{ public: int a,b,c; road(int q,int w,int e){ a=q; b=w; c=e; } }; bool cmp(road g,road h){ return g.c<h.c; } int father[100001]; int findfather(int l){ int u=l; //int u= findfather(l); while(father[l]!=l){ l= father[l]; } father[u]=l;//加上这句之后就从90变100了 return l; } int main(){ int j,k,l; cin>>n>>m; vector<road>roadlist; while(m--){ cin>>j>>k>>l; roadlist.push_back(road(j,k,l)); } for(int i=0;i<n;i++){ father[i]=i; } std::sort(roadlist.begin(), roadlist.end(),cmp); for(auto y:roadlist){ father[findfather(y.a)]= findfather(y.b); if (findfather(1)== findfather(n)){ cout<<y.c; return 0; } } }
标签:CSP201703,roadlist,int,修建,地铁,road From: https://www.cnblogs.com/hyffff/p/17100186.html