http://acm.hdu.edu.cn/showproblem.php?pid=3339

这道题就是dijkstra+01背包,先求一遍最短路,再用01背包求。

 #include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 100000
using namespace std;
const int inf=<<; int n,m,st,ed,c;
int dis[maxn];
bool vis[maxn];
int g[][];
int val[maxn];
int dp[maxn]; void dijkstra()
{
memset(vis,false,sizeof(vis));
for(int i=; i<=n; i++) dis[i]=(i==?:inf);
for(int i=; i<=n; i++)
{
int m=inf,x;
for(int y=; y<=n; y++) if(!vis[y]&&dis[y]<m) m=dis[x=y];
vis[x]=true;
for(int y=; y<=n; y++) dis[y]=min(dis[y],dis[x]+g[x][y]);
}
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
memset(val,,sizeof(val));
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
if(i==j) g[i][j]=;
else g[i][j]=inf;
}
}
for(int i=; i<m; i++)
{
scanf("%d%d%d",&st,&ed,&c);
g[st][ed]=g[ed][st]=min(g[st][ed],c);
}
int ans=;
for(int i=; i<=n; i++)
{
scanf("%d",&val[i]);
ans+=val[i];
}
dijkstra();
int mm=ans;
for(int i=; i<=mm; i++)
{
dp[i]=inf;
}
dp[]=;
for(int i=; i<=n; i++)
{
if(dis[i]!=inf)
{
for(int v=mm; v>=val[i]; v--)
{
dp[v]=min(dp[v],dp[v-val[i]]+dis[i]);
}
}
}
mm=mm/+;
int m1=inf;
for(int i=mm; i<=ans; i++)
{
m1=min(m1,dp[i]);
}
if(m1>=inf) printf("impossible\n");
else printf("%d\n",m1);
}
return ;
}

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

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

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

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

  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【最短路+01背包模板/主要是建模看谁是容量、价值】

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

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

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

  9. HDOJ 3339 In Action

    最短路+01背包 In Action Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

随机推荐

  1. Window Ghosting(仍可拖动失去响应的窗口,因为我们真正的窗口已经让系统用Ghosting窗口替代了。使用IsHungAppWindow 探测)

    最近工作中遇到Window Ghosting这个问题, 感觉挺有意思,这里简单记录下.     在XP时代我们的程序没有响应后只能通过任务管理器强制杀掉,但是Vista之后情况变了, 我们仍然可以拖动 ...

  2. 【转】Android中定时器的3种实现方法

    原文网址:http://www.android-study.com/pingtaikaifa/508.html 在Android开发中,定时器一般有以下3种实现方法: 一.采用Handler与线程的s ...

  3. poj3696:同余方程,欧拉定理

    感觉很不错的数学题,可惜又是看了题解才做出来的 题目大意:给定一个数n,找到8888....(x个8)这样的数中,满足能整除n的最小的x,若永远无法整除n 则输出0 做了这个题和后面的poj3358给 ...

  4. 符号表实现(Symbol Table Implementations)

    符号表的实现有很多方式,下面介绍其中的几种. 乱序(未排序)数组实现 这种情况,不需要改变数组,操作就在这个数组上执行.在最坏的情况下插入,搜索,删除时间复杂度为O(n). 有序(已排序)数组实现 这 ...

  5. hdu4622-Reincarnation(后缀自动机)

    Problem Description Now you are back,and have a task to do:Given you a string s consist of lower-cas ...

  6. 设置MATLAB中figure的背景为白色

    matlab的图形窗口每次背景都是灰色的,而我希望每次都是白色的背景,方便用图: 每次总是需要添加figure('color','w');或者figure('color',[1 1 1])或者set( ...

  7. poj 2836 Rectangular Covering(状态压缩dp)

    Description n points are given on the Cartesian plane. Now you have to use some rectangles whose sid ...

  8. java Color

    通过16进制颜色值获取颜色方法:Color.decode("#E0EEEE"); --后继续补充

  9. 5分钟精通git教程

    git是一个版本控制工具,就要先弄清楚什么是版本 版本: 对外发布的版本如v1.0.0,v1.1.0 叫version 内部代码的版本叫commit,如:修改了一个错别字 顾名思义一个version就 ...

  10. Difference between enabled and userInteractionEnabled properties

    I read through the documentation, and here are my findings. UIButton inherits from UIControl the boo ...