HDU 3339 In Action【最短路+01背包】

时间:2023-03-09 00:04:15
HDU 3339 In Action【最短路+01背包】

题目链接:【http://acm.hdu.edu.cn/showproblem.php?pid=3339】

In Action

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5951    Accepted Submission(s): 1998

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
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
题意:给出N个供电站编号从1~N,然后给出M条边:基地(编号为0)到某些供电站之间的距离和供电站之间的距离,并且给出这N个供电站的电量。每辆坦克从基地出发去攻击供电站,并且每辆坦克只能攻击一个供电站,求要使的破坏总电量的一半以,求这些坦克要走的最短距离。
题解:最短路+01背包。
#include<bits/stdc++.h>
using namespace std;
const int INF = 1e7 + ;
const int maxn = ;
int N, M;
int mp[][], val[];
int dis[], inq[];
void SPFA(int st)
{
for(int i = ; i <= N; i++)
dis[i] = INF, inq[i] = ;
dis[st] = ;
queue<int> que;
que.push(st);
inq[st] = ;
while(!que.empty())
{
int u = que.front();
inq[u] = ;
que.pop();
for(int v = ; v <= N; v++)
{
if(v == u) continue;
if(dis[v] > dis[u] + mp[u][v])
{
dis[v] = dis[u] + mp[u][v];
if(!inq[v]) que.push(v), inq[v] = ;
}
}
}
}
int main ()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d", &N, &M);
for(int i = ; i <= N; i++)
{
for(int j = ; j <= N; j++)
mp[i][j] = INF;
mp[i][i] = ;
}
int u, v, len;
for(int i = ; i <= M; i++)
{
scanf("%d%d%d", &u, &v, &len);
if(mp[u][v] > len) mp[u][v] = mp[v][u] = len;
}
SPFA();
int sum = ;
for(int i = ; i <= N; i++)
scanf("%d", &val[i]), sum += val[i];
int dp[sum + ];
for(int i = ; i <= sum; i++)
dp[i] = INF;
dp[] = ;
for(int i = ; i <= N; i++)
for(int j = sum; j >= val[i]; j--)
dp[j] = min(dp[j], dp[j - val[i]] + dis[i]);
int ans = INF;
for(int i = sum / + ; i <= sum; i++)
ans = min(ans, dp[i]);
if(ans == INF)
printf("impossible\n");
else
printf("%d\n", ans);
}
return ;
}