K算法调用un函数MLE
  • 板块P1194 买礼物
  • 楼主adampan
  • 当前回复2
  • 已保存回复2
  • 发布时间2021/3/31 19:34
  • 上次更新2023/11/5 01:18:55
查看原帖
K算法调用un函数MLE
295615
adampan楼主2021/3/31 19:34
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int maxn=1e3+5;
const int inf=0x3f3f3f3f;

struct node{
    int u, v, w;

    bool operator<(const node &p) const{
        return w<p.w;
    }
};

int fa[maxn];

int find(int x){
    if (fa[x]==x) return x;
    return fa[x]=find(fa[x]);
}

int un(int x, int b){
//    x=find(x);
//    b=find(b);
    fa[x]=b;
}

vector<node> a;

int main()
{
    ios::sync_with_stdio(false);
    int n, m;
    cin>>n>>m;
    for(int i=0; i<maxn; i++) fa[i]=i;
    for(int i=0; i<m; i++){
        for(int j=0; j<m; j++){
            int t;
            cin>>t;
            if(i<j){
                if (t==0) t=inf;
                a.push_back({i, j, t});
            }
        }
    }
    sort(a.begin(), a.end());
    int cnt=0;
    int now=-1;
    int ans=0;
    while(cnt<m-1){
        now++;
        int x=a[now].u, y=a[now].v, z=a[now].w;
        x=find(x);
        y=find(y);
        if (x==y) continue;
        cnt++;
        ans+=min(n, z);
        un(x, y);
//        fa[x]=y;
    }
    cout<<ans+n<<endl;

    return 0;
}
/*
  0. Enough array size? Enough array size? Enough array size? Integer overflow?

  1. Think TWICE, Code ONCE!
  Are there any counterexamples to your algo?

  2. Be careful about the BOUNDARIES!
  N=1? P=1? Something about 0?

  3. Do not make STUPID MISTAKES!
  Time complexity? Memory usage? Precision error?
*/

这个代码,在调用un函数的时候会出现MLE,但是我直接写语句就AC,有没有大佬解释一下是为啥啊

2021/3/31 19:34
加载中...