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 ...
随机推荐
- UNIX IPC: POSIX 消息队列 与 信号
POSIX消息队列可以注册空队列有消息到达时所触发的信号,而信号触发对应的信号处理函数. 下面是一份基本的消息队列和信号处理结合的代码(修改自UNIX网络编程:进程间通信) #include < ...
- cfE. Ehab and a component choosing problem(贪心)
题意 题目链接 给出一棵树,每个节点有权值,选出\(k\)个联通块,最大化 \[\frac{\sum_{i \in S} a_i}{k}\] Sol 结论:选出的\(k\)个联通块的大小是一样的且都等 ...
- git 远程代码回退
git reflog git reset --hard commitId git push -f
- 03.CSS动画-->自定义动画
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- win10 程序crash后弹出 XXX已停止工作
需要attach调试器的时候弹出的"XXX已停止工作"很方便, 现在win10默认禁用掉了. 恢复的方法是: win+R 输入gpedit.msc回车 管理模板 -> Win ...
- sqlserver查询数据可编辑方法
原文:http://www.cnblogs.com/DaphneOdera/p/6418592.html 第一步.选中表右键,点击编辑前200行. 第二步.数据展示页面点击下图中红线框中的sql按钮 ...
- Android的事件分发(dispatchTouchEvent),拦截(onInterceptTouchEvent)与处理(onTouchEvent)
在Android中,View的结构是树状的,所以,当触发触摸事件的时候,其事件传递也是从上之下一层层的传递.下面我们结合例子来一点点进行分析. 首先,我们需要了解事件处理中的几个方法: 1.在View ...
- win10命令控制符
IP:ipconfigIP地址侦测器:Nslookup显卡:dxdiag控制面板:control电话拨号:dialer木马捆绑工具,系统自带:iexpress本地用户和组:lusrmgr.msc鼠标属 ...
- Box(视图组件)如何在多个页面不同视觉规范下的复用
本文来自 网易云社区 . 问题描述 Android App中的页面元素,都是由一个个Box(可以理解成一个个自定义View组件和Widget同级)组成,这些Box可以在不同的页面.不同的模块达到复用的 ...
- windows&cmd常用命令&快捷键
1: cls即CLear Screen 功能:清除屏幕上的所有显示 2:Win+Tab快捷键进行3D窗口切换还有比较快速切换窗口的新方法. 3:利用Alt+Tab快捷键进行切换窗口时,在桌面中间会显示 ...