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

 
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 = 50000+20;
const int maxm = 1e6 + 10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
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<<2];
int head[maxn];
void init()
{
    tot=0;
    ms(head,-1);
    ms(dp,INF);
    ms(dis,INF);
    ms(vis,0);
    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=1;i<=n;i++)
        vis[i]=0,dis[i]=INF;
    vis[0]=1,dis[0]=0;
    q.push(0);
    while(!q.empty())
    {
        int u = q.front(); q.pop();
        vis[u]=0;
        for(int i=head[u]; i!=-1; 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]=1;
                    q.push(v);
                }
            }
        }
    }
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        init();
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
            add(v,u,w);
        }
        spfa();
        sum=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&p[i]); //电量
            sum += p[i];       //总发电量
        }
        dp[0]=0;
        for(int i=1; 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/2+1; 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 = 50000+20;
const int maxm = 1e6 + 10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
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<<2];
struct cmp
{
    bool operator()(int a,int b)
    {
        return dis[a]>dis[b];
    }
};
int head[maxn];
void init()
{
    tot=0;
    ms(head,-1);
    ms(dp,INF);
    ms(dis,INF);
    ms(vis,0);
    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=1;i<=n;i++)
        dis[i]=INF;
    //vis[0]=1;
    dis[0]=0;
    q.push(0);
    while(!q.empty())
    {
        int u = q.top(); q.pop();
        //vis[u]=0;
        for(int i=head[u]; i!=-1; 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=0;i<m;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
            add(v,u,w);
        }
        spfa();
        sum=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&p[i]); //电量
            sum += p[i]; //总的发电量
        }
        dp[0]=0;
        for(int i=1; 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/2+1; i<=sum; i++)
            ans=min(ans,dp[i]);
        if(ans>=INF)    cout<<"impossible"<<endl;
        else   cout<<ans<<endl;
    }
}

堆优化dij+01背包

原文地址:https://www.cnblogs.com/Roni-i/p/9440973.html

时间: 2024-08-08 19:35:11

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

hdu3339 In Action 最短路+01背包

//有n个电站,每一个电站能提供不同的power, //所有tank从0点出发,停在该电站就代表摧毁了该电站,tank从电站到电站之间需要耗费能量 //现在有无穷的tank,问最少需要耗费多少油能够摧毁一半以上的power //先用dijkstra求得到每个点的dis[i] //然后用一个01背包得到答案 #include<cstdio> #include<cstring> #include<iostream> using namespace std ; const i

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

In Action Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3869    Accepted Submission(s): 1237 Problem Description Since 1945, when the first nuclear bomb was exploded by the Manhattan Project t

hdu 3339(最短路+01背包)

In Action Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5135    Accepted Submission(s): 1710 Problem Description Since 1945, when the first nuclear bomb was exploded by the Manhattan Project t

HDU 3339 In Action(最短路+DP)

In Action Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4704    Accepted Submission(s): 1547 Problem Description Since 1945, when the first nuclear bomb was exploded by the Manhattan Project

hdu 4044 GeoDefense (树形dp+01背包)

GeoDefense Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 663    Accepted Submission(s): 267 Problem Description Tower defense is a kind of real-time strategy computer games. The goal of towe

HDU 3033 I love sneakers! (01背包+反分组背包)

题意:给你 n,m,k,表示有k种鞋子共n双,你有m的容量: 每双鞋子有容量p和价值v:问是否买全k种鞋子,若能在容量为m的情况下最多能买到鞋子的价值为多少: 每双鞋子只能买一次(01背包),每种鞋子至少买一种(分组背包:每组只能有一个)与传统分组背包的限制相反. 注意初始化!!! #include<cstdio> #include<stdlib.h> #include<string.h> #include<string> #include<map&g

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

转载请注明出处:http://blog.csdn.net/u012860063 贴一个自觉得解说不错的链接:http://www.cppblog.com/tanky-woo/archive/2010/07/31/121803.html 模版就直接贴代码: 01背包模板: /* 01背包问题 01背包问题的特点是,">每种物品仅有一件.能够选择放或不放. 01背包问题描写叙述: 有N件物品和一个容量为V的背包. 第i件物品的重量是c[i],价值是w[i]. 求解将哪些物品装入背包可使这些物品

1085 背包问题(0-1背包模板题)

1085 背包问题(0-1背包模板题)(51NOD基础题) 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 在N件物品取出若干件放在容量为W的背包里,每件物品的体积为W1,W2--Wn(Wi为整数),与之相对应的价值为P1,P2--Pn(Pi为整数).求背包能够容纳的最大价值. Input 第1行,2个整数,N和W中间用空格隔开.N为物品的数量,W为背包的容量.(1 <= N <= 100,1 <= W <= 10000) 第2 - N + 1行,每行

01背包 模板1 2 总结

物品质量 w[0] w[1]  w[2] ....... w[n]                背包容量c               T 物品价值 v[0]  v[1]  v[2]  .......  v[n]              物品种类 n                N (一)设DP(x,y)   表示  从前x项物品中  取出装入  体积为y的背包 的  物品的最大价值. 前x项物品(0---- x-1 , X)   去装          体积容量为y的背包 |------