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 ...
随机推荐
- 3.HelloWorld
准备开发环境和运行环境开发工具:eclipse运行环境:apache-tomcat-7.0.4工程:动态web 工程Spring 框架下载:spring-framework-3.2.3.RELEASE ...
- JavaScript----marquee滚动标签 图片无缝滚动 插入百度地图
页面的自动滚动效果,可由javascript来实现, 但是有一个html标签 - <marquee></marquee>可以实现多种滚动效果,无需js控制. 使用marquee ...
- mac tomcat
alampsdeMacBook-Pro:bin alamps$ ./startup.sh Using CATALINA_BASE: /Users/alamps/Library/apache-tomca ...
- android_demo01
/layout/activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/ ...
- paper 30 :libsvm的参数说明
English: libsvm_options: -s svm_type : set type of SVM (default 0) 0 -- C-SVC 1 -- nu-SVC 2 -- one-c ...
- 夺命雷公狗---DEDECMS----11dedecms字段标签
如果我们在开发的时候需要对获取的某个字段进行二次开发,我们可以对字段值调用某个函数来完成需求, 实例代码如下所示: <!DOCTYPE html> <html> <hea ...
- Sql Server 检测死锁的SQL语句
首先创建一个标量值函数DigLock,用来递归检测SqlServer中的每一个会话是否存在加锁循环,如果该函数最终返回1则表示检测到了加锁循环 (也就是说检测到了死锁),如果最终返回0则表示没有检测到 ...
- OpenStack collectd的从零安装服务端
安装collectd包操作同客户端相同,不在赘述 配置文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 2 ...
- equals 与 == 的区别和用法(C# & Java)【转】
转至http://www.cnblogs.com/beeone/archive/2011/04/25/2026674.html public class TestString { public sta ...
- 利用Qt Assistant 定制帮助文档
为了将Qt Assistant定制为自己应用程序的帮助文档浏览器.需要完成以下几步: 一.导入HTML格式的帮助文档 (1)首先,针对自己的应用程序创建HTML格式的帮助文档,请参见<Doxyg ...