hdu 3339(最短路+01背包)

Posted AC菜鸟机

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 3339(最短路+01背包)相关的知识,希望对你有一定的参考价值。

In Action

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


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
题意:一些坦克要占据一些能量据点,坦克从0点出发,总共有编号1-n n个能量据点,如果要摧毁敌方,必须要占领能量据点的能量值达到总能量的一半以上,现在知道m条路径,以及坦克在m条路上的油耗,然后知道每个能量据点的能量值,问摧毁敌方所需的最少油耗.
题解:最短路+01背包,将每个能量据点看成背包容量,油耗看成价值,然后进行01背包求解.我不知道为什么数组刚开始开小了会报tle..
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
const int N = 105;
const int INF = 99999999;
int graph[N][N];
int low[N];
bool vis[N];
int w[N]; ///第i座城市所拥有的能源
int dp[N*N]; ///dp[i]代表控制i吨能源所耗费的最少油量 (= =)没想到用什么词,就用吨好了
int n,m;
int dijkstra(int s){
    for(int i=1;i<=n;i++){
        low[i] = graph[s][i];
        vis[i] = false;
    }
    low[s] = 0;
    vis[s] = true;
    for(int i=1;i<n;i++){
        int Min = INF;
        for(int j=1;j<=n;j++){
            if(Min>low[j]&&!vis[j]){
                Min = low[j];
                s = j;
            }
        }
        vis[s] = true;
        for(int j=1;j<=n;j++){
            if(low[j]>low[s]+graph[s][j]&&!vis[j]){
                low[j] = low[s]+graph[s][j];
            }
        }
    }
}
int main()
{
    int tcase;
    scanf("%d",&tcase);
    while(tcase--){
        scanf("%d%d",&n,&m);
        for(int i=0;i<=n;i++){
            for(int j=0;j<=n;j++){
                if(i==j) graph[i][j] = 0;
                else graph[i][j] = INF;
            }
        }
        for(int i=0;i<m;i++){
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            if(c<graph[a][b])
            graph[a][b]=graph[b][a] =c;
        }
        int sum = 0;
        for(int i=1;i<=n;i++){
            scanf("%d",&w[i]);
            sum+=w[i];
        }
        dijkstra(0);
        for(int i=1;i<=sum;i++) dp[i] = INF;
        dp[0] = 0;
        for(int i=1;i<=n;i++){
            for(int v = sum;v>=w[i];v--){
                dp[v] = min(dp[v],dp[v-w[i]]+low[i]);
            }
        }
        int sum1 = sum/2+1; ///控制能量超过一半
        int Min = INF;
        for(int i=sum1;i<=sum;i++){
            if(dp[i]<Min) Min = dp[i];
        }
        if(Min==INF) printf("impossible\n");
        else printf("%d\n",Min);
    }
}

 

以上是关于hdu 3339(最短路+01背包)的主要内容,如果未能解决你的问题,请参考以下文章

HDU 3339 In Action最短路+01背包模板/主要是建模看谁是容量价值

nyoj 203 三国志(最短路加01背包)

Mr. Panda and Crystal HDU - 6007 最短路+完全背包

hdu6007 Mr. Panda and Crystal 最短路+完全背包

最小生成树次生成树最短路劲0-背包总结

hdu 2363(枚举+最短路好题)