HDOJ 4003 Find Metal Mineral

來源:互聯網
上載者:User

標籤:accept   dash   pre   max   collect   tip   metal   表示   res   


題意:

一棵有權樹,從根結點中放入 K 個機器人。求用這 K 個機器人遍曆全部的結點最少的權值和。

思路:

1. dp[u][i] 表示給以 u 為根節點的子樹放 i 個機器人,遍曆其子樹所須要的最小權值。

2. 關鍵在於 dp[u][0] 的理解,表示:最後停留在以 u 為根節點的子樹下 0 個機器人,而且遍曆了 u 子樹的最小權值和。

3. 以下的步驟就變成和分組背包類似的情況了,根節點 u 給孩子 v 放多少個機器人。

4. dp[u][i] = min(dp[u][i], dp[u][j] + dp[v][i-j] + (i-j) * c); 能夠理解成給 v 放了 i-j 個機器人,給 v 的其它兄弟放了 j 個,u 總共同擁有 i 個

Find Metal MineralTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 2354    Accepted Submission(s): 1071


Problem DescriptionHumans have discovered a kind of new metal mineral on Mars which are distributed in point‐like with paths connecting each of them which formed a tree. Now Humans launches k robots on Mars to collect them, and due to the unknown reasons, the landing site S of all robots is identified in advanced, in other word, all robot should start their job at point S. Each robot can return to Earth anywhere, and of course they cannot go back to Mars. We have research the information of all paths on Mars, including its two endpoints x, y and energy cost w. To reduce the total energy cost, we should make a optimal plan which cost minimal energy cost. 
InputThere are multiple cases in the input. 
In each case: 
The first line specifies three integers N, S, K specifying the numbers of metal mineral, landing site and the number of robots. 
The next n‐1 lines will give three integers x, y, w in each line specifying there is a path connected point x and y which should cost w. 
1<=N<=10000, 1<=S<=N, 1<=k<=10, 1<=x, y<=N, 1<=w<=10000. 
OutputFor each cases output one line with the minimal energy cost. 
Sample Input
3 1 11 2 11 3 13 1 21 2 11 3 1
 
Sample Output
32HintIn the first case: 1->2->1->3 the cost is 3;In the second case: 1->2; 1->3 the cost is 2; 
 
SourceThe 36th ACM/ICPC Asia Regional Dalian Site —— Online Contest 


#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn=11000;struct Edge{int to,next,w;}edge[2*maxn];int Adj[maxn],Size;void init(){memset(Adj,-1,sizeof(Adj)); Size=0;}void Add_Edge(int u,int v,int weight){edge[Size].to=v;edge[Size].next=Adj[u];edge[Size].w=weight;Adj[u]=Size++;}int n,S,K;int dp[maxn][20];bool vis[maxn];void dfs(int u){vis[u]=true;for(int i=Adj[u];~i;i=edge[i].next){int v=edge[i].to;int w=edge[i].w;if(vis[v]) continue;dfs(v);for(int j=K;j>=0;j--){dp[u][j]+=dp[v][0]+2*w;for(int jj=0;jj<=j;jj++){dp[u][j]=min(dp[u][j],dp[u][j-jj]+dp[v][jj]+jj*w);}}}}int main(){while(scanf("%d%d%d",&n,&S,&K)!=EOF){init();for(int i=1;i<n;i++){int a,b,w;scanf("%d%d%d",&a,&b,&w);Add_Edge(a,b,w);Add_Edge(b,a,w);}memset(dp,0,sizeof(dp));memset(vis,0,sizeof(vis));dfs(S);printf("%d\n",dp[S][K]);} return 0;}



HDOJ 4003 Find Metal Mineral

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.