您的位置:首页 > 其它

hdoj 3339 In Action【SPFA + 0-1背包】

2015-11-02 14:12 253 查看

In Action

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 4772    Accepted Submission(s): 1572


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

 

题意:给定 0点、n个能源站(编号从1 - n)和连接它们的m条无向路径,已知每个能源站含有的能源和每条路径的长度。你到达一个能源站便可以获得该站的所有能源,现在要求你从0点出发且获得的能源大于总能源的一半,问所需要走的最少距离。

没想到用最短路来优化,dp一直TLE。

思路:SPFA预处理起点0到所有点的最短路dist[],以有效的dist[]值之和为背包容量D,用p[]记录每个站的能源。那么问题就变成用容量为D的背包去装 n个重dist[i]、价值p[i]的物品,且没有定性要求背包正好装满,只要求背包里面物品价值和 大于p[]和 的一半。 这样就是一个裸的0-1背包问题了。

AC代码:280ms

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN 500000
#define MAXM 20010
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
struct Edge{
int from, to, val, next;
};
Edge edge[MAXM];
int head[110], edgenum;
int n, m;
void init(){
edgenum = 0;
CLR(head, -1);
}
int p[110];
void addEdge(int u, int v, int w)
{
Edge E1 = {u, v, w, head[u]};
edge[edgenum] = E1;
head[u] = edgenum++;
}
int sum;
void getMap()
{
Ri(n), Ri(m);
init();
for(int i = 0; i < m; i++)
{
int a, b, c;
Ri(a), Ri(b), Ri(c);
addEdge(a, b, c);
addEdge(b, a, c);
}
sum = 0;
for(int i = 1; i <= n; i++)
Ri(p[i]), sum += p[i];
}
int dist[MAXN]; bool vis[MAXN];
int D;
void SPFA(int s)
{
queue<int> Q;
vis[s] = true, dist[s] = 0;
Q.push(s);
while(!Q.empty())
{
int u = Q.front();
Q.pop();
vis[u] = false;
for(int i = head[u]; i != -1; i = edge[i].next)
{
Edge E = edge[i];
if(dist[E.to] > dist[u] + E.val)
{
dist[E.to] = dist[u] + E.val;
if(!vis[E.to])
{
vis[E.to] = true;
Q.push(E.to);
}
}
}
}
D = 0;
for(int i = 1; i <= n; i++)
D += dist[i] == INF ? 0 : dist[i];
}
int dp[MAXN];
void solve()
{
getMap(); CLR(vis, false); CLR(dist, INF);
//for(int i = 0; i <= n; i++)
SPFA(0); CLR(dp, 0);
sum = sum / 2 + 1;
int ans = INF;
for(int i = 1; i <= n; i++)
{
for(int j = D; j >= dist[i]; j--)
{
dp[j] = max(dp[j], dp[j-dist[i]]+p[i]);
if(dp[j] >= sum && j < ans)
ans = j;
}

}
if(ans == INF)
printf("impossible\n");
else
Pi(ans);
}
int main()
{
int t; Ri(t);
W(t){
solve();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: