欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

HDU 3339 In Action (01背包+最短路)

程序员文章站 2024-03-17 14:26:28
...
Problem Description
HDU 3339 In Action (01背包+最短路)
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.
 

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

Output
The minimal oil cost in this action.
If not exist print "impossible"(without quotes).
 

Sample Input

22 30 2 92 1 31 0 2132 12 1 313
 

Sample Output

5impossible
 

        题目大意:给出n个城市,以及他们在之间的m条道路上行驶时的油耗,同时每个城市有一个能量值,求从起点(n个城市之外的点)出发攻占城市后一直到获得的能量值超过半数时所需要的最小油耗,注意这里有无限辆坦克,且城市被攻占的条件是有坦克驻守,所以每次攻打城市时都要从起点出发。

        整个大问题可以分为两个小问题,①、求出起点到所有城市的最短(距离)油耗,②、从n个城市中选择几个城市使得到的能量值大于n/2,同时油耗尽量少。

        对于问题①,最短路求解即可;问题②,转换为求当前油耗下获得的最大能量,01背包。

        怎么感觉对于题目的精髓,总是把握不好呢。。。

        AC代码:

#include<bits/stdc++.h>
using namespace std;
bool u[110];
int dis[110];
int w[110][110];
int p[101];
int f[10010];
int n,m;
void dijktra(int s)
{
    memset(dis,0x7f,sizeof(dis));
    memset(u,0,sizeof(u));
    dis[s]=0;
    for(int i=0;i<=n;++i)
    {
        int k=0,an=99999999;
        for(int j=0;j<=n;++j)
        {
            if(!u[j]&&dis[j]<an)an=dis[j],k=j;
        }
        u[k]=1;
        for(int j=0;j<=n;++j)
        {
            if(!u[j])
            {
                dis[j]=min(dis[j],dis[k]+w[k][j]);
            }
        }
    }
}
int main()
{
    //freopen("1.txt","r",stdin);
    int t;
    cin>>t;

    while(~scanf("%d%d",&n,&m))
    {
        memset(f,0,sizeof(f));
        memset(w,0x7f,sizeof(w));
        memset(p,0,sizeof(p));
        for(int i=0;i<=n;++i)w[i][i]=0;
        for(int i=1;i<=m;++i)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            if(z<w[x][y])   //******最短路构图中易错点之一;
                w[x][y]=w[y][x]=z;
        }
        int sum=0;
        for(int i=1;i<=n;++i)scanf("%d",&p[i]),sum+=p[i];
        sum/=2;
        dijktra(0);
        int v=0;
        for(int i=1;i<=n;++i)
            if(dis[i]<0x7f)v+=dis[i];//******
        for(int i=1;i<=n;++i)
        {
            for(int j=v;j>=dis[i];--j)
            {
                f[j]=max(f[j],f[j-dis[i]]+p[i]);
            }
        }
        bool flag=0;
        for(int i=0;i<=v;++i)
        {
            if(f[i]>sum)
            {
                flag=1;
                printf("%d\n",i);
                break;
            }
        }
        if(!flag)printf("impossible\n");
    }
    return 0;
}