西电训练赛B题
#include <iostream> #include <cstdio> #include <algorithm> #include <vector> using namespace std; struct edge{ int to,cost; }; struct edge2{ int id,from,to,cost,shiyongquanzhi; double savetime; }; bool used[10003]; edge2 record[10003]; vector<edge> g[10003]; int tongji[10003]; int shendu[10003]; int tongjisum[10003]; bool cmp(edge2 a,edge2 b) { return a.savetime>b.savetime; } int dfs(int v) { if(used[v]) return shendu[v]; else { used[v]=true; int tmp=1; for(int i=0;i<g[v].size();i++) { if(!used[g[v][i].to]) { tmp+=dfs(g[v][i].to); } else continue; } shendu[v]=tmp; return tmp; } } int main() { int n,k,sk,sc; cin>>n>>k>>sk>>sc; for(int i=1;i<=n-1;i++) { int from,to,quan; scanf("%d%d%d",&from,&to,&quan); g[from].push_back((edge){to,quan}); g[to].push_back((edge){from,quan}); struct edge2 data; data.id=i; data.from=from; data.to=to; data.cost=quan; data.shiyongquanzhi=0; data.savetime=0; record[i]=data; } for(int i=0;i<=10002;i++) shendu[i]=0; dfs(1); for(int i=1;i<=n-1;i++) { int da=shendu[record[i].from]>shendu[record[i].to]?shendu[record[i].from]:shendu[record[i].to]; int xiao=shendu[record[i].from]<shendu[record[i].to]?shendu[record[i].from]:shendu[record[i].to]; int a1=n-xiao-1; int a2=xiao-1; record[i].shiyongquanzhi=a1*a2+n-1; record[i].savetime=1.0*record[i].shiyongquanzhi*(1.0*record[i].cost/sk-1.0*record[i].cost/sc); } sort(record+1,record+n,cmp); for(int i=1;i<=k;i++) printf("%d\n",record[i].id); }