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
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.
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.
If not exist print "impossible"(without quotes).
2 3
0 2 9
2 1 3
1 0 2
1
3
2 1
2 1 3
1
3
impossible
- #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 ;
- }
HDU 3339 In Action【最短路+01背包】的更多相关文章
- HDU 3339 In Action 最短路+01背包
题目链接: 题目 In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu3339In Action(最短路+01背包)
http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=259#problem/H Description Since 1945, whe ...
- HDU 3339 In Action【最短路+01背包模板/主要是建模看谁是容量、价值】
Since 1945, when the first nuclear bomb was exploded by the Manhattan Project team in the US, the n ...
- hdu 3339 In Action (最短路径+01背包)
In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- hdu 3339 In Action
http://acm.hdu.edu.cn/showproblem.php?pid=3339 这道题就是dijkstra+01背包,先求一遍最短路,再用01背包求. #include <cstd ...
- HDU-3339 IN ACTION(Dijkstra +01背包)
Since 1945, when the first nuclear bomb was exploded by the Manhattan Project team in the US, the ...
- HDU 3339 In Action(迪杰斯特拉+01背包)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3339 In Action Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 3339 In Action(迪杰斯特拉+01背包)
In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- HDU 3339 最短路+01背包
In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
随机推荐
- 用setTimeout模拟QQ延时提示框
很简单的代码,不多解释,一看就懂. <!DOCTYPE html> <html> <head> <meta http-equiv="Content- ...
- 三星c7换屏幕教程
https://jingyan.baidu.com/article/20b68a88f49cb9796cec6282.html
- R1(下)—数据挖掘—关联规则理论介绍与R实现
Apriori algorithm是关联规则里一项基本算法.是由Rakesh Agrawal和Ramakrishnan Srikant两位博士在1994年提出的关联规则挖掘算法.关联规则的目的就是在一 ...
- CodeForces - 1015D
There are nn houses in a row. They are numbered from 11 to nn in order from left to right. Initially ...
- webpack4 未设置mode会自动压缩
最近想用LayaBox做个小游戏,然而Laya本身不自带构建工具.然后觉得写模块化的东西还是用webpack好使,用es6的语法也比较清晰. 于是就装了webpack,只用babel-loader来编 ...
- 多进程Process
多进程旧式写法 from multiprocessing import Pool def f(x): return x*x if __name__ == '__main__': p = Pool(5) ...
- 好久没写了,SQLSERVER服务丢失后怎么办
服务器突然中了病毒,查杀后,结果两个服务也丢了, 从其他机器上COPY了两个EXE过来,编写这两个服务就搞定了,不用重装MSSQL2005了 sc create MSSQLSERVER binpath ...
- CTF线下赛AWD套路小结
近打了2场CTF线下赛,把AWD模式中的一些小套路做一些总结,本人web狗,二进制部分就不班门弄斧了. 一. AWD模式简介 AWD:Attack With Defence,比赛中每个队伍维护多台服务 ...
- KKT条件和拉格朗日乘子法详解
\(\frac{以梦为马}{晨凫追风}\) 最优化问题的最优性条件,最优化问题的解的必要条件和充分条件 无约束问题的解的必要条件 \(f(x)\)在\(x\)处的梯度向量是0 有约束问题的最优性条件 ...
- sql server 2008 r2 产品密钥
数据中心版:PTTFM-X467G-P7RH2-3Q6CG-4DMYBDDT3B-8W62X-P9JD6-8MX7M-HWK38==================================== ...