4.11
#include<iostream> #include<cstring> int map[101][101],minn[100],MST,vis[101]; using namespace std; int main() { memset(map,0x7f,sizeof(map)); int n,e,x,y,w; cin>>n>>e; for(int i=0;i<e;i++) { cin>>x>>y>>w; map[x][y]=map[y][x]=w; } memset(minn,0x7f,sizeof(minn)); minn[1]=0; for(int i=1;i<=n;i++) { int k=0; for(int j=1;j<=n;j++) if(!vis[j]&&minn[j]<minn[k]) k=j; vis[k]=true; for(int j=1;j<=n;j++) if(!vis[j]&&map[k][j]<minn[j]) minn[j]=map[k][j]; } for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(map[i][j]==minn[i])cout<<j<<" "<<i<<endl; return 0; }
4.12
#include<iostream> #include<cstring> #include<cstdio> using namespace std; int n,map[105][105]; long long minn[105]; bool vis[101]; int main() { freopen("wire.in","r",stdin); freopen("wire.out","w",stdout); cin>>n; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) cin>>map[i][j]; memset(minn,0x7f,sizeof(minn)); minn[1]=0; for(int i=1;i<=n;i++) { int k=0; for(int j=1;j<=n;j++) if(!vis[j]&&minn[j]<minn[k]) k=j; vis[k]=true; for(int j=1;j<=n;j++) if(!vis[j]&&map[k][j]<minn[j]) minn[j]=map[k][j]; } int tot=0; for(int i=1;i<=n;i++) tot+=minn[i]; cout<<tot<<endl; return 0; }