码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 3339 In Action(最短路+DP)

时间:2015-08-18 01:16:58      阅读:180      评论:0      收藏:0      [点我收藏+]

标签: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
Lost@HDU
 

Source
 

题意:一辆坦克只能占领一个核电站,有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;
}



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

HDU 3339 In Action(最短路+DP)

标签:hdu 3339 in action   最短路   dp   

原文地址:http://blog.csdn.net/acm_baihuzi/article/details/47738301

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!