您的位置:首页 > 其它

poj 2705 Tangled in Cables 基础prim

2011-10-08 10:12 141 查看
基础prim

#include<iostream>
using namespace std;
char a[2000][25];
int n,m;
char ta[25],tb[25];
double map[1509][1509];
bool vis[2000];
double dis[2000];
double prim()
{
double sum;
for(int i=1;i<=n;i++)
{
double cf=9999999.0;//特别注意啊!!这个我已开始就用0x7FFFFFFF了,就是wa,拉无语了,最后改成小数就过了!!!
int temp;
for(int j=1;j<=n;j++)
{
if(!vis[j]&&dis[j]<cf)
{
cf=dis[j];
temp=j;
}
}
if(cf==9999999.0)
cf=0;
sum+=cf;
vis[temp]=true;
for(int j=1;j<=n;j++)
{
if(!vis[j]&&dis[j]>map[temp][j])
dis[j]=map[temp][j];
}
}
return sum;
}
int main()
{
// cout<<"haha\n";
// system("pause");
double l,tc;
while(scanf("%lf",&l)!=EOF)
{
memset(vis,false,sizeof(vis));
memset(map,9999999.0,sizeof(map));
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",a[i]);
}
scanf("%d",&m);
while(m--)
{

scanf("%s%s%lf",ta,tb,&tc);
int tempa,tempb;
for(int i=1;i<=n;i++)
{
if(strcmp(ta,a[i])==0)
tempa=i;
if(strcmp(tb,a[i])==0)
tempb=i;
}
map[tempa][tempb]=map[tempb][tempa]=tc;
}
for(int i=1;i<=n;i++)
dis[i]=map[1][i];
vis[1]=true;
double t=prim();
if(t<=l)
printf("Need %.1lf miles of cable\n",t);
else
printf("Not enough cable\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: