hdoj 3339 In Action【SPFA + 0-1背包】
2015-11-02 14:12
253 查看
In ActionTime 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 |
没想到用最短路来优化,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; }
相关文章推荐
- 【PAT】1080. Graduate Admission (30)
- Android~Handler机制自我理解
- ORACLE创建序列
- Android EditText的设置
- Angular (二) Jasmine单元测试和端到端测试
- JS入门之ActiveXObject对象
- iOS-新手学习之程序断点调试
- 2015-11-02-Android Apk签名总结
- asp.net中“从客户端中检测到有潜在危险的Request.Form值”错误的解决办法
- mysqladmin: connect to server at 'localhost' failed
- JVM 启动参数详解
- iOS category 和Extension,人家写的不错,转载一下
- mysqladmin: connect to server at 'localhost' failed
- leetcode@ [279]Perfect Squares
- JAVA中获取项目文件路径
- 邮件发送
- 对比度受限的自适应直方图均衡化(CLAHE)
- 微服务分布式事务的一些思考
- Qt使用自带的windeployqt 查找生成exe 必需的库文件
- CGridCtrl显示图片