HDU 3339 In Action(最短路+背包)
题目:
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
题目大意:
要破坏掉一个电网, 有n个电站编号为1~n,每个电站有它自己的能量值。有一个军事基地编号为0,里面有无限个坦克,可以开到某个电站轰炸破坏掉电站,并且一个坦克只能破坏一个。现在要破坏掉其中一些电站,要让电网的总能量值损失一半以上, 并且要让所有执行任务的坦克去目的地路费最少。
分析与总结:
1. 容易想到用单源最短路算法求出0到所有其他点的最短距离。
2. 关键是要选择破坏哪些电站,使得这些破坏的电站总能量值为原来的一半以上。
因为对于每一个电站,要么是选择破坏,要么是不破坏,那么便可以联想到经典的01背包问题。
把求得的最短距离总和当作背包容量, 各个站的能量值当作物品价值,然后OK。
代码:
[cpp]
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<utility>
using namespace std;
typedef pair<int, int>pii;
const int INF = 0x7fffffff;
const int VN = 105;
const int EN = 20005;
struct Edge{int v,next,w;}E[EN];
priority_queue<pii,vector<pii>,greater<pii> >q;
int n, size;
int head[VN], d[VN], pow[VN];
int dp[10005];
int oil[10005];
void init(){
size = 0;
memset(head, -1, sizeof(head));
while(!q.empty()) q.pop();
}
void addEdge(int u,int v,int w){
E[size].v=v, E[size].w=w;
E[size].next = head[u];
head[u] = size++;
}
void Dijkstra(int src){
for(int i=0; i<=n; ++i) d[i] = INF;
d[src] = 0;
q.push(make_pair(d[src], src));
while(!q.empty()){
pii x = q.top(); q.pop();
int u = x.second;
if(d[u] != x.first) continue;
for(int e=head[u]; e!=-1; e=E[e].next){
int tmp = d[u] + E[e].w;
if(d[E[e].v] > tmp){
d[E[e].v] = tmp;
q.push(make_pair(tmp,E[e].v));
}
}
}
}
int main(){
int T,m,u,v,c;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
init();
for(int i=0; i<m; ++i){
scanf("%d%d%d",&u,&v,&c);
addEdge(u,v,c);
addEdge(v,u,c);
}
for(int i=1; i<=n; ++i){
scanf("%d",&pow[i]);
}
Dijkstra(0);
if(d[1]==INF){
puts("impossible"); continue;
}
int dis_sum = 0, pow_sum = 0;
for(int i=1; i<=n; ++i){
dis_sum += d[i];
pow_sum += pow[i];
}
memset(dp, 0, sizeof(dp));
for(int i=1; i<=n; ++i)
for(int j=dis_sum; j>=d[i]; --j)
dp[j] = max(dp[j],dp[j-d[i]]+pow[i]);
pow_sum = (pow_sum>>1)+1;
for(int i=1; i<=dis_sum; ++i){
if(dp[i]>=pow_sum){
printf("%d\n", i);
补充:软件开发 , C++ ,