POJ 2075
#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=0; int v[MAXN],i,j,k; for (i=0;i<n;i++) min[i]=inf,v[i]=0,pre[i]=-1; for (min[j=0]=0;j<n;j++){ for (k=-1,i=0;i<n;i++) if (!v[i]&&(k==-1||min[i]<min[k])) k=i; for (v[k]=1,ret+=min[k],i=0;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 = 0; i < MAXN ; ++ i) { for(j = 0; j < MAXN; ++ j) _m[i][j] = inf; } cin>>st; cin>>num_name; for(i = 0; i < num_name; ++ i) { cin>>s; coll.insert(pair<string,int>(s,i)); } cin>>edge; for(i = 0; 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(1)<<ans<<" miles of cable"<<endl; }
关注我的公众号,当然,如果你对Java, Scala, Python等技术经验,以及编程日记,感兴趣的话。
技术网站地址: vmfor.com