求大佬查错

P3366 【模板】最小生成树

bool cmp(const int &i,const int &j){ return i<j; } 应该是这个吧。 //跳循环的条件保险起见一般现在if外面,我比较喜欢写在for里面
by Dog_Two @ 2018-01-02 22:05:05


@[Dog\_Two](/space/show?uid=38283) 好像不是这个问题。。。。
by FiaoxR @ 2018-01-02 22:10:58


@[FiaoxR](/space/show?uid=62682) 你这个书害人不浅啊······没有用结构体存边,cmp函数的基数还这么恐怖······
by Dog_Two @ 2018-01-02 22:22:32


@[Dog\_Two](/space/show?uid=38283) 。。。。 可能是我菜
by FiaoxR @ 2018-01-02 22:26:29


看你造化了,,, https://www.luogu.org/record/show?rid=4806589
by Marginalin @ 2018-01-02 22:32:48


@[Marginalin](/space/show?uid=19741) 我。。没到60分。。。。。。
by FiaoxR @ 2018-01-02 22:34:24


```cpp @[FiaoxR](/space/show?uid=62682) #include<bits/stdc++.h> using namespace std; const int Node_n=5050,Edge_n=200000+50; struct edge{ int u,v,w; /*bool operator <(edge n)const{ return w<n.w; }*/ }; int fa[Node_n]; vector<edge>E; int n,m; bool if_uni; int getfa(int a){return fa[a]==a?a:fa[a]=getfa(fa[a]);} bool equal(int a,int b){return getfa(a)==getfa(b);} void uni(int a,int b){fa[getfa(a)]=getfa(b);} bool cmp(int a,int b){ return a<b; } void readin(){ int x,y,w; cin>>n>>m; for(int i=1;i<=n;i++) fa[i]=i; for(int i=1;i<=m;i++){ scanf("%d%d%d",&x,&y,&w); E.push_back((edge){x,y,w}); } sort(E.begin(),E.end(),cmp); } void Kruskal(){ int cnt=0,tot=0; for(int i=0;i<E.size();i++){ register int f1=getfa(E[i].u),f2=getfa(E[i].v); if(f1!=f2) { uni(f1,f2); tot+=E[i].w; cnt++; } if(cnt==n-1) break; } if_uni=cnt==n-1; if(if_uni) printf("%d",tot); else puts("orz"); } int main(){ readin(); Kruskal(); return 0; } ``` - 用结构体存边,不然排序的时候会打乱边权 - - cmp函数的用法要注意 - 目前就这两条(重载运算符临时改的cmp函数,可能会有bug,你自己注意下)
by Dog_Two @ 2018-01-02 22:34:47


```cpp @[FiaoxR](/space/show?uid=62682) #include<algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <cmath> using namespace std; const int MAXN = 5000 + 10; const int MAXM = 200000 + 10; #define For(i, a, b) for(int i=a; i<=b; i++) #define Dow(i, a, b) for(int i=a; i>=b; i--) void Read(int&); int n, m, sum, fa[MAXN], tot, cnt; struct Edge { int u, v, w; }edge[MAXM]; inline void add(int u, int v, int w) { edge[++cnt] = (Edge) { u, v, w }; } inline bool cmp(const Edge & a, const Edge & b) { return a.w < b.w; } int find(int x) { return x==fa[x]? x : fa[x]=find(fa[x]); } inline void Kruskal() { sort(edge+1, edge+m+1, cmp); For(i, 1, n) fa[i] = i; For(i, 1, m) { int f1 = find(edge[i].u); int f2 = find(edge[i].v); if(f1 != f2) { fa[f1] = f2; sum += edge[i].w; tot ++; } } } inline void init() { Read(n); Read(m); For(i, 1, m) { int u, v, w; Read(u); Read(v); Read(w); add(u, v, w); } } inline void print() { if(tot < n-1) puts("orz"); else printf("%d", sum); } int main() { init(); Kruskal(); print(); return 0; } inline void Read(int &x) { x = 0; bool fg=1; char ch; ch = getchar(); while(ch<'0' || ch>'9') { if(ch == '-') fg = -1; ch = getchar(); } while(ch>='0' && ch<='9') { x = (x<<3) + (x<<1) + ch - 48; ch = getchar(); } x *= fg; } ```
by Marginalin @ 2018-01-02 22:35:35


谢谢两位大佬 我再看看
by FiaoxR @ 2018-01-02 22:43:31


|