HDU 3339 In Action(最短路+DP)

In Action

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 4704    Accepted Submission(s): 1547

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

[email protected]

Source

HDOJ Monthly Contest – 2010.03.06

题意:一辆坦克只能占领一个核电站,有n个核站,m条路。两个核战间可能有更短的路。每个核战都有相应的核值。

问控制一半以上的核值,需要最   少的油费?

题解:一开始理解成只要有坦克经过就相当于销毁了一个核电站,硬是搞不出来。看懂题意后不就是最短路+01背包么。。

先跑最短路求出0到任意点的最短距离,然后背包求出大于一般核值得最小油费。

#include<cstring>
#include<algorithm>
#include<iostream>
#include<cstdio>

using namespace std;

const int MAXN=110;
const int INF=0x3f3f3f3f;
bool vis[MAXN];
int lowcost[MAXN],cost[MAXN][MAXN];
int n,m;
void Dijkstra(int beg) {
    for(int i=0; i<n; i++) {
        lowcost[i]=INF;
        vis[i]=false;
    }
    lowcost[beg]=0;
    for(int j=0; j<n; j++) {
        int k=-1;
        int Min=INF;
        for(int i=0; i<n; i++)
            if(!vis[i]&&lowcost[i]<Min) {
                Min=lowcost[i];
                k=i;
            }
        if(k==-1)break;
        vis[k]=true;
        for(int i=0; i<n; i++)
            if(!vis[i]&&lowcost[k]+cost[k][i]<lowcost[i]) {
                lowcost[i]=lowcost[k]+cost[k][i];
            }
    }
}

int val[MAXN];
int dp[MAXN*MAXN];

int main() {
    //freopen("test.in","r",stdin);
    int t;
    cin>>t;
    while(t--) {
        scanf("%d%d",&n,&m);
        n++;
        int u,v,w;
        for(int i=0; i<n; i++)
            for(int j=i; j<n; j++) {
                if(i==j)cost[i][j]=0;
                else  cost[i][j]=cost[j][i]=INF;
            }
        for(int i=1; i<=m; i++) {
            scanf("%d%d%d",&u,&v,&w);
            cost[u][v]=cost[v][u]=min(cost[u][v],w);
        }
        int sum=0;
        for(int i=1; i<n; i++) {
            scanf("%d",&val[i]);
            sum+=val[i];
        }
        Dijkstra(0);
        for(int i=0; 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]]+lowcost[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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-04 10:23:12

HDU 3339 In Action(最短路+DP)的相关文章

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

In Action Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3869    Accepted Submission(s): 1237 Problem Description Since 1945, when the first nuclear bomb was exploded by the Manhattan Project t

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

hdu 3339 In Action 背包+flyod

In Action Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3339 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

hdu 3339 In Action(迪杰斯特拉+01背包)

In Action Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5102    Accepted Submission(s): 1696 Problem Description Since 1945, when the first nuclear bomb was exploded by the Manhattan Project t

hdu 4568 Hunter 最短路+dp

Hunter Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2014    Accepted Submission(s): 615 Problem Description One day, a hunter named James went to a mysterious area to find the treasures. Jame

HDU 4856 Tunnels (最短路+状压DP)

题意:给你N*N的网格,'.'表示可以走,'#'表示不能走,m条管道,每条管道有起点和终点坐标, Bob每次可以走到相邻的网格花费1s,问Bob走完m条管道要花多少时间:Bob在管道内不计算时间 即计算Bob从管道 i 的出口走到管道 j 的入口的时间Dis(e[i],s[j])的最小和,起点可以任意: 思路:看了题解说是状态压缩DP然后深入理解了下. 首先算出d[e[i]][s[j]]的最短距离,不能到达为-1: dp[i][j] : 表示以 j 为起点状态为 i 的最小值.其中 i 是用十进

HDOJ 3339 In Action

最短路+01背包 In Action Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3857    Accepted Submission(s): 1229 Problem Description Since 1945, when the first nuclear bomb was exploded by the Manhattan

HDU 3832 Earth Hour (最短路)

Earth Hour Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)Total Submission(s): 1518    Accepted Submission(s): 607 Problem Description Earth Hour is an annual international event created by the WWF (World Wide Fun

HDU 2833 WuKong(floyd最短路)

题目地址:HDU 2833 这题想到了最后是通过dis[s][t]==dis[s][i]+dis[i][j]+dis[j][t]的思路来判定是否属于最短路的一条..但是没想到可以用floyd来找最短路中的点数...最短路还是太渣了..好多性质都不会利用.. 这题的思路就是通过floyd对每两个点之间的最短路条数进行计数,然后通过上面的公式(对两条路线均要判定,都符合才说明都可以走),再找最短路中的最大点数. 代码如下: #include <iostream> #include <stdi