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): 5951 Accepted Submission(s): 1998
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<bits/stdc++.h>
using namespace std;
const int INF = 1e7 + ;
const int maxn = ;
int N, M;
int mp[][], val[];
int dis[], inq[];
void SPFA(int st)
{
for(int i = ; i <= N; i++)
dis[i] = INF, inq[i] = ;
dis[st] = ;
queue<int> que;
que.push(st);
inq[st] = ;
while(!que.empty())
{
int u = que.front();
inq[u] = ;
que.pop();
for(int v = ; v <= N; v++)
{
if(v == u) continue;
if(dis[v] > dis[u] + mp[u][v])
{
dis[v] = dis[u] + mp[u][v];
if(!inq[v]) que.push(v), inq[v] = ;
}
}
}
}
int main ()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d", &N, &M);
for(int i = ; i <= N; i++)
{
for(int j = ; j <= N; j++)
mp[i][j] = INF;
mp[i][i] = ;
}
int u, v, len;
for(int i = ; i <= M; i++)
{
scanf("%d%d%d", &u, &v, &len);
if(mp[u][v] > len) mp[u][v] = mp[v][u] = len;
}
SPFA();
int sum = ;
for(int i = ; i <= N; i++)
scanf("%d", &val[i]), sum += val[i];
int dp[sum + ];
for(int i = ; i <= sum; i++)
dp[i] = INF;
dp[] = ;
for(int i = ; i <= N; i++)
for(int j = sum; j >= val[i]; j--)
dp[j] = min(dp[j], dp[j - val[i]] + dis[i]);
int ans = INF;
for(int i = sum / + ; i <= sum; i++)
ans = min(ans, dp[i]);
if(ans == INF)
printf("impossible\n");
else
printf("%d\n", ans);
}
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) ...
- hdu3339In Action(最短路+01背包)
http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=259#problem/H Description Since 1945, whe ...
- 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背包)
In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- hdu 3339 In Action
http://acm.hdu.edu.cn/showproblem.php?pid=3339 这道题就是dijkstra+01背包,先求一遍最短路,再用01背包求. #include <cstd ...
- 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背包)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3339 In Action Time Limit: 2000/1000 MS (Java/Others) ...
- 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 最短路+01背包
In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
随机推荐
- 一款基于react-native的弹窗提示组件
介绍一款基于react-native的弹窗提示插件 react-native-ms , github地址:https://github.com/jiangzhenfei/react-native-ms ...
- 1、编写第一个java程序--Hello—World
1.下载JDK8.0文件 下载网址:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.ht ...
- 海洋CMS v6.53 v6.54命令执行
测试下载地址:https://pan.baidu.com/s/1jHQBKFk 至于分析实在是看的一脸懵逼就不累赘了.直接上exp POST /haiyang/upload/search.php HT ...
- 目标检测-基于Pytorch实现Yolov3(1)- 搭建模型
原文地址:https://www.cnblogs.com/jacklu/p/9853599.html 本人前段时间在T厂做了目标检测的项目,对一些目标检测框架也有了一定理解.其中Yolov3速度非常快 ...
- C++学习之路(十):虚继承引入的执行效率
这篇文章不知道取啥名字了,暂且这样叫,直接看场景就明白了.节选自<深度探索C++对象模型> Point3d origin, *pt = &origin; (1)origin.x = ...
- Linux内核中实现生产者与消费者(避免无效唤醒)【转】
转自:http://blog.csdn.net/crazycoder8848/article/details/42581399 本文关注的重点是,避免内核线程的无效唤醒,并且主要是关注消费者线程的设计 ...
- webstrom 使用git
1.首先进入码云创建项目 2.创建成功,复制https地址,打开webstrom,选择git,填入https的地址 3.下载完成,打开项目,新建一个测试测HTML文件,点击右键,选择git,再选择ad ...
- 【hdoj_2100】Lovekey(大数+字符处理)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2100 根据题目意思,现将字符串转化为10进制,再采用10进制加法相加,再转化为26进制. 另一种直接的思路 ...
- beego离线安装及运行
官网: https://beego.me/ 由于公司上不了网,啥都得下载到本地来弄. go的安装不多说了,GOPATH要设置好的. 先离线下载好https://github.com/astaxie/b ...
- Rule Compilation error xxx cannot be resolved
说明:在eclipse中部署服务器时不报错,但在dos窗口中部署时报如下异常 原因:规则引擎drl文件文件中两个含义相同的变量的中文注释(只能用//,不能使用/**xxx*/或/*xxx*/)要保持相 ...