#include<iostream>
#include<stdio.h>
#include<string>
#include<map>
#include<iomanip>
#define MAXN 500
#define inf 1000000000
typedef double elem_t;
using namespace std; double _m[MAXN][MAXN];
int pre[MAXN];
map<string,int> coll;
map<string,int>::iterator pos1,pos2; elem_t prim(int n,elem_t mat[][MAXN],int* pre){
elem_t min[MAXN],ret=;
int v[MAXN],i,j,k;
for (i=;i<n;i++)
min[i]=inf,v[i]=,pre[i]=-;
for (min[j=]=;j<n;j++){
for (k=-,i=;i<n;i++)
if (!v[i]&&(k==-||min[i]<min[k]))
k=i;
for (v[k]=,ret+=min[k],i=;i<n;i++)
if (!v[i]&&mat[k][i]<min[i])
min[i]=mat[pre[i]=k][i];
}
return ret;
} int main()
{
//freopen("acm.acm","r",stdin);
double st;
int i;
int j;
double ans;
int num_name;
int edge;
string s;
string s1;
double len;
for(i = ; i < MAXN ; ++ i)
{
for(j = ; j < MAXN; ++ j)
_m[i][j] = inf;
}
cin>>st;
cin>>num_name;
for(i = ; i < num_name; ++ i)
{
cin>>s;
coll.insert(pair<string,int>(s,i));
}
cin>>edge;
for(i = ; i < edge; ++ i)
{
cin>>s;
cin>>s1;
cin>>len;
pos1 = coll.find(s);
pos2 = coll.find(s1);
_m[pos1->second][pos2->second] = len;
_m[pos2->second][pos1->second] = len;
}
if((ans = prim(num_name,_m,pre)) > st)
cout<<"Not enough cable "<<endl;
else
cout<<"Need "<<setiosflags(ios::fixed)<<setprecision()<<ans<<" miles of cable"<<endl;
}
关注我的公众号,当然,如果你对Java, Scala, Python等技术经验,以及编程日记,感兴趣的话。
技术网站地址: vmfor.com