| ||||||||||
| 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 | |||||||||
我为啥用Prim不能AC如题,总说WA。
牛人帮我看下把
#include<iostream>
using namespace std;
int main()
{
int n,m;
while(cin>>n>>m)
{
int i,j,k=0,u,v;
long l,max,min;
long **a=new long *[m];
for(i=0;i<m;i++)
a[i]=new long [3];
long **b=new long *[n+1];
for(i=0;i<n+1;i++)
b[i]=new long [n+1];
for(i=1;i<n+1;i++)
for(j=1;j<n+1;j++)
b[i][j]=0;
for(i=0;i<m;i++)
{
cin>>u>>v>>l;
b[u][v]=b[v][u]=l;
}
for(i=1;i<n+1;i++)
for(j=1;j<n+1;j++)
if(b[i][j]==0)
b[i][j]=2147483647;
bool *s=new bool [n+1];
long *lowcost=new long [n+1];
int *closest=new int [n+1];
s[1]=true;
for(i=2;i<=n;i++){
lowcost[i]=b[1][i];
closest[i]=1;
s[i]=false;
}
for(i=1;i<n;i++){
min=2147483647;
j=1;
for(k=2;k<=n;k++)
if((lowcost[k]<min)&&(!s[k])){
min=lowcost[k];
j=k;
}
s[j]=true;
for(k=2;k<=n;k++)
if((b[j][k]<lowcost[k])&&(!s[k])){
lowcost[k]=b[j][k];
closest[k]=j;
}
}
max=0;
for(i=2;i<=n;i++)
if(s[i]==true&&lowcost[i]>max)
max=lowcost[i];
cout<<max<<endl;
}
return 0;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator