@Chilling
2017-02-16T17:56:02.000000Z
字数 3516
阅读 925
最短路
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
题意:基地在0点,从基地到需要炸毁的点1~n有一定距离,每单位距离要耗费一单位的油。这1~n个点有自己的能量值,你至少需要减少他们能量总和的一半。问最少耗费的油是多少,如果不能完成输入impossible。
分析:求出从0到其他点的最短距离
dis[1]~dis[n]
,然后又知道每个点的能量值pow[1]~pow[n]
,求得总和sum,sum/2+1就是至少炸毁的能量,简单的01背包。
void dij()
{
dis[0]=0;
priority_queue<node>q;
q.push(node(0,dis[0]));
while(!q.empty())
{
node now,next;
now=q.top();
q.pop();
if(vis[now.en]==1)
continue;
vis[now.en]=1;
for(int i=0;i<v[now.en].size();i++)
{
next=v[now.en][i];
if(dis[next.en]>now.len+next.len)
{
dis[next.en]=now.len+next.len;
q.push(node(next.en,dis[next.en]));
}
}
}
}
void DP()
{
int i,j,x;
for(i=1;i<=n;i++)
sumd+=dis[i];
for(i=1;i<=n;i++)
for(j=sumd;j>=dis[i];j--)
dp[j]=max(dp[j],dp[j-dis[i]]+p[i]);
for(i=1;i<=sumd;i++)
if(dp[i]>=sump) //若大于或者等于了总能量的一半
{
x=i;
break;
}
printf("%d\n",x);
}
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
#define INF 1e9
using namespace std;
int dis[111],vis[111],n,m,p[111],dp[11111];
int sump,sumd;
struct node
{
int en,len;
node(int en1=0,int len1=0)
{
en=en1;len=len1;
}
friend bool operator <(node x,node y)
{
return x.len>y.len;
}
};
vector<node> v[11111];
void dij()
{
dis[0]=0;
priority_queue<node>q;
q.push(node(0,dis[0]));
while(!q.empty())
{
node now,next;
now=q.top();
q.pop();
if(vis[now.en]==1)
continue;
vis[now.en]=1;
for(int i=0;i<v[now.en].size();i++)
{
next=v[now.en][i];
if(dis[next.en]>now.len+next.len)
{
dis[next.en]=now.len+next.len;
q.push(node(next.en,dis[next.en]));
}
}
}
}
void DP()
{
int i,j,x;
for(i=1;i<=n;i++)
sumd+=dis[i];
for(i=1;i<=n;i++)
for(j=sumd;j>=dis[i];j--)
dp[j]=max(dp[j],dp[j-dis[i]]+p[i]);
for(i=1;i<=sumd;i++)
if(dp[i]>=sump)
{
x=i;
break;
}
printf("%d\n",x);
}
int main()
{
int t,a,b,c,i;
scanf("%d",&t);
while(t--)
{
sump=0,sumd=0;
for(i=0;i<111;i++)
dis[i]=INF;
memset(vis,0,sizeof(vis));
memset(p,0,sizeof(p));
memset(dp,0,sizeof(dp));
scanf("%d%d",&n,&m);
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
v[a].push_back(node(b,c));
v[b].push_back(node(a,c));
}
for(i=1;i<=n;i++)
{
scanf("%d",&p[i]);
sump+=p[i];
}
sump=sump/2+1; //一半以上
dij();
if(dis[1]==INF)
printf("impossible\n");
else
DP();
for(i=0;i<=n;i++)
v[i].clear();
}
return 0;
}