输入文件in.txt,输出文件ans.txt,都与代码项目同目录
#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
struct node{
vector<double> w;
vector<int> ind;
};
const int maxi = 50;
double v[maxi][maxi]; //v[i][j]表示行业i和j的相关性
map<string, node> company;
int main(){
freopen("in.txt", "r", stdin);
freopen("ans.txt", "w", stdout);
for(int i=0; i<1806; i++){
int ind1, ind2;
double va;
cin>>ind1>>ind2>>va;
v[ind1][ind2] = v[ind2][ind1] = va;
}
for(int i=0; i<3727; i++){
string code;
double weight;
int industry;
cin>>code>>weight>>industry;
company[code].w.push_back(weight);
company[code].ind.push_back(industry);
}
map<string, node>::iterator it;
for(it=company.begin(); it!=company.end(); it++){
double up=0, ans;
int n = (it->second).w.size();
if(n==1) ans = 0;
else{
for(int k=0; k<n; k++){
double wei=0;
for(int t=0; t<n; t++){
if(t==k) continue;
wei += v[(it->second).ind[k]][(it->second).ind[t]];
}
up += (it->second).w[k] * wei;
}
ans = up / (n-1);
}
cout<<(it->first)<<' '<<ans<<endl;
}
fclose(stdin);
fclose(stdout);
return 0;
}