In Action

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5102    Accepted Submission(s):
1696

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
 
Author
Lost@HDU
 
Source
 
Recommend
lcy   |   We have carefully selected several similar
problems for you:  1385 3338 1102 3342 1598 
 
先用迪杰斯特拉求出从0点到各点的最短距离,由于需要大于一半总能量,所以需要判断是否取这个点,使用01背包解决。
 
题意:第一个数为数据组数,然后输入n,m,代表有n个发电站,m条路,然后给出m条路和该路消耗的油,然后是n行,每行是相应的发电站的能量,要注意,每个发电站必须停一辆坦克才能控制这个发电站,只有控制的总能量超过一半能使其瘫痪
 
附上代码:
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#define M 105
#define inf 0x3f3f3f3f
using namespace std;
int map[M][M],dis[M],vis[M];
int dp[];
int mins(int a,int b)
{
return a>b?b:a;
}
int main()
{
int i,j,n,m,T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
memset(dis,,sizeof(dis));
memset(vis,,sizeof(vis));
for(i=; i<=n; i++)
for(j=; j<=n; j++)
if(i==j) map[i][j]=;
else map[i][j]=inf;
int a,b,c;
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
if(map[a][b]>c)
map[a][b]=map[b][a]=c;
}
vis[]=;
for(i=; i<=n; i++)
dis[i]=map[][i];
int min,t;
for(i=; i<=n; i++) //迪杰斯特拉
{
min=inf;
for(j=; j<=n; j++)
if(!vis[j]&&min>dis[j])
{
min=dis[j];
t=j;
}
vis[t]=;
for(j=; j<=n; j++)
if(!vis[j]&&map[t][j]<inf)
if(dis[j]>dis[t]+map[t][j])
dis[j]=dis[t]+map[t][j];
}
int s[M],sum=;
for(i=; i<=n; i++)
{
scanf("%d",&s[i]);
sum+=s[i];
}
for(i=; i<=sum; i++)
dp[i]=inf;
dp[]=;
for(i=; i<=n; i++) //01背包
for(j=sum; j>=s[i]; j--)
dp[j]=mins(dp[j],dp[j-s[i]]+dis[i]);
int x=sum/+,mm=inf;
for(i=x; i<=sum; i++) //选出最小的距离
if(dp[i]<mm)
mm=dp[i];
if(mm<inf)
printf("%d\n",mm);
else
printf("impossible\n");
}
return ;
}

hdu 3339 In Action(迪杰斯特拉+01背包)的更多相关文章

  1. HDU 3339 In Action(迪杰斯特拉+01背包)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3339 In Action Time Limit: 2000/1000 MS (Java/Others) ...

  2. HDU 3339 In Action【最短路+01背包】

    题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=3339] In Action Time Limit: 2000/1000 MS (Java/Other ...

  3. HDU 3339 In Action【最短路+01背包模板/主要是建模看谁是容量、价值】

     Since 1945, when the first nuclear bomb was exploded by the Manhattan Project team in the US, the n ...

  4. HDU 2680 最短路 迪杰斯特拉算法 添加超级源点

    Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  5. HDU 2544最短路 (迪杰斯特拉算法)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2544 最短路 Time Limit: 5000/1000 MS (Java/Others)    Me ...

  6. HDU 3790(两种权值的迪杰斯特拉算法)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3790 最短路径问题 Time Limit: 2000/1000 MS (Java/Others)    ...

  7. HDU 1874畅通工程续(迪杰斯特拉算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Time Limit: 3000/1000 MS (Java/Others)     ...

  8. hdu 1142(迪杰斯特拉+记忆化搜索)

    A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  9. 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 ...

随机推荐

  1. 八.DBN深度置信网络

    BP神经网络是1968年由Rumelhart和Mcclelland为首的科学家提出的概念,是一种按照误差反向传播算法进行训练的多层前馈神经网络,是目前应用比较广泛的一种神经网络结构.BP网络神经网络由 ...

  2. golang函数二

  3. ubuntn 18 开起ssh 并用root远程登陆

    原文:ubuntn 18 开起ssh 并用root远程登陆 版权声明:本文为博主原创文章,随意转载. https://blog.csdn.net/Michel4Liu/article/details/ ...

  4. 如何在Liferay 7中用html显示页面

    liferay portlet默认的显示页面是view.jsp,虽然可以在jsp中用include标签包括html文件,但是如何直接通过修改配置文件让默认的显示页面为view.html呢? 1.用Li ...

  5. JavaScript--结合CSS变形、缩放能拖拽的登录框

    上例图: 代码块: <!DOCTYPE html> <html> <head lang="en"> <meta charset=" ...

  6. 2019.11.12htmlhomework1

    ex: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...

  7. retailMall-vuedemo1

    //home.vue <template> <div class="home"> <div class="top-info"> ...

  8. Maximum Depth of Binary Tree 树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  9. hdu2897 巴什博奕

    n%(q+p)==0,也就是说先手必胜; n%(q+p)<=p,先手必输; n%(q+p)==k if(k>p&&k<=q)先手必胜; if(k>p&& ...

  10. LeetCode91 Decode Ways

    题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...