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. 格式化用jad反编译混淆过的代码,能去大部分错误 (zhuanzai)

    http://blog.csdn.net/chruan/article/details/8484783

  2. iptables 添加,删除,查看,修改

      iptables是linux系统自带的防火墙,功能强大,学习起来需要一段时间,下面是一些习iptables的时候的记录.如果iptables不熟悉的话可以用apf,是一款基于iptables的防火 ...

  3. Logstash 介绍

    Logstash 介绍: Logstash 是一个开源的数据收集引擎具有实时管道能力, Logstash 可以动态的统一数据从不同的来源和使数据规范化到你选择的目的地. 当Logstash 起初驾驭创 ...

  4. elasticsearch 修改内存

    [elk@zjtest7-redis bin]$ cat elasticsearch.in.sh if [ "x$ES_MIN_MEM" = "x" ]; th ...

  5. Delphi 使用 Format格式话字符串的用法

    找出以前的代码,写个随笔, 又想起以前的公司和以前的同事来, 希望以前的公司和同事事事顺意. //返回格式化的字符串function TfrmLedgerExVoucherLoad.GetFormat ...

  6. Excel2010 柱形图与折线图制表

    示例1: 数据格式 问题:现在要用柱形图表示手机网民数和年增长率,横轴表示年份,纵轴(1)表示手机网民数,纵轴折线图(2)表示年增长率,要做在一个图表中,请问该怎么做? 步骤: 1.选择A/B/C所在 ...

  7. 自制USB wifi信号放大天线

    这是我的usb wifi天线第一个版本,灵感来自: http://www.instructables.com/id/EQARE4I72GEPUCHTHU/ http://www.usbwifi.orc ...

  8. Thinkphp显示系统常量信息的方法(php的用法)

    输入 :public function Main()    {        dump(get_defined_constants(true));    }显示系统信息, 其中: 'APP_PATH' ...

  9. Gstreamer 中的playback插件

    1. PLAYBACK插件基本介绍 在早期的版本中同时存在playbin和playbin2,但是在最新的版本中,playbin2已经稳定,取代了playbin, playbin不再进行维护.下面是官网 ...

  10. transition过渡的趣玩

    本例中将三张图(来自网络)进行堆叠,鼠标悬停触发.附有源代码