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 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

Recommend

lcy   |   We have
carefully selected several similar problems for you:  3338 1546 3333 3341 3335


 1 //187MS    616K    1856 B    C++
2 /*
3
4 题意:
5 题意挺重要的。一个源点0,源点每到一个站可得到一个pow[i],求得到总pow值一半以上时最短行走距离。
6
7 最短路+01背包:
8 首先最短路求出源点0到每一个点的距离,然后得出n个点,每个点有一个距离花费d,和一个价值权值w,
9 以到所有的点最短路程和作为背包容量,距离作为花费,权值作为价值,01背包。
10 当dp[j]>sum_pow/2+1时的距离和j为所求。
11
12 */
13 #include<iostream>
14 #include<queue>
15 #include<vector>
16 #define N 105
17 #define inf 0x7ffffff
18 using namespace std;
19 typedef struct node{
20 int d,w;
21 }node;
22 node p[N];
23 int vis[N];
24 int d[N];
25 int dp[N*N];
26 vector<node>V[N];
27 int n;
28 int Max(int x,int y)
29 {
30 return x>y?x:y;
31 }
32 void spfa(int s)
33 {
34 for(int i=0;i<=n;i++)
35 d[i]=inf;
36 d[s]=0;
37 vis[s]=1;
38 queue<int>Q;
39 Q.push(s);
40 while(!Q.empty()){
41 int u=Q.front();
42 Q.pop();
43 vis[u]=0;
44 int m=V[u].size();
45 for(int i=0;i<m;i++){
46 int v=V[u][i].d;
47 int w=V[u][i].w;
48 if(d[v]>d[u]+w){
49 d[v]=d[u]+w;
50 if(!vis[v]){
51 vis[v]=1;
52 Q.push(v);
53 }
54 }
55 }
56 }
57 }
58 int main(void)
59 {
60 int t,m;
61 int u,v,w,pow;
62 scanf("%d",&t);
63 while(t--)
64 {
65 int sd=0,sw=0;
66 scanf("%d%d",&n,&m);
67 for(int i=0;i<=n;i++) V[i].clear();
68 for(int i=0;i<m;i++){
69 scanf("%d%d%d",&u,&v,&w);
70 node q={v,w};
71 V[u].push_back(q);
72 q.d=u;
73 V[v].push_back(q);
74 }
75 for(int i=1;i<=n;i++){
76 scanf("%d",&p[i].w);
77 sw+=p[i].w;
78 }
79 spfa(0);
80 for(int i=1;i<=n;i++){
81 p[i].d=d[i];
82 if(d[i]!=inf)
83 sd+=p[i].d;
84 }
85 memset(dp,0,sizeof(dp));
86 for(int i=1;i<=n;i++)
87 for(int j=sd;j>=p[i].d;j--)
88 dp[j]=Max(dp[j],dp[j-p[i].d]+p[i].w);
89 int ans=0;
90 for(int i=1;i<=sd;i++)
91 if(dp[i]>=sw/2+1){
92 ans=i;break;
93 }
94 if(ans==0) puts("impossible");
95 else printf("%d\n",ans);
96 }
97 return 0;
98 }

hdu 3339 In Action (最短路径+01背包),布布扣,bubuko.com

时间: 2024-08-07 00:13:22

hdu 3339 In Action (最短路径+01背包)的相关文章

HDU 2639 Bone Collector II(01背包变型)

此题就是在01背包问题的基础上求所能获得的第K大的价值. 具体做法是加一维去推当前背包容量第0到K个价值,而这些价值则是由dp[j-w[ i ] ][0到k]和dp[ j ][0到k]得到的,其实就是2个数组合并之后排序,但是实际做法最好不要怎么做,因为你不知道总共有多少种,而我们最多只需要前K个大的就行了(因为可能2个数组加起来的组合数达不到K个),如果全部加起来数组开多大不清楚,所以可以选用归并排序中把左右2个有序数组合并成一个有序数组的方法来做,就是用2个变量去标记2个有序数组的头,然后比

hdu3339 In Action(Dijkstra+01背包)

1 /* 2 题意:有 n 个站点(编号1...n),每一个站点都有一个能量值,为了不让这些能量值连接起来,要用 3 坦克占领这个站点!已知站点的 之间的距离,每个坦克从0点出发到某一个站点,1 unit distance costs 1 unit oil! 4 最后占领的所有的站点的能量值之和为总能量值的一半还要多,问最少耗油多少! 5 6 */ 7 8 /* 9 思路:不同的坦克会占领不同的站点,耗油最少那就是路程最少,所以我们先将从 0点到其他各点的 10 最短距离求出来!也就是d[i]的

HDU 5234 Happy birthday --- 三维01背包

HDU 5234 题目大意:给定n,m,k,以及n*m(n行m列)个数,k为背包容量,从(1,1)开始只能往下走或往右走,求到达(m,n)时能获得的最大价值 解题思路:dp[i][j][k]表示在位置(i,j)有一个容量为k的背包所能获得的最大价值 决策:a[i][j]处的数是否选取 不选取: dp[i][j][k]= max(dp[i-1][j][k], dp[i][j-1][k]) 选取:首先要求k >=a[i][j],那么dp[i][j][k] = max(dp[i-1][j][k-w[i

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 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 2602 Bone Collector(01背包)

Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 42179    Accepted Submission(s): 17543 Problem Description Many years ago , in Teddy’s hometown there was a man who was called “Bon

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

HDU 2639 Bone Collector II(01背包变形【第K大最优解】)

Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4739    Accepted Submission(s): 2470 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took pa

HDU - 2602 Bone Collector(01背包讲解)

题意:01背包:有N件物品和一个容量为V的背包.每种物品均只有一件.第i件物品的费用是volume[i],价值是value[i],求解将哪些物品装入背包可使价值总和最大. 分析: 1.构造二维数组:dp[i][j]---前i件物品放入一个容量为j的背包可以获得的最大价值. dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - volume[i]] + value[i]);---(a) (1)dp[i - 1][j]---不放第i件物品,因此前i件物品放入一个容量为