您的位置:首页 > 其它

hdu 3339(最短路+01背包)

2016-05-17 20:37 513 查看

In Action

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


[align=left]Problem Description[/align]



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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
The minimal oil cost in this action.
If not exist print "impossible"(without quotes).

[align=left]Sample Input[/align]

2
2 3
0 2 9
2 1 3
1 0 2
1
3
2 1
2 1 3
1
3

[align=left]Sample Output[/align]

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

;
int low
;
bool vis
;
int w
; ///第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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: