| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
这么简单的题目为什么给我WA??难道我PRIM错了???#include <iostream>
#include <fstream>
using namespace std;
int n;
int mark[110];
int cost[110][110];
int total;
void prim(){
mark[1]=1;
int dest[110];
int i,j,k;
for(i=2;i<=n;i++){
dest[i]=cost[1][i];
}
for(i=1;i<n;i++){
int min=1000000;
int node=-1;
for(j=1;j<=n;j++){
if(mark[j]==0&&dest[j]<min){
min=dest[j];
node=j;
}
}
mark[node]=1;
total+=min;
for(j=1;j<=n;j++){
if(mark[j]==0&&dest[j]>cost[node][j]){
dest[j]=cost[node][j];
}
}
}
}
void main(){
// ifstream cin("data.txt");
while(cin>>n){
int i,j,k;
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
cin>>cost[i][j];
}
}
total=0;
prim();
cout<<total<<endl;
}
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator