现在的位置: 首页 > 综合 > 正文

HDU3339:In Action(Dijkstra+01背包)

2013年02月16日 ⁄ 综合 ⁄ 共 2716字 ⁄ 字号 评论关闭
Problem Description

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
2 2 3 0 2 9 2 1 3 1 0 2 1 3 2 1 2 1 3 1 3
 

Sample Output
5 impossible

 

题意:给出n,m,代表有n个发电站,m条路

然后给出每条路和该路消耗的油

然后是n行,每行是相应的发电站的能量,要注意,每个发电站必须停一辆坦克才能控制这个发电站,只有控制的总能量超过一半能使其瘫痪

 

思路:因为是求最少油耗,所以是最短路,但是在此路上的各个站都有相应的能量,那么就是看在大于一半的情况下,去不去这个站并取最小油耗的问题,这就是01背包取和不取的问题了 

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int inf = 100000000;
int map[105][105];
int vis[105],cast[105];
int dp[2000000],v[105];
int n,m;

void dijkstra(int s)
{
    int i,j,min,pos;
    memset(vis,0,sizeof(vis));
    for(i = 0; i<=n; i++)
        cast[i] = map[s][i];
    for(i = 0; i<=n; i++)
    {
        min = inf;
        for(j = 0; j<=n; j++)
        {
            if(cast[j]<min && !vis[j])
            {
                pos = j;
                min = cast[j];
            }
        }
        vis[pos] = 1;
        for(j = 0; j<=n; j++)
        {
            if(cast[pos]+map[pos][j]<cast[j] && !vis[j])
                cast[j] = cast[pos]+map[pos][j];
        }
    }
}

int main()
{
    int t,i,j,k,x,y,z;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(i = 0; i<=n; i++)
        {
            for(j = 0; j<=n; j++)
                map[i][j] = inf;
            map[i][i] = 0;
        }
        while(m--)
        {
            scanf("%d%d%d",&x,&y,&z);
            if(z<map[x][y])
                map[x][y] = map[y][x] = z;
        }
        int sum = 0;
        for(i = 1; i<=n; i++)
        {
            scanf("%d",&v[i]);
            sum+=v[i];
        }
        dijkstra(0);//最短路求出0到每个发电站的油耗量
        for(i = 1; i<=sum; i++)
            dp[i] = inf;
        dp[0] = 0;
        for(i = 1; i<=n; i++)//01背包找出最小油耗量
        {
            for(j = sum; j>=v[i]; j--)
                dp[j] = min(dp[j],dp[j-v[i]]+cast[i]);
        }
        x = sum/2+1;//必须大于一半,等于都不行
        int minn = inf;
        for(i = x; i<=sum; i++)
        {
            if(dp[i]<minn)
                minn = dp[i];
        }
        if(minn == inf)
            printf("impossible\n");
        else
            printf("%d\n",minn);
    }

    return 0;
}

抱歉!评论已关闭.