HDU 5148(Cities-樹上背包),hducities-

來源:互聯網
上載者:User

HDU 5148(Cities-樹上背包),hducities-

CitiesTime Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 670    Accepted Submission(s): 198


Problem DescriptionLong long ago,there is a knight called JayYe.He lives in a small country.This country is made up of n cities connected by n-1 roads(that means it's a tree).The king wants to reward JayYe because he beats the devil and save the princess.The king decide to give JayYe exactly K cities as his daughter's dowry.
Here comes the question.Although JayYe beats the devil,his knee was injured.So he doesn't want to move too much,he wants his citys as close as possible,that is, JayYe wants the expected distance of his cities as small as possible.
The expected distance is defined as the expected distance between node u and node v,both u and v are randomly choose from JayYe's K cities equiprobably(that means first choose u randomly from JayYe’s K cities,then choose v randomly from JayYe’s K cities,so the case u equals to v is possible).
Suppose you are the king,please determine the K cities so that JayYe is happy.
Because the author is lazy,you only need tell me the minimum expect distance. 
InputThe first line contains a single integer T,indicating the number of test cases.
Each test case begins with two integers n and K,indicating the number of cities in this country,the number of cities the king gives to the knight.Then follows n-1 lines,each line contains three integers a,b,and c, indicating there is a road connects city a and city b with length c.

[Technical Specification]
1 <= T <= 100
1 <= K <= min(50,n)
1 <= n <= 2000
1 <= a,b <= n
0 <= c <= 100000 
OutputFor each case, output one line, contain one integer, the minimum expect distance multiplyK2. 
Sample Input
12 21 2 1
 
Sample Output
2
 
SourceBestCoder Round #23 
Recommendheyang   |   We have carefully selected several similar problems for you:  5231 5230 5229 5228 5227  

易證樹上所取子圖連通

令dp[i,j]表示子樹i取j個節點

則對u樹的子樹v  u-v 所取次數=2*子樹v所取節點數*(K-子樹v所取節點數)  K同題意

那麼它滿足 最優子結構


#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<functional>#include<iostream>#include<vector>using namespace std;#define For(i,n) for(int i=1;i<=n;i++)#define Fork(i,k,n) for(int i=k;i<=n;i++)#define Rep(i,n) for(int i=0;i<n;i++)#define ForD(i,n) for(int i=n;i;i--)#define RepD(i,n) for(int i=n;i>=0;i--)#define Forp(x) for(int p=pre[x];p;p=next[p])#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  #define Lson (x<<1)#define Rson ((x<<1)+1)#define MEM(a) memset(a,0,sizeof(a));#define MEMI(a) memset(a,127,sizeof(a));#define MEMi(a) memset(a,128,sizeof(a));#define F (100000007)#define MAXN (2000+10)#define MAXK (100+10)#define MAXc (100000+10) #define INF (1000000000000LL)typedef long long ll;int n,k;ll dp[MAXN][MAXK];vector<int> edge[MAXN],dis[MAXN];void dfs(int u,int fa){int sz=edge[u].size();Rep(i,sz){int v=edge[u][i];ll d=dis[u][i];if (v!=fa){dfs(v,u);ForD(j,k){ForD(t,j-1) //至少取1個節點 dp[u][j]=min(dp[u][j],dp[u][t]+dp[v][j-t]+(j-t)*(k-(j-t))*d*2);}}}}int main(){//freopen("1003.in","r",stdin);//freopen(".out","w",stdout);int T;scanf("%d",&T);while(T--){scanf("%d%d",&n,&k);For(i,n) dis[i].resize(0);For(i,n) edge[i].resize(0);For(i,n-1){int a,b,c;scanf("%d%d%d",&a,&b,&c);edge[a].push_back(b);edge[b].push_back(a);dis[a].push_back(c);dis[b].push_back(c);}MEM(dp)For(i,n)Fork(j,2,k){dp[i][j]=(ll)INF;}dfs(1,-1);ll ans=INF;For(i,n) ans=min(ans,dp[i][k]);printf("%I64d\n",ans);}return 0;}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.