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. FJ省队集训最终测试 T2

    思路:发现如果一个人一共选了x个点,那么选中某一个点对的概率都是一样的,一个人选x个点的总方案是C(n,x),一个人选中某个点对的总方案是C(n-2,x-2),这样,那么选中某个点对的概率就是 x*( ...

  2. 【转】如何解决Ubuntu终端里面显示路径名称太长

    原文网址:http://jingyan.baidu.com/article/3d69c5516c129df0ce02d77b.html Ubuntu 默认的终端下面,进入很多层的目录后,前面那个提示符 ...

  3. Struct2(三) Struct2 标签

    在上一篇 Struct2(二)中,我们新建了工程Struct2test用来验证hello World 程序,在index.jsp中,我们添加了一个Struct2 uri 标签用来创建一个指向hello ...

  4. 线性时间构造普吕弗(Prüfer)序列

    tree -> sequence 首先预处理数组 deg[N], deg[i]表示编号为i的节点的度数,我们每次要删除的节点肯定是 满足deg[i]=1 的编号最小节点, 首先找到所有叶子并选出 ...

  5. yaml 1.6 操作

    /** * Copyright (c) 2008, http://www.snakeyaml.org * * Licensed under the Apache License, Version 2. ...

  6. iphone UIScrollView缩放

    allImageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)]; allImageScrol ...

  7. springMVC之annotation优化

    1.在之前配置的spring配置文件中会有这样的代码: <!-- 方法映射 -->  <bean class="org.springframework.web.servle ...

  8. [Javascript] Task queue & Event loop.

    Javascript with Chorme v8 engine works like this : For Chorme engine, v8, it has call stack. And all ...

  9. 随着时间的推移:构造SDK路径错误(An error occurred while automatically activating bundle com.android.ide.eclipse.adt)

    在进行Android应用的开发过程中,有时候在配置SDK路径的时候(Windows->Preferences->Android).会出现例如以下报错:An error occurred w ...

  10. CodeSmith使用总结--调用自定义方法

    上一篇读取了一个表的内容,但是到了真正应用的时候还是不够用的,我们很容易可以对比出来,SQL里边的数据类型的定义和C#中有所不同,比如Saler--String,大写的String在C#中不是一个类型 ...