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.
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.
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
题意就是你需要阻止核武器发射,用最少的钱切断电网一半以上的功率,每个点都有功率,你需要用坦克占领一些点,占领点总值超过总功率的一半就可以,然后本部在0点,每段路都有过路费。那么我们首先可以求出,占领每个点的最小花费,也就是以过路费做路长算0到每个点的最短路径,然后我们只需要计算,把这些点钟拿出一些点功率和超过总功率的一半需要的最少钱。这就转变成了01背包问题,我们可以以功率做价值,过路费做背包。最后从1开始循环背包数组,找到第一个大于总功率一半的f[i],此时的i就是我们需要满足条件的最小花费。
代码如下:
#include
#include
#include
using namespace std;
#define inf 0x3f3f3f3f
int e[120][120],dis[120],book[120],q[120];
int n,m,t,f[1000020];
int main()
{
scanf("%d",&t);
while(t--)
{
memset(f,0,sizeof(f));
scanf("%d%d",&n,&m);
for(int i=0;i<=n;i++)
for(int j=0;j<=n;j++)
if(i==j)e[i][j]=0;
else e[i][j]=e[j][i]=inf;
int sum=0,p=0,a,b,c,flag=-1;
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
if(ce[j][i]+e[i][k])
e[j][k]=e[j][i]+e[i][k];
for(int i=1;i<=n;i++)
if(e[0][i]!=inf)
sum+=e[0][i];
for(int i=1;i<=n;i++)
for(int j=sum;j>=e[0][i];j--)
f[j]=max(f[j],f[j-e[0][i]]+q[i]);
for(int i=0;i<=sum;i++)
if(f[i]>p/2)
{
flag=i;break;
}
if(flag!=-1)printf("%d\n",flag);
else printf("impossible\n");
}
}