标签:
题意:
Nick‘s company employed n people. Now Nick needs to build a tree hierarchy of «supervisor-surbodinate» relations in the company (this is to say that each employee, except one, has exactly one supervisor). There are m applications written in the following form: «employeeai is ready to become a supervisor of employee bi at extra cost ci». The qualification qj of each employee is known, and for each application the following is true: qai > qbi.
Would you help Nick calculate the minimum cost of such a hierarchy, or find out that it is impossible to build it.
思路:
implementation题,,【要先找出根】
代码:
int n,m; int q[1005]; int g[1005][1005]; int ans[1005]; int fa[1005]; int main(){ cin>>n; rep(i,1,n) cin>>q[i]; cin>>m; int temp=-inf; int head; rep(i,1,n){ if(q[i]>temp){ temp=q[i]; head=i; } } mem(g,inf); mem(ans,inf); rep(i,1,m){ int a,b,c; cin>>a>>b>>c; g[a][b]=min(g[a][b],c); } rep(i,1,n){ rep(j,1,n){ if(g[j][i]<ans[i]){ ans[i]=g[j][i]; fa[i]=j; } } } int res=0; rep(i,1,n){ if(i==head) continue; if(ans[i]==inf){ puts("-1"); ret 0; } res+=g[fa[i]][i]; } print("%d\n",res); return 0; }
标签:
原文地址:http://www.cnblogs.com/fish7/p/4329916.html