最短路+01背包

In Action

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3857    Accepted Submission(s): 1229

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
 

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm> using namespace std; const int INF=0x3f3f3f3f; int n,m;
int dist[110],vis[110],power[110];
int g[110][110];
int dp[11000]; void dijkstra()
{
memset(dist,63,sizeof(dist));
memset(vis,0,sizeof(vis)); dist[0]=0; for(int j=0;j<=n;j++)
{
int mark=-1,mindist=INF;
for(int i=0;i<=n;i++)
{
if(vis[i]) continue;
if(mindist>dist[i])
{
mindist=dist[i]; mark=i;
}
} if(mark==-1) break;
vis[mark]=1; for(int i=0;i<=n;i++)
{
if(vis[i]) continue;
dist[i]=min(dist[i],dist[mark]+g[mark][i]);
}
}
} int main()
{
int T_T;
scanf("%d",&T_T);
while(T_T--)
{
memset(g,63,sizeof(g)); scanf("%d%d",&n,&m); for(int i=0;i<m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
g[a][b]=g[b][a]=min(g[a][b],c);
} dijkstra(); int sumd=0,sum=0; for(int i=1;i<=n;i++)
{
scanf("%d",power+i);
if(dist[i]!=INF) sumd+=dist[i];
sum+=power[i];
} memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++)
{
if(dist[i]==INF) continue;
for(int j=sumd;j>=dist[i];j--)
{
dp[j]=max(dp[j],dp[j-dist[i]]+power[i]);
}
}
int ans=-1,low=0,high=sumd,mid;
while(low<=high)
{
mid=(low+high)/2;
if(dp[mid]*2>sum)
ans=mid,high=mid-1;
else low=mid+1;
} if(ans==-1)
puts("impossible");
else printf("%d\n",ans);
}
return 0;
}

HDOJ 3339 In Action的更多相关文章

  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 背包+flyod

    In Action Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=333 ...

  4. hdu 3339 In Action (最短路径+01背包)

    In Action Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  5. hdu 3339 In Action(迪杰斯特拉+01背包)

    In Action Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  6. HDU 3339 In Action 最短路+01背包

    题目链接: 题目 In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  7. hdu 3339 In Action

    http://acm.hdu.edu.cn/showproblem.php?pid=3339 这道题就是dijkstra+01背包,先求一遍最短路,再用01背包求. #include <cstd ...

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

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

  9. HDU 3339 In Action(最短路+背包)题解

    思路:最短路求出到每个点的最小代价,然后01背包,求出某一代价所能拿到的最大价值,然后搜索最后结果. 代码: #include<cstdio> #include<set> #i ...

随机推荐

  1. oc21--super

    // // Phone.h #import <Foundation/Foundation.h> typedef enum { kFlahlightStatusOpen, kFlahligh ...

  2. 【POJ 2976】 Dropping Tests

    [题目链接] http://poj.org/problem?id=2976 [算法] 0/1分数规划 [代码] #include <algorithm> #include <bits ...

  3. MYSQL工具之binlog2sql闪回操作

    文档结构: 在生产环境中如果遇到误删,改错数据的情况,利用mysql闪回工具binlog2sql,可以实现数据的快速回滚,从binlog中提取SQL,并能生成回滚SQL语句.Binlog以event作 ...

  4. Java电子书下载地址

    http://www.itpub.net/search.php?searchid=1660&orderby=lastpost&ascdesc=desc&searchsubmit ...

  5. DWR框架在web.xml的完整配置及注释

    <!-- 配置DWR框架 --> <servlet> <servlet-name>dwr</servlet-name> <servlet-clas ...

  6. 微信小程序开发之animation动画实现

    1. 创建动画实例 wx.createAnimation(OBJECT) 创建一个动画实例animation.调用实例的方法来描述动画.最后通过动画实例的export方法导出动画数据传递给组件的ani ...

  7. mysql Seconds_Behind_Master

    通过show slave status查看到的Seconds_Behind_Master,从字面上来看,他是slave落后master的秒数,一般情况下,也确实这样,通过Seconds_Behind_ ...

  8. Algorithms算法题<1.1>

    1.1.27 二项分布.估计用一下代码计算binomial(100,50,0.25)将会产生的递归调用次数: public static double binomial(int N,int k,dou ...

  9. 应用五:Vue之ElementUI 表格Table与分页Pagination组件化

    (注:本文适用于有一定Vue基础或开发经验的读者,文章就知识点的讲解不一定全面,但却是开发过程中很实用的) 在平时的web项目开发过程中,列表分页查询展示应用的很频繁,为了便于阅读并减少代码的冗余,所 ...

  10. 使用jquery获取css的top和left属性

    使用jquery获取css的top和left属性 因为left和top也都是普通的css属性所以可以使用如下代码来获取 var left = $('#test').css('left'); var t ...