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 int maxm = 600010 ;
const int maxn = 110 ;
const int inf = 0x3f3f3f3f;
int dp[maxm] ;
struct Edge
{
    int v , w ;
    int next ;
}edge[maxm] ;
int head[maxn] ;
int v[maxn] ;
int nedge ;
int dis[maxn] ;
int vis[maxn] ;
int n , m ;
void addedge(int u , int v , int w)
{
    edge[nedge].v = v ;
    edge[nedge].w = w ;
    edge[nedge].next = head[u] ;
    head[u] = nedge++;
}
int dijkstra()
{
    memset(vis , 0 ,sizeof(vis)) ;
    for(int i = 1;i <= n;i++)
    dis[i] = inf ;
    dis[0] = 0 ;
    int u = 0;
    int sum = 0 ;
    while(1)
    {
        int mi = inf ;int pos ;
        for(int i = 0;i <= n;i++)
        if(!vis[i] && dis[i] < mi)
        mi = dis[pos = i] ;
        if(mi == inf)break;
        sum += mi ;
        vis[pos] = 1;
        for(int i = head[pos] ; i != -1 ;i = edge[i].next)
        {
            int v  = edge[i].v ;
            dis[v] = min(dis[v] , dis[pos] + edge[i].w) ;
        }
    }
    return sum ;
}
int main()
{
   // freopen("in.txt" ,"r" , stdin) ;
    int T ;
    scanf("%d" ,&T) ;
    while(T--)
    {
        scanf("%d%d" ,&n , &m) ;
        memset(head , -1 , sizeof(head)) ;
        nedge = 0 ;
        int sum = 0 ;
        while(m--)
        {
            int u ,v , w ;
            scanf("%d%d%d" ,&u , &v , &w) ;
            addedge(u , v , w) ;
            addedge(v , u , w) ;
        }
        for(int i = 1;i <= n;i++)
        scanf("%d" ,&v[i]) ,sum+=v[i] ;
        int tmp = dijkstra() ;
        memset(dp , 0 , sizeof(dp)) ;
        for(int i = 1;i <= n;i++)
        {
            if(dis[i] == inf)continue ;
            for(int j = tmp ;j >= dis[i];j--)
            dp[j] = max(dp[j] , dp[j-dis[i]] + v[i]) ;
        }
        int ans = inf;
        for(int i = 0;i <= tmp ;i++)
        if(dp[i] >= (sum)/2 + 1)
        {
            ans = i ;
            break;
        }
        if(ans == inf)puts("impossible");
        else
        cout<<ans<<endl;
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-11 02:17:31

hdu3339 In Action 最短路+01背包的相关文章

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

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

In Action(最短路+01背包)

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

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

hdu3339 In Action(Dijkstra+01背包)

1 /* 2 题意:有 n 个站点(编号1...n),每一个站点都有一个能量值,为了不让这些能量值连接起来,要用 3 坦克占领这个站点!已知站点的 之间的距离,每个坦克从0点出发到某一个站点,1 unit distance costs 1 unit oil! 4 最后占领的所有的站点的能量值之和为总能量值的一半还要多,问最少耗油多少! 5 6 */ 7 8 /* 9 思路:不同的坦克会占领不同的站点,耗油最少那就是路程最少,所以我们先将从 0点到其他各点的 10 最短距离求出来!也就是d[i]的

HDU3339 In Action 【SPFA】+【01背包】

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

hdu 3339 In Action(迪杰斯特拉+01背包)

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

nyoj 203 三国志(最短路加01背包)

三国志 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描述 <三国志>是一款很经典的经营策略类游戏.我们的小白同学是这款游戏的忠实玩家.现在他把游戏简化一下,地图上只有他一方势力,现在他只有一个城池,而他周边有一些无人占的空城,但是这些空城中有很多不同数量的同种财宝.我们的小白同学虎视眈眈的看着这些城池中的财宝. 按照游戏的规则,他只要指派一名武将攻占这座城池,里面的财宝就归他所有了.不过一量攻占这座城池,我们的武将就要留守,不能撤回.因为我们的小白手下有无数的武将,

ZOJ1366经典dp(多重背包转01背包+优化空间)

1 //zoj1366类似背包的问题 2 //争取一遍AC 3 #include<iostream> 4 #include<string.h> 5 #include<stdio.h> 6 #define maxn 13 7 using namespace std; 8 9 int k[maxn]; 10 int n1[maxn]; 11 int c1[maxn]; 12 int c2[105]; 13 int dp[110000]; 14 int Cash,N1,N2;