prim求助

P3366 【模板】最小生成树

可以用$\texttt{Kruskal}$啊
by Visual_Studio_IDE @ 2019-07-27 09:54:13


```cpp #include<bits/stdc++.h> using namespace std; int n,m,v[5010][5010]={0},ans=0,pn=0; bool p[5010]; int q[5010],tmp=0; int main(){ ios::sync_with_stdio(false); cin.tie(0); cin>>n>>m; int cn,cn1,V; for (int i=1; i<=n; i++){ for (int j=1; j<=n; j++) v[i][j]=-1; } for (int i=1; i<=m; i++){ cin>>cn>>cn1>>V; v[cn][cn1]=V; v[cn1][cn]=V; } for (int i=1; i<=n; i++) p[i]=true; p[1]=false; q[1]=1; tmp=1; pn++; while (tmp<n){ if (pn==n) break; int maxn=INT_MAX,wz=0,wz1=0; for (int i=1; i<=tmp; i++){ for (int j=1; j<=n; j++){ if (p[j]==true&&v[q[i]][j]<maxn&&v[q[i]][j]!=-1){ maxn=v[q[i]][j]; wz=q[i]; wz1=j; } } } if (wz1!=0){ p[wz1]=false; ans+=v[wz][wz1]; pn++; } tmp+=1; q[tmp]=wz1; } if (pn==n) cout<<ans<<endl; else cout<<"orz"<<endl; return 0; } ``` 我也是,WA了7个点,TLE了3个 ~~不如用Kruskal~~
by 面壁者4号 @ 2019-07-27 21:39:11


|