prim和kruskal全TLE,求助

P3366 【模板】最小生成树

lmn985 @ 2024-08-29 21:44:35

#include <bits/stdc++.h>
#define st first
#define nd second
#define MP make_pair
#define f(i,a,b) for(int i=a;i<=b;i++)
#define allx(a) a.begin(),a.end()
#define ally(a) a.rbegin(),a.rend()
#define allx_arr(a,n) next_permutation(a,a+n)
#define ally_arr(a,n) next_permutation(a.begin(),a.end())
using namespace std;
typedef long long ll;
const int N=1000005,X=105,dx[4]={0,0,-1,1};
const ll INF=1000000000000000000,mod=1000000007;
vector<int> pri(int n){
    vector<int> prime;
    bool is_prime[n+1];
    for(int i=0;i<=n;i++)is_prime[i]=1;
    for(int i=2;i<=n;i++){
        if(is_prime[i])
            prime.push_back(i);
        for(int j=0;j<prime.size();j++){
            if(prime[j]*i>n)break;
            is_prime[prime[j]*i]=0;
            if(i%prime[j]==0)break;
        }
    }
    return prime;
}
int h[100000],fat[100000];
int find(int x){
    if(fat[x]==x)return x;
    else return fat[x]=find(fat[x]);
}
void init(){
    for(int i=0;i<=99999;i++)fat[i]=i;
    fill(h,h+99999,1);
}
inline bool same(int x,int y){return find(x)==find(y);}
inline void unite(int x,int y){
    int lx=find(x),ly=find(y);
    if(h[lx]<h[ly])fat[lx]=ly;
    else {
        fat[ly]=lx;
        if(h[lx]==h[ly])h[lx]++;
    }
}
inline int pow(int x,int y){
    if(y<2)return max((int)(1),x);
    int cnt=pow(x,y/2);
    if(y%2==0)return cnt*cnt;
    else return cnt*cnt*x;
}
inline int log(int x,int a){
    if(a==x)return 1;
    return log(x,a/x)+1;
}
inline void read(int &a){
    char x;
    int cnt=1,ans=0;
    while(1){
        x=getchar();
        if(x==' ' or x=='\n')break;
        if(x=='-')cnt=-1;
        else ans*=10,ans+=x-'0';
    }
    a=cnt*ans;
}
inline void write(int a,char x){
    string s;
    while(a!=0)s+=a%10+'0',a/=10;
    reverse(s.begin(),s.end());
    for(int i=0;i<s.size();i++)
        putchar(s[i]);
    putchar(x);
}
#define print_(a,n) for(int i=0;i<n;i++)write(a[i],' ');
#define print_v(a) for(int i=0;i<a.size();i++)write(a[i],' ');
inline int gcd(ll x,ll y){
    return (x==0 or y==0)?x+y:gcd(min(x,y),max(x,y)%min(x,y));
}
inline int lcm(ll a,ll b){return a/gcd(a,b)*b;}
//---------------------以上均为模板----------------------//
int v,e,lx,ly,cost,mincost[N];
vector<pair<int,int> > G[N];
vector<pair<int,pair<int,int> > > edge;
bool used[N];
int prim(){
    int res=0;
    priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > w;
    w.push(MP(0,1));
    memset(mincost,0x3f,sizeof(mincost));
    mincost[1]=0;
    while(w.size()){
        int x=w.top().nd;
        w.pop();
        if(used[x])continue;
        res+=mincost[x];
        used[x]=1;
        for(int i=0;i<G[x].size();i++){
            int lx=G[x][i].st;
            mincost[lx]=min(mincost[lx],mincost[x]+G[x][i].nd);
            w.push(MP(mincost[lx],lx));
        }
    }
    return res;
}
int kruskal(){
    init();
    int res=0;
    sort(edge.begin(),edge.end());
    for(int i=0;i<edge.size();i++){
        if(!same(edge[i].nd.st,edge[i].nd.nd)){
            res+=edge[i].st;
            unite(edge[i].nd.st,edge[i].nd.nd);
        }
    }
    return res;
}
signed main(){
    read(v),read(e);
    for(int i=0;i<e;i++){
        read(lx),read(ly),read(cost);
        G[lx].push_back(MP(ly,cost));
        G[ly].push_back(MP(lx,cost));
        edge.push_back(MP(cost,MP(lx,ly)));
    }
    cout<<kruskal();
    return 0;
}

by lmn985 @ 2024-08-29 21:46:18

回复的一律关注


by SF_bee @ 2024-08-29 21:51:51

猜测快读写挂了,因为把快读换scanf就不超时了。


by SF_bee @ 2024-08-29 21:53:14

当然,问题不止快读,因为换scanf后雀氏不TLE,但是WA


by bladrrxy @ 2024-08-29 21:54:50

@lmn985 对,如果文件不以空格或换行结尾就寄


by bladrrxy @ 2024-08-29 21:58:08

@lmn985 没判不连通。。。


by JOKER_chu @ 2024-08-29 22:01:38

你快读错了,笑(


by lmn985 @ 2024-08-30 09:13:05

谢谢大佬的指点


by lmn985 @ 2024-08-30 09:14:57

%%%%%%%%%%%.......(%*INF)


|