prim算法,但是一直有三个样例过不了,哪位打谁帮小弟看看

P3366 【模板】最小生成树

希望更丰富的展现?使用Markdown
by Hexarhy @ 2019-04-06 22:12:34


这个数据量, $2e5$用$cin + 5000^2$ 很容易炸吧
by Celestial_Scarlet @ 2019-04-06 22:14:35


建议加个快读
by Celestial_Scarlet @ 2019-04-06 22:14:49


@[xumoumou](/space/show?uid=140430)
by Celestial_Scarlet @ 2019-04-06 22:17:52


第一次在上面问问题,没学会排版
by xumoumou @ 2019-04-06 22:20:36


#include<iostream> <br> #include<algorithm> using namespace std; int n,m; int book[5500],e[5500][5500],dis[5500]; const int INF=999999999; void prim() { int sum=0; for(int i=0;i<n-1;i++) { int u=-1,Min=INF; for(int j=1;j<=n;j++) { if(book[j]==0&&Min>dis[j]) { Min=dis[j]; u=j; } } if(u==-1) { cout<<"orz"<<endl; return; } book[u]=1; sum+=dis[u]; for(int j=1;j<=n;j++) { if(book[j]==0&&e[u][j]<INF&&dis[j]>e[u][j]) dis[j]=e[u][j]; } } cout<<sum; } int main() { cin>>n>>m; fill(e[0],e[0]+5050*5050,INF); for(int i=0;i<=n;i++) { e[i][i]=0; } for(int i=0;i<m;i++) { int t1,t2,t3; cin>>t1>>t2>>t3; if(e[t1][t2]>t3) e[t1][t2]=e[t2][t1]=t3; } for(int i=1;i<=n;i++) { dis[i]=e[1][i]; } book[1]=1; prim(); }
by xumoumou @ 2019-04-06 22:33:53


@[HyyypRtf06](/space/show?uid=80049) 我重新弄了一下,能不能帮我看看哪错了,指点我一下,谢谢了
by xumoumou @ 2019-04-06 22:36:09


@[baoyu](/space/show?uid=93465) 谢谢,我试试看,其实我没学过快读,正好学学看
by xumoumou @ 2019-04-06 22:37:24


```cpp include<iostream> include<algorithm> using namespace std; int n,m; int book[5500],e[5500][5500],dis[5500]; const int INF=999999999; void prim() { int sum=0; for(int i=0;i<n-1;i++) { int u=-1,Min=INF; for(int j=1;j<=n;j++) { if(book[j]==0&&Min>dis[j]) { Min=dis[j]; u=j; } } if(u==-1) { cout<<"orz"<<endl; return; } book[u]=1; sum+=dis[u]; for(int j=1;j<=n;j++) { if(book[j]==0&&e[u][j]<INF&&dis[j]>e[u][j]) dis[j]=e[u][j]; } } cout<<sum; } int main() { cin>>n>>m; fill(e[0],e[0]+5050*5050,INF); for(int i=0;i<=n;i++) { e[i][i]=0; } for(int i=0;i<m;i++) { int t1,t2,t3; cin>>t1>>t2>>t3; if(e[t1][t2]>t3) e[t1][t2]=e[t2][t1]=t3; } for(int i=1;i<=n;i++) { dis[i]=e[1][i]; } book[1]=1; prim(); return 0//别忘了 } ``` @[xumoumou](/space/show?uid=140430)
by Hexarhy @ 2019-04-06 22:40:03


其实你可以用scanf
by Hexarhy @ 2019-04-06 22:40:17


| 下一页