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

来源:互联网 发布:九黎影视源码 编辑:程序博客网 时间:2024/06/11 00:15

In Action

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

Problem Description

Since 1945, when the firstnuclear bomb was exploded by the Manhattan Project team in the US, the numberof nuclear weapons have soared across the globe.
Nowadays,the crazy boy in FZU named AekdyCoin possesses some nuclear weaponsand wanna destroy our world. Fortunately, our mysterious spy-net has gotten hisplan. Now, we need to stop it.
But the arduous task is obviously not easy. First of all, we know that theoperating system of the nuclear weapon consists of some connected electricstations, which forms a huge and complex electric network. Every electricstation has its power value. To start the nuclear weapon, it must cost half ofthe electric network's power. So first of all, we need to make more than halfof 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 themif and only if our tanks stop there. 1 unit distance costs 1 unit oil. And wehave enough tanks to use.
Now our commander wants to know the minimal oil cost in this action.

 

 

Input

The first line of the inputcontains 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 thenumber 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 thisaction.
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的节点,其余有n个发电站节点,有m条无向边,每次可以派出一辆坦克占领一个发电站,要求占领的发电站的点power和大于总点power的一半,,问最少油量消耗,油量消耗==路径的长度和。

 思路:先跑个最短路,然后以起点到所有电站的距离作为背包的价值,以每个电站的发电量总和作为背包物品的容量,进行01背包。


#include <bits/stdc++.h>using namespace std;#define mst(a,b) memset((a),(b),sizeof(a))#define f(i,a,b) for(int i=(a);i<(b);++i)#define ll long longconst int maxn = 105;const int mod = 475;const ll INF = 0x3f3f3f3f;const double eps = 1e-6;#define rush() int T;scanf("%d",&T);while(T--)int n,m;int dis[maxn][maxn];int p[maxn];int dp[10005];void init(){    for(int i=0;i<=n;i++)        for(int j=0;j<=n;j++)        {            if(i==j)                dis[i][j]=0;            else dis[i][j]=INF;        }}void floyd(){    for(int k=0;k<=n;k++)        for(int i=0;i<=n;i++)        {            if(dis[i][k]!=INF)            {                for(int j=0;j<=n;j++)                if(dis[i][k]+dis[k][j]<dis[i][j])                    dis[i][j]=dis[i][k]+dis[k][j];            }        }}int main(){    int s,t,val;    rush()    {        scanf("%d%d",&n,&m);        init();        for(int i=0;i<m;i++)        {            scanf("%d%d%d",&s,&t,&val);            dis[s][t]=dis[t][s]=min(dis[s][t],val);        }        floyd();        int sum=0;        for(int i=1;i<=n;i++)        {            scanf("%d",&p[i]);            sum+=p[i];        }        mst(dp,0x3f);        dp[0]=0;        for(int i=1;i<=n;i++)        {            if(dis[0][i]!=INF)            for(int j=sum;j>=p[i];j--)            {                if(dp[j]>dp[j-p[i]]+dis[0][i])                    dp[j]=dp[j-p[i]]+dis[0][i];            }        }        int ans=INF;        for(int i=sum/2+1;i<=sum;i++)        {            if(ans>dp[i])                ans=dp[i];        }        if(ans!=INF)            printf("%d\n",ans);        else printf("impossible\n");    }    return 0;}

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 dnf二级输错了怎么办 如果受到法律的伤害怎么办 86岁了还怕死怎么办 风衣的腰带丢了怎么办 成为伪娘身上的毛怎么办 军官升不上去了怎么办 王者荣耀代练封号怎么办 cf淘宝代练封号怎么办 买音乐会的票过期怎么办 十年多年前被怨错拘留了怎么办 与室友关系闹僵怎么办 开庭后被告威胁我们证人怎么办 开车撞了豪车怎么办 我把人撞了全责怎么办 开车撞伤人没钱赔怎么办 开车撞伤无证驾驶人怎么办 开车把人撞伤了只买交强险怎么办 开车撞伤人赔不起怎么办 如果车撞死人了怎么办 给车撞了跑了怎么办 开车把人撞死了怎么办 开车把人蹭了怎么办 驾照被扣54分怎么办? 驾照被扣了12分怎么办 驾驶证丢失后被扣分怎么办 驾照扣了三十分怎么办 一次被扣了12分怎么办 驾照被扣35分后怎么办 我驾驶证扣了6分怎么办 c1驾照被扣6分怎么办 被扣了6分怎么办 今年扣了6分怎么办 驾照分不够扣了怎么办 被扣6分怎么办罚款15 驾照分数扣36分怎么办 驾照12分不够扣怎么办 驾照b证扣分了怎么办 违章18分不够扣怎么办 b2驾驶证扣11分怎么办 驾照一共扣30分怎么办 驾照扣了100分怎么办