洛谷 P1546 最短网络 Agri-Net
作者:互联网
题目传送门
解题思路:
一道裸的最小生成树,题解传送门
AC代码:
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 5 using namespace std; 6 7 int n,g[101][101],dis[101],ans; 8 bool p[101]; 9 10 void Prim() { 11 dis[1] = 0; 12 p[1] = true; 13 for(int i = 1;i < n; i++) { 14 int k = 0; 15 for(int j = 1;j <= n; j++) 16 if(!p[j] && dis[j] < dis[k]) 17 k = j; 18 p[k] = true; 19 ans += dis[k]; 20 for(int j = 1;j <= n; j++) 21 dis[j] = min(dis[j],g[k][j]); 22 } 23 } 24 25 int main() 26 { 27 memset(g,0x3f,sizeof(g)); 28 scanf("%d",&n); 29 for(int i = 1;i <= n; i++) 30 for(int j = 1;j <= n; j++) 31 scanf("%d",&g[i][j]); 32 for(int i = 0;i <= n; i++) 33 dis[i] = g[1][i]; 34 Prim(); 35 printf("%d",ans); 36 return 0; 37 }
标签:include,洛谷,int,题解,P1546,传送门,Agri,101,dis 来源: https://www.cnblogs.com/lipeiyi520/p/11286127.html