|
In ActionTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3165 Accepted Submission(s): 1008
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
Sample Output
Author
Lost@HDU
Source
Recommend
lcy,
|
本题先是最短路预处理一下,求出从基地到每个节点的距离d[i],然后用背包模型求解问题。用dp[i][j]表示考虑前i个节点在恰好消灭j的能量的时候所需的最小路程。dp[i][j]=min(dp[i-1][j],dp[i-1][j-p[i]]+d[i]) p[i]表示i节点的能量。我在实现的时候使用的是滚动数组。
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <cassert>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include<cstdio>
#define maxn 109
#define maxm 20009
#define INF 1000000000
using namespace std;
int first[maxn];
int v[maxm],next[maxm],w[maxm];
int dp[maxm];
int p[maxn];
bool inq[maxn];
int d[maxn];
int tot,n,m;
void add(int x,int y,int z)
{
v[tot]=y;w[tot]=z;
next[tot]=first[x];
first[x]=tot++;
}
void spfa()
{
for(int i=0;i<=n;i++)
d[i]=INF;
queue<int>q;
d[0]=0;
q.push(0);
while(!q.empty())
{
int x=q.front();
q.pop();
inq[x]=0;
for(int e=first[x];e!=-1;e=next[e])
{
if(d[v[e]]>d[x]+w[e])
{
d[v[e]]=d[x]+w[e];
if(!inq[v[e]])
{
inq[v[e]]=1;
q.push(v[e]);
}
}
}
}
}
int main()
{
int tt;
scanf("%d",&tt);
while(tt--)
{
scanf("%d%d",&n,&m);
memset(first,-1,sizeof(first));tot=0;
for(int i=0;i<m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(y,x,z);
add(x,y,z);
}
spfa();
int sum=0;
for(int i=1;i<=n;i++)
scanf("%d",&p[i]),sum+=p[i];
int tar=sum/2+1;
for(int i=0;i<=sum;i++)
dp[i]=INF;
dp[0]=0;
for(int i=1;i<=n;i++)
{
if(d[i]==INF)
continue;
for(int j=sum;j>=p[i];j--)
dp[j]=min(dp[j],dp[j-p[i]]+d[i]);
}
int ans=INF;
for(int i=tar;i<=sum;i++)
ans=min(ans,dp[i]);
if(ans==INF)
printf("impossible\n");
else
printf("%d\n",ans);
}
}