标签:namespace mes href ref for long www https 最小
https://www.luogu.com.cn/problem/P3366
1 #define IO std::ios::sync_with_stdio(0) 2 #include <bits/stdc++.h> 3 #define pb push_back 4 using namespace std; 5 typedef long long ll; 6 const int N=5005; 7 const int inf=2147483647; 8 9 int n,m; 10 11 struct node{ 12 int x,dis; 13 }; 14 15 vector<node>G[N]; 16 17 int vis[N],d[N]; 18 19 int Prim(){ 20 fill(d+1,d+1+n,inf); 21 d[1]=0; 22 int ans=0; 23 for(int i=1;i<=n;i++){ 24 int mi=inf; 25 int u=-1; 26 for(int j=1;j<=n;j++){ 27 if(!vis[j]&&d[j]<mi){ 28 mi=d[j]; 29 u=j; 30 } 31 } 32 if(u==-1)return -1; 33 vis[u]=1; 34 ans+=d[u]; 35 for(int j=0;j<G[u].size();j++){ 36 int v=G[u][j].x; 37 if(!vis[v]&&G[u][j].dis<d[v])d[v]=G[u][j].dis; 38 } 39 } 40 return ans; 41 } 42 43 int main(){ 44 IO; 45 cin>>n>>m; 46 for(int i=1;i<=m;i++){ 47 int u,v,w; 48 cin>>u>>v>>w; 49 G[u].pb((node){v,w}); 50 G[v].pb((node){u,w}); 51 } 52 int ans=Prim(); 53 if(ans==-1)cout<<"orz"<<endl; 54 else cout<<ans<<endl; 55 }
标签:namespace mes href ref for long www https 最小
原文地址:https://www.cnblogs.com/ccsu-kid/p/14025736.html