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

时间:2023-12-22 14:30:44

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
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 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
Recommend
lcy   |   We have carefully selected several similar problems for you:  3338 1546 3333 3341 3335 
 //187MS    616K    1856 B    C++
/* 题意:
题意挺重要的。一个源点0,源点每到一个站可得到一个pow[i],求得到总pow值一半以上时最短行走距离。 最短路+01背包:
首先最短路求出源点0到每一个点的距离,然后得出n个点,每个点有一个距离花费d,和一个价值权值w,
以到所有的点最短路程和作为背包容量,距离作为花费,权值作为价值,01背包。
当dp[j]>sum_pow/2+1时的距离和j为所求。 */
#include<iostream>
#include<queue>
#include<vector>
#define N 105
#define inf 0x7ffffff
using namespace std;
typedef struct node{
int d,w;
}node;
node p[N];
int vis[N];
int d[N];
int dp[N*N];
vector<node>V[N];
int n;
int Max(int x,int y)
{
return x>y?x:y;
}
void spfa(int s)
{
for(int i=;i<=n;i++)
d[i]=inf;
d[s]=;
vis[s]=;
queue<int>Q;
Q.push(s);
while(!Q.empty()){
int u=Q.front();
Q.pop();
vis[u]=;
int m=V[u].size();
for(int i=;i<m;i++){
int v=V[u][i].d;
int w=V[u][i].w;
if(d[v]>d[u]+w){
d[v]=d[u]+w;
if(!vis[v]){
vis[v]=;
Q.push(v);
}
}
}
}
}
int main(void)
{
int t,m;
int u,v,w,pow;
scanf("%d",&t);
while(t--)
{
int sd=,sw=;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) V[i].clear();
for(int i=;i<m;i++){
scanf("%d%d%d",&u,&v,&w);
node q={v,w};
V[u].push_back(q);
q.d=u;
V[v].push_back(q);
}
for(int i=;i<=n;i++){
scanf("%d",&p[i].w);
sw+=p[i].w;
}
spfa();
for(int i=;i<=n;i++){
p[i].d=d[i];
if(d[i]!=inf)
sd+=p[i].d;
}
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)
for(int j=sd;j>=p[i].d;j--)
dp[j]=Max(dp[j],dp[j-p[i].d]+p[i].w);
int ans=;
for(int i=;i<=sd;i++)
if(dp[i]>=sw/+){
ans=i;break;
}
if(ans==) puts("impossible");
else printf("%d\n",ans);
}
return ;
}