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.

InputThe 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.OutputThe 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
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = +;
const int maxm = 1e6 + ;
const double PI = acos(-1.0);
const double eps = 1e-;
const int dx[] = {-,,,,,,-,-};
const int dy[] = {,,,-,,-,,-};
int dir[][] = {{,},{,-},{-,},{,}};
const int mon[] = {, , , , , , , , , , , , };
const int monn[] = {, , , , , , , , , , , , };
int t,n,m,u,v,w,x,y,p[maxn],tot,sum;
int vis[maxn],dp[maxn];
int dis[maxn];
struct node
{
int u,v,w,nxt;
}e[maxn<<];
int head[maxn];
void init()
{
tot=;
ms(head,-);
ms(dp,INF);
ms(dis,INF);
ms(vis,);
ms(e,INF);
}
void add(int u,int v,int w)
{
e[tot].v=v;
e[tot].w=w;
e[tot].nxt=head[u];
head[u]=tot++;
} void spfa()
{
queue<int>q;
for(int i=;i<=n;i++)
vis[i]=,dis[i]=INF;
vis[]=,dis[]=;
q.push();
while(!q.empty())
{
int u = q.front(); q.pop();
vis[u]=;
for(int i=head[u]; i!=-; i=e[i].nxt)
{
int v = e[i].v;
if(dis[v] > dis[u]+e[i].w)
{
dis[v] = dis[u]+e[i].w;
if(!vis[v])
{
vis[v]=;
q.push(v);
}
}
}
}
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init();
for(int i=;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
spfa();
sum=;
for(int i=;i<=n;i++)
{
scanf("%d",&p[i]); //电量
sum += p[i]; //总发电量
}
dp[]=;
for(int i=; i<=n; i++) //个数
{
for(int j=sum; j>=p[i] ;j--)
{
dp[j] = min(dp[j], dp[j-p[i]]+dis[i]);
}
} int ans=INF;
for(int i=sum/+; i<=sum; i++)
ans=min(ans,dp[i]);
if(ans>=INF) cout<<"impossible"<<endl;
else cout<<ans<<endl;
}
}
/*
【题意】
题意是,有若干发电站,有无数的坦克,每个发电站有一个发电量,
每个坦克能到达的发电站的发电量要达到总的发电量的一半以上,这个发射装置才能被摧毁,否则不能。
但是到达每个发电站都有一段距离,坦克的耗油量 = 这段距离,现在要求最少耗油量来摧毁这个发射装置。 【类型】
最短路+01背包 【分析】
首先是求,基地0能到达的每个发电站的最短路
然后用01背包求出消耗的最少油量。要是所能到达的发电站产生的电量都没有超过总产电量的一半,则无法摧毁。
以总路径为背包,点的能量值为价值,对每个点只有去和不去两种状态,用01背包解决。 【时间复杂度&&优化】 【trick】
在t组数据中sum忘记清零了! 【数据】
spfa:218MS
dij:187MS
*/

SPFA+01背包

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = +;
const int maxm = 1e6 + ;
const double PI = acos(-1.0);
const double eps = 1e-;
const int dx[] = {-,,,,,,-,-};
const int dy[] = {,,,-,,-,,-};
int dir[][] = {{,},{,-},{-,},{,}};
const int mon[] = {, , , , , , , , , , , , };
const int monn[] = {, , , , , , , , , , , , };
int t,n,m,u,v,w,x,y,p[maxn],tot,sum;
int vis[maxn],dp[maxn];
int dis[maxn];
struct node
{
int u,v,w,nxt;
}e[maxn<<];
struct cmp
{
bool operator()(int a,int b)
{
return dis[a]>dis[b];
}
};
int head[maxn];
void init()
{
tot=;
ms(head,-);
ms(dp,INF);
ms(dis,INF);
ms(vis,);
ms(e,INF);
}
void add(int u,int v,int w)
{
e[tot].v=v;
e[tot].w=w;
e[tot].nxt=head[u];
head[u]=tot++;
} void spfa()
{
priority_queue<int,vector<int>,cmp >q;
for(int i=;i<=n;i++)
dis[i]=INF;
//vis[0]=1;
dis[]=;
q.push();
while(!q.empty())
{
int u = q.top(); q.pop();
//vis[u]=0;
for(int i=head[u]; i!=-; i=e[i].nxt)
{
int v = e[i].v;
if(dis[v] > dis[u]+e[i].w)
{
dis[v] = dis[u]+e[i].w;
q.push(v); }
}
}
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init();
for(int i=;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
spfa();
sum=;
for(int i=;i<=n;i++)
{
scanf("%d",&p[i]); //电量
sum += p[i]; //总的发电量
}
dp[]=;
for(int i=; i<=n; i++) //个数
{
for(int j=sum; j>=p[i] ;j--) //电量
{
dp[j] = min(dp[j], dp[j-p[i]]+dis[i]);
}
} int ans=INF;
for(int i=sum/+; i<=sum; i++)
ans=min(ans,dp[i]);
if(ans>=INF) cout<<"impossible"<<endl;
else cout<<ans<<endl;
}
}

堆优化dij+01背包

HDU 3339 In Action【最短路+01背包模板/主要是建模看谁是容量、价值】的更多相关文章

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

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

  2. hdu3339In Action(最短路+01背包)

    http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=259#problem/H Description Since 1945, whe ...

  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

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

  6. HDU-3339 IN ACTION(Dijkstra +01背包)

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

  7. HDU 2602 - Bone Collector - [01背包模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Many years ago , in Teddy’s hometown there was a ...

  8. 01背包模板、全然背包 and 多重背包(模板)

    转载请注明出处:http://blog.csdn.net/u012860063 贴一个自觉得解说不错的链接:http://www.cppblog.com/tanky-woo/archive/2010/ ...

  9. 【洛谷P2722 USACO】 总分 01背包模板

    P2722 总分 Score Inflation 题目背景 学生在我们USACO的竞赛中的得分越多我们越高兴. 我们试着设计我们的竞赛以便人们能尽可能的多得分,这需要你的帮助 题目描述 我们可以从几个 ...

随机推荐

  1. [Luogu 2146] NOI2015 软件包管理器

    [Luogu 2146] NOI2015 软件包管理器 树剖好题. 通过对题目的分析发现,这些软件构成一棵树,\(0\) 是树根. 每下载一个软件,需要下载根到这个软件的路径上的所有软件: 每卸载一个 ...

  2. 【CodeForces】601 D. Acyclic Organic Compounds

    [题目]D. Acyclic Organic Compounds [题意]给定一棵带点权树,每个点有一个字符,定义一个结点的字符串数为往下延伸能得到的不重复字符串数,求min(点权+字符串数),n&l ...

  3. 深入分析Spring 与 Spring MVC容器(山东数漫江湖)

    1 Spring MVC WEB配置 Spring Framework本身没有Web功能,Spring MVC使用WebApplicationContext类扩展ApplicationContext, ...

  4. bzoj 1856 组合

    这道题有些类似卡特兰数的其中一种证明,总方案数是c(n+m,n),点(m,n)对应y=x-1对称点为(n+1,m-1),所以答案为c(n+m,n)-c(n+m,n+1). 反思:开始坐标轴画错了,结果 ...

  5. Java 中的方法内部类

    方法内部类就是内部类定义在外部类的方法中,方法内部类只在该方法的内部可见,即只在该方法内可以使用. 一定要注意哦:由于方法内部类不能在外部类的方法以外的地方使用,因此方法内部类不能使用访问控制符和 s ...

  6. Coursera在线学习---第八节.K-means聚类算法与主成分分析(PCA)

    一.K-means聚类中心初始化问题. 1)随机初始化各个簇类的中心,进行迭代,直到收敛,并计算代价函数J. 如果k=2~10,可以进行上述步骤100次,并分别计算代价函数J,选取J值最小的一种聚类情 ...

  7. deepin 快捷键

    从此脱离鼠标

  8. Eclipse中SVN更改连接用户

    Eclipse中安装了SVN插件,当连接到SVN服务器后,便无法从客户端更改连接帐号 百度一下,也就知道 查看Eclipse中使用的是什么SVN Interface,位置在windows > p ...

  9. 采用dlopen、dlsym、dlclose加载动态链接库【转】

    转自:http://www.cnblogs.com/Anker/p/3746802.html 1.前言 为了使程序方便扩展,具备通用性,可以采用插件形式.采用异步事件驱动模型,保证主程序逻辑不变,将各 ...

  10. DTW 算法(转)

    DTW为(Dynamic Time Warping,动态时间归准)的简称.应用很广,主要是在模板匹配中,比如说用在孤立词语音识别,计算机视觉中的行为识别,信息检索等中.可能大家学过这些类似的课程都看到 ...