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): 7869 Accepted Submission(s): 2674

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.
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 <iostream>
#include <cstdio>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<memory>
#include<queue>
#include<vector>
using namespace std;
typedef long long LL;
#define max_v 10005
#define INF 99999999 int dp[max_v];
int v[max_v];
int cost[max_v]; int e[max_v][max_v];
int n,m;
int used[max_v];
int dis[max_v];
void init()
{
memset(used,,sizeof(used));
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
e[i][j]=INF;
}
dis[i]=INF;
}
}
void Dijkstra(int s)
{
for(int i=; i<=n; i++)
{
dis[i]=e[s][i];
}
dis[s]=;
for(int i=; i<=n; i++)
{
int index,mindis=INF;
for(int j=; j<=n; j++)
{
if(used[j]==&&dis[j]<mindis)
{
mindis=dis[j];
index=j;
}
}
used[index]=;
for(int j=; j<=n; j++)
{
if(dis[index]+e[index][j]<dis[j])
dis[j]=dis[index]+e[index][j];
}
}
}
void ZeroOnePack_improve(int n,int c)
{
memset(dp,,sizeof(dp));
for(int i=; i<=n; i++)
{
for(int j=c; j>=cost[i]; j--)
{
dp[j]=max(dp[j],dp[j-cost[i]]+v[i]); }
}
}
int main()
{
int t;
int a,b,c;
int sum;
int sumv;
int flag;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
n++;
init();
for(int i=; i<m; i++)
{
scanf("%d %d %d",&a,&b,&c);
a++;
b++;
if(e[a][b]>c)
e[a][b]=e[b][a]=c;
}
sumv=;
for(int i=; i<=n-; i++)
{
scanf("%d",&v[i]);//价值
sumv+=v[i];
} Dijkstra();
int k=;
sum=;
for(int i=; i<=n; i++)
{
cost[k++]=dis[i];//花费
if(dis[i]!=INF)//wa点
sum+=dis[i];
}
ZeroOnePack_improve(k-,sum);
flag=;
for(int i=; i<=sum; i++)
{
if(dp[i]>=sumv/+)
{
flag=;
printf("%d\n",i);
break;
}
}
if(flag==)
printf("impossible\n");
}
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)Total S ...
- 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背包模板/主要是建模看谁是容量、价值】
Since 1945, when the first nuclear bomb was exploded by the Manhattan Project team in the US, the n ...
- HDU 2680 最短路 迪杰斯特拉算法 添加超级源点
Choose the best route Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU 2544最短路 (迪杰斯特拉算法)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2544 最短路 Time Limit: 5000/1000 MS (Java/Others) Me ...
- HDU 3790(两种权值的迪杰斯特拉算法)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3790 最短路径问题 Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 1874畅通工程续(迪杰斯特拉算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Time Limit: 3000/1000 MS (Java/Others) ...
- hdu 1142(迪杰斯特拉+记忆化搜索)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- hdu 1595 find the longest of the shortest(迪杰斯特拉,减去一条边,求最大最短路)
find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 ...
随机推荐
- c# 使用 Newtonsoft.Json 序列化json字符串以及,反序列化对象
1. 序列化 对象 /** 使用 Newtonsoft.Json 序列化对象 **/ [WebMethod] public String getPersonInfos() { // 初始化数据 Lis ...
- OpenStack IceHouse 部署 - 2 - 网络与软件环境初始化
OpenStack应用:节点软硬件环境配置 节点硬件与IP分配 实验室网关 10.14.39.1 各个节点 节点名称 硬件(Linux硬盘分区,RAM,CPU) ip地址(接口) 作用与运行的服 ...
- 记录一次teamview无法远程连接对方teamview的过程
问题描述: teamviewer 提示 超时后连接被阻断.您的许可证对您与伙伴的最大话时间有所限制...... 解决方法: 1.先将自己的teamview完全卸载,连同安装目录一起删除.尽量卸载完全 ...
- 停课+2week
可真是,累啊. 本以为停课之后会轻松一点,结果天天好累的说... 今天开始得去锻炼锻炼了... 已经好几次突然一阵晕眩了qwq... 希望我还能挺得住吧,至少要挺到WC结束啊... 这次,可是关系到我 ...
- CSS3字体火焰燃烧效果
动画的CSS: // fire @keyframes fireDiv { 0% { text-shadow: 0 0 4px white, 0 -5px 4px #ff3, 2px -10px 6px ...
- CSS3 教程
CSS3 教程 CSS 用于控制网页的样式和布局. CSS3 是最新的 CSS 标准. 本教程向您讲解 CSS3 中的新特性. 开始学习 CSS3! 更多:http://www.runoob.com ...
- numpy数组的创建
创建数组 创建ndarray 创建数组最简单的方法就是使用array函数.它接收一切序列型的对象(包括其他数组),然后产生一个新的含有传入数据的Numpy数组. array函数创建数组 import ...
- Android 解决NestedScrollView 嵌套 RecyclerView出现的卡顿,上拉刷新无效
解决卡顿的方法最简单的就是设置RecyclerView的android:nestedScrollingEnabled="false",放弃自己的滑动,交给外部的NestedScro ...
- sudo的安装
关于mini版的centos7的是不能直接sudo命令的 下面我来说一下怎么让sudo命令生效 第一步 先切换到root用户: su - 第二步 visudo 给相应用户分配sudo的权限 第三 ...
- pmp证书