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. python之特点

    .python区分大小写:2.注释规范:python使用井号#作为单行注释,且注释的位置,一般放在要注释代码的前一行或这代码的右侧:多行注释则可以用连续三个单引号开始一行,并连续三个单引号在要注释的代 ...

  2. Phpstrom 配置php版本语法支持

  3. 响应式布局及bootstrap(实例)

    说明: 这几天公司要求网站实现响应式布局,所以对响应式布局进行了相对全面的了解,并做了几个实例. 转载请注明源地址,谢谢^_^,http://www.cnblogs.com/liu-zhen/p/44 ...

  4. Winform实现调用asp.net数据接口实例

    本文实例讲述了Winform实现调用asp.net数据接口的方法,分享给大家供大家参考.具体实现方法如下: 一.问题: 最近一个WPF项目需要改写成android项目,思路是在asp.net项目中编写 ...

  5. CI框架--浅谈前后台区分

    谈到CI框架,这是我第二个用到的框架,初步使用过后,眼前一亮.CI框架上手简单.模式明确.适合新手学习框架时入手. 下面给大家讲讲CI框架区分前后台文件的具体做法: 首先在application文件夹 ...

  6. IO流1 --- File类的实例化 --- 技术搬运工(尚硅谷)

    构造器1 File(String pathname) //相对路径 File file1 = new File("hello.txt"); //windows绝对路径 File f ...

  7. 前端如何实现图片懒加载(lazyload) 提高用户体验

    定义 图片懒加载又称图片延时加载.惰性加载,即在用户需要使用图片的时候加载,这样可以减少请求,节省带宽,提高页面加载速度,相对的,也能减少服务器压力. 惰性加载是程序人性化的一种体现,提高用户体验,防 ...

  8. 洛谷3861八月月赛A题解

    链接 用f[i][j]表示乘积为i的,包含的最大数小于等于j时的方案总数 我们先考虑所用的数为1到n的情况 最后的答案就是f[n][n]-1 转移时考虑f[i][j]可以转移到的状态 显然f[i][j ...

  9. MaxCompute技术人背后的故事:从ApacheORC到AliORC

    2019大数据技术公开课第一季<技术人生专访>来袭,本季将带领开发者们探讨大数据技术,分享不同国家的工作体验.本文整理自阿里巴巴计算平台事业部高级技术专家吴刚的专访,将为大家介绍Apach ...

  10. Linux常用命令3 文件搜索命令

    文件搜索非常占用资源,所以尽量不要使用这个命令 避免少用该命令最好的方式是设置好文件夹结构,文件不要乱放 1.文件搜索命令:find 命令名称:find 所在路径:/bin/find 执行权限:所有用 ...