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 ...
随机推荐
- 【BZOJ】4032: [HEOI2015]最短不公共子串(LibreOJ #2123)
[题意]给两个小写字母串A,B,请你计算: (1) A的一个最短的子串,它不是B的子串 (2) A的一个最短的子串,它不是B的子序列 (3) A的一个最短的子序列,它不是B的子串 (4) A的一个最短 ...
- 【转载】iPhone系统概览
iPhone OS OverviewiPhone系统概览iPhone OS comprises the operating system and technologies that you use t ...
- python学习笔记(十二)之函数
牛刀小试: 定义一个无参函数 >>> def myFirstFunc(): ... print("Hello python") ... print("h ...
- Sublime之插件的安装(一)
由于最近刚换了一个工作,所以决定重新申请一个blog,把工作当中遇到的一些问题记录下来,方便自己下次忘记,也希望能与一起需要的小伙伴一起共勉. 如果有不同的观点或者是不同的看法,大家都可以畅谈,我一直 ...
- 大聊PYthon----生成器
再说迭代器与生成器之前,先说一说列表生成式 列表生成式 什么是列表生成式呢? 这个非常简单! 先看看普通青年版的! >>> a [0, 1, 2, 3, 4, 5, 6, 7, 8, ...
- unity3d 资源文件从MAX或者MAYA中导出的注意事项
unity3d 资源文件从MAX或者MAYA中导出的注意事项 1.首先,Unity3d 中,导出带动画的资源有2种导出方式可以选择: 1) 导出资源时,只导出一个文件,保留模型,骨骼和所 ...
- python3-可变和不可变数据类型
可变:[ ] { } 不可变:int str ( ) 应用实例: 把列表l,追加到列表s中,现在网列表l中追加一个5,打印列表s可以看到,列表s中的列表l中也有5. d={&q ...
- python基础===15条变量&方法命名的最佳实践
不同的代码段采用不同的命名长度.通常来说,循环计数器(loop counters)采用1位的单字符来命名,循环判断变量(condition/loop variables)采用1个单词来命名,方法采用1 ...
- 去除\ufeff的解决方法,python语言
语言:python 编程工具:pycharm 硬件环境:win10 64位 读取文件过程中发现一个问题:已有记事本文件(非空),转码 UTF-8,复制到pycharm中,在开始位置打印结果会出现 \ ...
- Zabbix3.0 API调用
Zabbix API 是什么? API简单来说是服务对外开放的一个接口,用户通过该接口传递请求,完成操作.API的背后是一组方法的集合,这些方法实现了服务对应的不同功能,调用API实际上就是换了一种方 ...