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 ...
随机推荐
- Python条件循环判断
1.条件判断语句 Python中条件选择语句的关键字为:if .elif .else这三个.其基本形式如下: 1 2 3 4 5 6 7 8 9 age_of_cc = 27 age = int( ...
- 算法训练 区间k大数查询
http://lx.lanqiao.org/problem.page?gpid=T11 算法训练 区间k大数查询 时间限制:1.0s 内存限制:256.0MB 问题描述 给定一个 ...
- 从一个例子讲解拷贝构造函数与return
#include "iostream" using namespace std; class Location { public: Location(, ) { X = xx; Y ...
- 通过JavaScript设置样式和jQuey设置样式,还有随机数抛出水果的习题
一:通过JavaScript的方式设置样式(:拿习题为例): var shuiguo = document.getElementById('fruit'); shuiguo.style.bac ...
- bzoj 4358 permu
比较容易想到莫队算法+线段树,但是这样时间复杂度是O(nsqrtnlogn)无法通过,考虑如果不进行删除操作,只有添加操作的话那么并查集就可以实现了,于是可以设定sqrtn块,每个块范围为(i-1)* ...
- hdu5322 Hope
设dp[n]为n个数字排列时候的答案,那么可以得到dp方程 dp[n]=Σdp[n-i]*c(n-1,i-1)*(i-1)!*i^2(1<=i<=n) 然后上式可以化成卷积形式,分治FFT ...
- Maven(一)
Maven学习总结(一)——Maven入门 一.Maven的基本概念 Maven(翻译为"专家","内行")是跨平台的项目管理工具.主要服务于基于Java平台的 ...
- FastJson之有道翻译
在AndroidMainifast.xml中加入相应的访问权限 <uses-permission android:name="android.permission.INTERNET&q ...
- 初学android的第一个习作
首发:个人博客,更新&纠错&回复 项目源码在这里,因为github上传速度太差,传到了oschina的git托管上. 1.项目结构及技术点 主页面使用FragmentTabHost,5 ...
- Android 带checkbox的listView 实现多选,全选,反选,删除
activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...