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 ...
随机推荐
- sql语句感想
select出来内容可以当成表拿来用,,比如取别名什么的. union是纵向的,追加记录(行) join on是横向的,追加列
- Leetcode: Lexicographical Numbers
Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,1 ...
- 将url转化成对象
<script> var url = "baidu.com?"; //var url = window.location.search; //获取url functio ...
- HDU 5000 Clone(离散数学+DP)(2014 ACM/ICPC Asia Regional Anshan Online)
Problem Description After eating food from Chernobyl, DRD got a super power: he could clone himself ...
- paper 52 :windows7环境下theano安装
要做卷积神经网络的一些东西,所以要装theano,网上很多Theano安装教程版本较老,而各安装包更新很快,参考价值有限.走了很多弯路才装好,把这个过程记录下来,希望对大家有帮助~ ~ 我的配置:wi ...
- JDBCTest
package com.atguigu.spring.jdbc; import java.sql.SQLException; import java.util.ArrayList; import ja ...
- 【HDNOIP】HD201404最短路径
HD201404最短路径 [试题描述] a.b.c是3个互不相等的1位正数,用它们和数字0可以填满一个n行n列的方格阵列,每格中都有4种数码中的一个.填入0的格子表示障碍物,不能属于任何路径.你是否能 ...
- 前端框架与UI搭配
如果是 Angular 那就选 Ionic (一对好 CP)如果是 Vue 那就选 Vux (基于 WeUI)如果是 jQuery 那就选 Framework7 (iOS 和 Android 双皮肤) ...
- FastJson--阿里巴巴公司开源的速度最快的Json和对象转换工具(转)
本文转自:http://blog.csdn.net/djun100/article/details/24237371 这是关于FastJson的一个使用Demo,在Java环境下验证的 class U ...
- django migrate10060 Duplicate column name错误
这个错误意思是有重复的列名,其实大部分原因是因为某些列被执行了多次,可以打开migration里面的django生成的文件去排查错误,然后自己手动修改数据库还原,实在不行可以把除了0001和init文 ...