HDU 3339 In Action【最短路+01背包】

题目链接:【http://acm.hdu.edu.cn/showproblem.php?pid=3339】

In Action

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5951    Accepted Submission(s): 1998


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
 

 

Author
Lost@HDU
 

 

Source
 

 

Recommend
lcy
题意:给出N个供电站编号从1~N,然后给出M条边:基地(编号为0)到某些供电站之间的距离和供电站之间的距离,并且给出这N个供电站的电量。每辆坦克从基地出发去攻击供电站,并且每辆坦克只能攻击一个供电站,求要使的破坏总电量的一半以,求这些坦克要走的最短距离。
题解:最短路+01背包。
#include<bits/stdc++.h>
using namespace std;
const int INF =  1e7 + 15;
const int maxn = 10050;
int N, M;
int mp[150][150], val[150];
int dis[150], inq[150];
void SPFA(int st)
{
    for(int i = 1; i <= N; i++)
        dis[i] = INF, inq[i] = 0;
    dis[st] = 0;
    queue<int> que;
    que.push(st);
    inq[st] = 1;
    while(!que.empty())
    {
        int u = que.front();
        inq[u] = 0;
        que.pop();
        for(int v = 0; v <= N; v++)
        {
            if(v == u) continue;
            if(dis[v] > dis[u] + mp[u][v])
            {
                dis[v] = dis[u] + mp[u][v];
                if(!inq[v]) que.push(v), inq[v] = 1;
            }
        }
    }
}
int main ()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d", &N, &M);
        for(int i = 0; i <= N; i++)
        {
            for(int j = 0; j <= N; j++)
                mp[i][j] = INF;
            mp[i][i] = 0;
        }
        int u, v, len;
        for(int i = 1; i <= M; i++)
        {
            scanf("%d%d%d", &u, &v, &len);
            if(mp[u][v] > len) mp[u][v] = mp[v][u] = len;
        }
        SPFA(0);
        int sum = 0;
        for(int i = 1; i <= N; i++)
            scanf("%d", &val[i]), sum += val[i];
        int dp[sum + 5];
        for(int i = 1; i <= sum; i++)
            dp[i] = INF;
        dp[0] = 0;
        for(int i = 1; i <= N; i++)
            for(int j = sum; j >= val[i]; j--)
                dp[j] = min(dp[j], dp[j - val[i]] + dis[i]);
        int ans = INF;
        for(int i = sum / 2 + 1; i <= sum; i++)
            ans = min(ans, dp[i]);
        if(ans == INF)
            printf("impossible\n");
        else
            printf("%d\n", ans);
    }
    return 0;
}

 

posted @ 2017-04-07 15:43  _Mickey  阅读(160)  评论(0编辑  收藏  举报