您的位置:首页 > 其它

Find Metal Mineral - HDU 4003 dp

2014-10-13 15:58 375 查看


Find Metal Mineral

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)

Total Submission(s): 2530 Accepted Submission(s): 1142



Problem Description

Humans 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.



Input

There 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.



Output

For each cases output one line with the minimal energy cost.



Sample Input

3 1 1
1 2 1
1 3 1
3 1 2
1 2 1
1 3 1




Sample Output

3
2

HintIn the first case: 1->2->1->3 the cost is 3;
In the second case: 1->2; 1->3 the cost is 2;




题意:在一个n个节点的树上,你开始时在s点有k个机器人,问遍历所有的节点后,走的最短距离是多少。

思路:dp[i][j]表示i节点用了j个机器人遍历了i节点下的所有点所走的距离。dp[i][0]就代表一个机器人下去遍历后又回来。我们可以推断出最多相当于会有1个下去的机器人再上来,因为如果有两个下去,又都上来的话,和一个是一样的。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
int n,s,k;
ll dp[10010][15];
struct node
{
    int y,w;
};
vector<node> vc[10010];
void dfs(int f,int u)
{
    int i,j,p,v,w,len=vc[u].size(),a,b;
    for(i=0;i<len;i++)
    {
        v=vc[u][i].y;
        w=vc[u][i].w;
        if(v==f)
          continue;
        dfs(u,v);
        for(j=k;j>=1;j--)
        {
            dp[u][j]+=dp[v][0]+w*2;
            for(p=1;p<=j;p++)
               dp[u][j]=min(dp[u][j],dp[u][j-p]+dp[v][p]+w*p);
        }
        dp[u][0]+=dp[v][0]+w*2;
    }
}
int main()
{
    int i,j,u,v,w;
    ll ans;
    node A;
    while(~scanf("%d%d%d",&n,&s,&k))
    {
        if(n==1)
        {
            printf("0\n");
            continue;
        }
        for(i=1;i<=n;i++)
           vc[i].clear();
        memset(dp,0,sizeof(dp));
        for(i=1;i<n;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            A.w=w;
            A.y=v;
            vc[u].push_back(A);
            A.y=u;
            vc[v].push_back(A);
        }
        dfs(-1,s);
        ans=dp[s][0];
        for(i=1;i<=k;i++)
           ans=min(ans,dp[s][i]);
        printf("%I64d\n",ans);
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: