http://poj.org/problem?id=1258
多么单纯的mst!
#include<iostream> #include<string.h> using namespace std; const int INF=103; int graph[INF][INF]; int f1[INF]; int low[INF]; int res,n; int prim() { f1[1]=1; memset(low,0,sizeof(low)); for(int i=1; i<=n; i++) { low[i]=graph[1][i]; } int loc,MIN=0x1f1f1f1f; for(int i=1; i<n; i++) { MIN=0x1f1f1f1f; for(int j=1; j<=n; j++) { if(!f1[j]&&low[j]<MIN) { loc=j; MIN=low[j]; } } f1[loc]=1; res+=MIN; for(int i=1; i<=n; i++) { if(!f1[i]&&graph[loc][i]<low[i]) { low[i]=graph[loc][i]; } } } return res; } int main() { while( cin>>n) { res=0; memset(graph,0,sizeof(graph)); memset(f1,0,sizeof(f1)); for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) cin>>graph[i][j]; } cout<<prim()<<endl; } return 0; }
http://write.blog.csdn.net/postedit
同一个婊子~没意思!
#include<iostream> #include<string.h> using namespace std; const int INF=103; int graph[INF][INF]; int f1[INF]; int low[INF]; int res,n; int prim() { f1[1]=1; memset(low,0,sizeof(low)); for(int i=1; i<=n; i++) { low[i]=graph[1][i]; } int loc,MIN=0x1f1f1f1f; for(int i=1; i<n; i++) { MIN=0x1f1f1f1f; for(int j=1; j<=n; j++) { if(!f1[j]&&low[j]<MIN) { loc=j; MIN=low[j]; } } f1[loc]=1; res+=MIN; for(int i=1; i<=n; i++) { if(!f1[i]&&graph[loc][i]<low[i]) { low[i]=graph[loc][i]; } } } return res; } int main() { while( cin>>n) { res=0; memset(graph,0,sizeof(graph)); memset(f1,0,sizeof(f1)); for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) cin>>graph[i][j]; } cout<<prim()<<endl; } return 0; }
poj 1258 Agri-Net poj 2485 Highways
原文地址:http://blog.csdn.net/lsgqjh/article/details/46277743