哪位大佬帮我看一下代码,谢谢了

P1111 修复公路

希望更丰富的展现?使用Markdown
by yyk504 @ 2019-05-02 20:57:58


希望更丰富的展现?[使用Mark♂down](https://www.luogu.org/wiki/show?name=%E5%B8%AE%E5%8A%A9%EF%BC%9Amarkdown)
by aminoas @ 2019-05-02 20:58:21


@[长江豚](/space/show?uid=179309)
by yyk504 @ 2019-05-02 20:59:37


请不要用Markdown来强调头文件的重要性
by Smile_Cindy @ 2019-05-02 21:11:11


~~请不要使用markdown来强调define的重要性(~~
by wxwoo @ 2019-05-02 21:17:00


```cpp #include<cstdio> #include<cstdlib> #include<iostream> using namespace std; #define maxm 100005 #include <algorithm> int n,m; int pre[1005]; int find (int x) { int r=x; while(r!=pre[r]) { r=pre[r]; } int lx=x; int ly; while(lx!=r) { ly=pre[lx]; pre[lx]=r; lx=ly; } return r; } void join(int x,int y) { int kx=find (x); int ky=find (y); if(kx!=ky) { pre[x]=pre[y]; } } struct note { int x,y,z; }a[10010]; bool cmp(note v,note w) { return v.z < w.z; } int main() { cin>>n>>m; for(int i=1;i<=m;i++) cin>>a[i].x>>a[i].y>>a[i].z; for(int i=1;i<=n;i++) pre[i]=i; sort(a+1,a+1+m,cmp); int side =0; for(int i=1;i<=m;i++) { int fx ,fy ; fx=find(a[i].x); fy=find (a[i].y); if(fx!=fy) { join(a[i].x,a[i].y); side+=a[i].z; } } int ans=0; for(int i=1;i<=n;i++) { if(i==pre[i]) ans++; if(ans>=2) break; } if(ans==1) { cout<<side; } else { cout<<"-1"; } return 0; } ```
by eeesc03 @ 2019-05-02 21:19:30



by 矢信 @ 2019-05-02 21:38:30


这代码。。。有人来分个行吗
by xQAQyn @ 2019-05-02 21:41:01


```c #include<cstdio> #include<cstdlib> #include<iostream> using namespace std; #define maxm 100005 #include <algorithm> int n,m; int pre[1005]; int find (int x) { int r=x; while(r!=pre[r]) { r=pre[r]; } int lx=x; int ly; while(lx!=r) { ly=pre[lx]; pre[lx]=r; lx=ly; } return r; } void join(int x,int y) { int kx=find (x); int ky=find (y); if(kx!=ky) { pre[x]=pre[y]; } } struct note { int x,y,z; } a[10010]; bool cmp(note v,note w) { return v.z < w.z; } int main() { cin>>n>>m; for(int i=1; i<=m; i++) cin>>a[i].x>>a[i].y>>a[i].z; for(int i=1; i<=n; i++) pre[i]=i; sort(a+1,a+1+m,cmp); int side =0; for(int i=1; i<=m; i++) { int fx ,fy ; fx=find(a[i].x); fy=find (a[i].y); if(fx!=fy) { join(a[i].x,a[i].y); side+=a[i].z; } } int ans=0; for(int i=1; i<=n; i++) { if(i==pre[i]) ans++; if(ans>=2) break; } if(ans==1) { cout<<side; } else { cout<<"-1"; } return 0; } ``` 是这样的吧。。。
by 厂长 @ 2019-05-02 21:48:27


@[长江豚](/space/show?uid=179309) 吐槽一下; 能不能把 ```cpp using namespace std; ``` 打在 ```cpp #include <ios> #include <map> #include <set> #include <list> #include <stack> #include <deque> #include <cmath> #include <queue> #include <cstdio> #include <cctype> #include <iosfwd> #include <bitset> #include <limits> #include <vector> #include <cwchar> #include <string> #include <cerrno> #include <utility> #include <clocale> #include <iomanip> #include <complex> #include <fstream> #include <cstdlib> #include <cstring> #include <ostream> #include <istream> #include <sstream> #include <cwctype> #include <iostream> #include <algorithm> #include <stdexcept> #include <streambuf> #include <exception>  #include <functional> ``` 以及 ```cpp #define ...... #define ...... ``` 的后面。 这是个好习惯。。。。
by G_M_H @ 2019-05-02 23:16:02


| 下一页