In Action(SPFA+01背包)
In Action
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4720 Accepted Submission(s): 1553
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处,问怎样安排坦克去的电站使的耗油量最少并且能控制多于一半的电量.
SPFA求出0到每个点的距离,然后01背包;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <map>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAX = 1e5+10;
int n,m;
int Map[110][110],Dp[10010];
int Dis[110];
bool vis[110];
int va[110];
void SPFA()//求最短路
{
memset(vis,false,sizeof(vis));
memset(Dis,INF,sizeof(Dis));
Dis[0]=0;
vis[0]=true;
queue<int>Q;
Q.push(0);
while(!Q.empty())
{
int u=Q.front();
Q.pop();
for(int i=0;i<=n;i++)
{
if(Dis[u]+Map[u][i]<Dis[i])
{
Dis[i]=Map[u][i]+Dis[u];
if(!vis[i])
{
Q.push(i);
vis[i]=true;
}
}
}
vis[u]=false;
}
}
int main()
{
int T;
int u,v,w;
int sum;
scanf("%d",&T);
while(T--)
{
memset(Map,INF,sizeof(Map));
scanf("%d %d",&n,&m);
sum=0;
for(int i=1;i<=m;i++)
{
scanf("%d %d %d",&u,&v,&w);
if(Map[u][v]>w)//去重
{
Map[u][v]=w;
Map[v][u]=w;
}
}
for(int i=1;i<=n;i++)
{
scanf("%d",&va[i]);
sum+=va[i];
}
SPFA();
memset(Dp,INF,sizeof(Dp));
Dp[0]=0;
for(int i=1;i<=n;i++)//01背包
{
for(int j=sum;j>=va[i];j--)
{
Dp[j]=min(Dp[j],Dp[j-va[i]]+Dis[i]);
}
}
int Max=INF;
for(int i=sum/2+1;i<=sum;i++)
{
if(Dp[i]<Max)
{
Max=Dp[i];
}
}
if(Max==INF)
{
printf("impossible\n");
}
else
{
printf("%d\n",Max);
}
}
return 0;
}
In Action(SPFA+01背包)的更多相关文章
- hdu 3339 In Action (最短路径+01背包)
In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- hdu3339 In Action(Dijkstra+01背包)
/* 题意:有 n 个站点(编号1...n),每一个站点都有一个能量值,为了不让这些能量值连接起来,要用 坦克占领这个站点!已知站点的 之间的距离,每个坦克从0点出发到某一个站点,1 unit dis ...
- 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背包】
题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=3339] In Action Time Limit: 2000/1000 MS (Java/Other ...
- HDU 3339 In Action 最短路+01背包
题目链接: 题目 In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 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(迪杰斯特拉+01背包)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3339 In Action Time Limit: 2000/1000 MS (Java/Others) ...
- 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背包)
In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
随机推荐
- display:flex 多栏多列布局
转自:http://www.360doc.com/content/14/0811/01/2633_400926000.shtml display:flex 多栏多列布局浏览器支持情况:火狐直接支持w3 ...
- Leetcode: Remove Elements
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- nyist 506 洗澡
http://acm.nyist.net/JudgeOnline/problem.php?pid=506 洗澡 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描述 Mos ...
- [原创]java WEB学习笔记90:Hibernate学习之路-- -HQL检索方式,分页查询,命名查询语句,投影查询,报表查询
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Python基础(1)python+Eclipse+pydev环境搭建
编辑器:Python 自带的 IDLE 简单快捷, 学习Python或者编写小型软件的时候.非常有用. 编辑器: Eclipse + pydev插件 1. Eclipse是写JAVA的 ...
- Oracle游标总结三
-- 声明游标:CURSOR cursor_name IS select_statement --For 循环游标--(1)定义游标--(2)定义游标变量--(3)使用for循环来使用这个游标,for ...
- 夺命雷公狗---Thinkphp----9之中间层的创建,防止跨目录访问
我们创建一个CommonController.class.php的中间层,让后让别的控制器都直接继承CommonController这个控制器即可决解跨目录访问的问题, <?php namesp ...
- Logic and Fault simulation
fault simulation是指对fault circuit的simulation,来locate manufacturing defects并且进行fault diagnosis. logic ...
- SSAS维度上有多个表的注意事项
在Sql Server Analysis Service中维度上有多张表(大于一张表)时,一定要注意将第二张表开始用到维度属性中的KeyColumns下的NullProcessing要设置为Unkno ...
- [转]编译Android源代码常见错误解决办法
1. 编译时出现/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../libz.so when ...