首页 > ACM题库 > HDU-杭电 > HDU 3339-In Action-背包问题-[解题报告]HOJ
2014
03-16

HDU 3339-In Action-背包问题-[解题报告]HOJ

In Action

问题描述 :

Kakuro Extension
Since 1945, when the first nuclear bomb was exploded by the Manhattan Project team in the US, the number of nuclear weapons have soared across the globe.
Nowadays,the crazy boy in FZU named AekdyCoin possesses some nuclear weapons and wanna destroy our world. Fortunately, our mysterious spy-net has gotten his plan. Now, we need to stop it.
But the arduous task is obviously not easy. First of all, we know that the operating system of the nuclear weapon consists of some connected electric stations, which forms a huge and complex electric network. Every electric station has its power value. To start the nuclear weapon, it must cost half of the electric network’s power. So first of all, we need to make more than half of the power diasbled. Our tanks are ready for our action in the base(ID is 0), and we must drive them on the road. As for a electric station, we control them if and only if our tanks stop there. 1 unit distance costs 1 unit oil. And we have enough tanks to use.
Now our commander wants to know the minimal oil cost in this action.

输入:

The first line of the input contains a single integer T, specifying the number of testcase in the file.
For each case, first line is the integer n(1<= n<= 100), m(1<= m<= 10000), specifying the number of the stations(the IDs are 1,2,3…n), and the number of the roads between the station(bi-direction).
Then m lines follow, each line is interger st(0<= st<= n), ed(0<= ed<= n), dis(0<= dis<= 100), specifying the start point, end point, and the distance between.
Then n lines follow, each line is a interger pow(1<= pow<= 100), specifying the electric station’s power by ID order.

输出:

The first line of the input contains a single integer T, specifying the number of testcase in the file.
For each case, first line is the integer n(1<= n<= 100), m(1<= m<= 10000), specifying the number of the stations(the IDs are 1,2,3…n), and the number of the roads between the station(bi-direction).
Then m lines follow, each line is interger st(0<= st<= n), ed(0<= ed<= n), dis(0<= dis<= 100), specifying the start point, end point, and the distance between.
Then n lines follow, each line is a interger pow(1<= pow<= 100), specifying the electric station’s power by ID order.

样例输入:

2
2 3
0 2 9
2 1 3
1 0 2
1
3
2 1
2 1 3
1
3

样例输出:

5
impossible

#include <iostream>
#include <cstring>
#include <cstdio>
#define inf 99999999
using namespace std;
int map[105][105];
int d[105],n;
int v[105];
int dp[100005];
void dijkstra()
{
    int i;
    int vis[105];
    memset(vis,0,sizeof(vis));
    for(i=0;i<=n;i++)
        d[i]=inf;
    d[0]=0;
    for(i=0;i<=n;i++)
    {
        int x,y,m=inf;
        for(y=0;y<=n;y++)
            if(d[y]<m&&!vis[y])
                m=d[x=y];
        vis[x]=1;
        for(y=0;y<=n;y++)
        {
            if(map[x][y]!=-1&&d[y]>d[x]+map[x][y])
                d[y]=d[x]+map[x][y];
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int m;
        scanf("%d%d",&n,&m);
        int i;
        memset(map,-1,sizeof(map));
        for(i=0;i<m;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            if(map[u][v]==-1||w<map[u][v])
                map[u][v]=map[v][u]=w;    //有重边,一定要细心额~~
        }
        int s=0;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&v[i]);
            s=s+v[i];
        }
        dijkstra();
        for(i=0;i<=s;i++)            //先是写成i 0-n~~~
            dp[i]=inf;
        dp[0]=0;
        int j;
        for(i=1;i<=n;i++)
        {
            for(j=s;j>=v[i];j--)
            {
                dp[j]=min(dp[j],dp[j-v[i]]+d[i]);
            }
        }
        int mm;
        mm=s/2+1;             //是大于一般 不是大于等于~~~额~汗 太不仔细了
        int min=inf;
        for(i=mm;i<=s;i++)
        {
            if(dp[i]<min)       //先是dp[i]!=inf break;后来仔细想想不对,
                 min=dp[i];     //有可能i大但dp[i]小~!
                                //原因是dp[i]表示恰好装满i最小价值,
                                //有可能dp[i]没有恰好装满导致dp[i]=inf,
                                //而dp[i+1]恰好装满dp[i+1]<inf!!

        }
        if(min==inf)
            printf("impossible\n");
        else
            printf("%d\n",min);

    }
    return 0;
}

参考:http://blog.csdn.net/juststeps/article/details/8780491


  1. 很高兴你会喜欢这个网站。目前还没有一个开发团队,网站是我一个人在维护,都是用的开源系统,也没有太多需要开发的部分,主要是内容整理。非常感谢你的关注。

  2. 我没看懂题目
    2
    5 6 -1 5 4 -7
    7 0 6 -1 1 -6 7 -5
    我觉得第一个应该是5 6 -1 5 4 输出是19 5 4
    第二个是7 0 6 -1 1 -6 7输出是14 7 7
    不知道题目例子是怎么得出来的

  3. #include <cstdio>
    #include <algorithm>

    struct LWPair{
    int l,w;
    };

    int main() {
    //freopen("input.txt","r",stdin);
    const int MAXSIZE=5000, MAXVAL=10000;
    LWPair sticks[MAXSIZE];
    int store[MAXSIZE];
    int ncase, nstick, length,width, tmp, time, i,j;
    if(scanf("%d",&ncase)!=1) return -1;
    while(ncase– && scanf("%d",&nstick)==1) {
    for(i=0;i<nstick;++i) scanf("%d%d",&sticks .l,&sticks .w);
    std::sort(sticks,sticks+nstick,[](const LWPair &lhs, const LWPair &rhs) { return lhs.l>rhs.l || lhs.l==rhs.l && lhs.w>rhs.w; });
    for(time=-1,i=0;i<nstick;++i) {
    tmp=sticks .w;
    for(j=time;j>=0 && store >=tmp;–j) ; // search from right to left
    if(j==time) { store[++time]=tmp; }
    else { store[j+1]=tmp; }
    }
    printf("%dn",time+1);
    }
    return 0;
    }

  4. 第一句可以忽略不计了吧。从第二句开始分析,说明这个花色下的所有牌都会在其它里面出现,那么还剩下♠️和♦️。第三句,可以排除2和7,因为在两种花色里有。现在是第四句,因为♠️还剩下多个,只有是♦️B才能知道答案。