In Action
Time Limit: 2000/1000 MS(Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5977 Accepted Submission(s): 2010
Problem Description
Since 1945, when the firstnuclear bomb was exploded by the Manhattan Project team in the US, the numberof nuclear weapons have soared across the globe.
Nowadays,the crazy boy in FZU named AekdyCoin possesses some nuclear weaponsand wanna destroy our world. Fortunately, our mysterious spy-net has gotten hisplan. Now, we need to stop it.
But the arduous task is obviously not easy. First of all, we know that theoperating system of the nuclear weapon consists of some connected electricstations, which forms a huge and complex electric network. Every electricstation has its power value. To start the nuclear weapon, it must cost half ofthe electric network's power. So first of all, we need to make more than halfof 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 themif and only if our tanks stop there. 1 unit distance costs 1 unit oil. And wehave enough tanks to use.
Now our commander wants to know the minimal oil cost in this action.
Input
The first line of the inputcontains 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 thenumber 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 thisaction.
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的节点,其余有n个发电站节点,有m条无向边,每次可以派出一辆坦克占领一个发电站,要求占领的发电站的点power和大于总点power的一半,,问最少油量消耗,油量消耗==路径的长度和。
思路:先跑个最短路,然后以起点到所有电站的距离作为背包的价值,以每个电站的发电量总和作为背包物品的容量,进行01背包。
#include <bits/stdc++.h>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define f(i,a,b) for(int i=(a);i<(b);++i)
#define ll long long
const int maxn = 105;
const int mod = 475;
const ll INF = 0x3f3f3f3f;
const double eps = 1e-6;
#define rush() int T;scanf("%d",&T);while(T--)
int n,m;
int dis[maxn][maxn];
int p[maxn];
int dp[10005];
void init()
{
for(int i=0;i<=n;i++)
for(int j=0;j<=n;j++)
{
if(i==j)
dis[i][j]=0;
else dis[i][j]=INF;
}
}
void floyd()
{
for(int k=0;k<=n;k++)
for(int i=0;i<=n;i++)
{
if(dis[i][k]!=INF)
{
for(int j=0;j<=n;j++)
if(dis[i][k]+dis[k][j]<dis[i][j])
dis[i][j]=dis[i][k]+dis[k][j];
}
}
}
int main()
{
int s,t,val;
rush()
{
scanf("%d%d",&n,&m);
init();
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&s,&t,&val);
dis[s][t]=dis[t][s]=min(dis[s][t],val);
}
floyd();
int sum=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&p[i]);
sum+=p[i];
}
mst(dp,0x3f);
dp[0]=0;
for(int i=1;i<=n;i++)
{
if(dis[0][i]!=INF)
for(int j=sum;j>=p[i];j--)
{
if(dp[j]>dp[j-p[i]]+dis[0][i])
dp[j]=dp[j-p[i]]+dis[0][i];
}
}
int ans=INF;
for(int i=sum/2+1;i<=sum;i++)
{
if(ans>dp[i])
ans=dp[i];
}
if(ans!=INF)
printf("%d\n",ans);
else printf("impossible\n");
}
return 0;
}