• 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 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.
     
    Input
    The 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.
     
    Output
    The 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
     
    Author
    Lost@HDU
     
    Source
     
    Recommend
    lcy   |   We have carefully selected several similar problems for you:  1385 3338 1102 3342 1598 
     
    先用迪杰斯特拉求出从0点到各点的最短距离,由于需要大于一半总能量,所以需要判断是否取这个点,使用01背包解决。
     
    题意:第一个数为数据组数,然后输入n,m,代表有n个发电站,m条路,然后给出m条路和该路消耗的油,然后是n行,每行是相应的发电站的能量,要注意,每个发电站必须停一辆坦克才能控制这个发电站,只有控制的总能量超过一半能使其瘫痪
     
    附上代码:
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #define M 105
     5 #define inf 0x3f3f3f3f
     6 using namespace std;
     7 int map[M][M],dis[M],vis[M];
     8 int dp[10005];
     9 int mins(int a,int b)
    10 {
    11     return a>b?b:a;
    12 }
    13 int main()
    14 {
    15     int i,j,n,m,T;
    16     scanf("%d",&T);
    17     while(T--)
    18     {
    19         scanf("%d%d",&n,&m);
    20         memset(dis,0,sizeof(dis));
    21         memset(vis,0,sizeof(vis));
    22         for(i=0; i<=n; i++)
    23             for(j=0; j<=n; j++)
    24                 if(i==j) map[i][j]=0;
    25                 else     map[i][j]=inf;
    26         int a,b,c;
    27         while(m--)
    28         {
    29             scanf("%d%d%d",&a,&b,&c);
    30             if(map[a][b]>c)
    31                 map[a][b]=map[b][a]=c;
    32         }
    33         vis[0]=1;
    34         for(i=0; i<=n; i++)
    35             dis[i]=map[0][i];
    36         int min,t;
    37         for(i=0; i<=n; i++)  //迪杰斯特拉
    38         {
    39             min=inf;
    40             for(j=0; j<=n; j++)
    41                 if(!vis[j]&&min>dis[j])
    42                 {
    43                     min=dis[j];
    44                     t=j;
    45                 }
    46             vis[t]=1;
    47             for(j=0; j<=n; j++)
    48                 if(!vis[j]&&map[t][j]<inf)
    49                     if(dis[j]>dis[t]+map[t][j])
    50                         dis[j]=dis[t]+map[t][j];
    51         }
    52         int s[M],sum=0;
    53         for(i=1; i<=n; i++)
    54         {
    55             scanf("%d",&s[i]);
    56             sum+=s[i];
    57         }
    58         for(i=1; i<=sum; i++)
    59             dp[i]=inf;
    60         dp[0]=0;
    61         for(i=1; i<=n; i++)     //01背包
    62             for(j=sum; j>=s[i]; j--)
    63                 dp[j]=mins(dp[j],dp[j-s[i]]+dis[i]);
    64         int x=sum/2+1,mm=inf;
    65         for(i=x; i<=sum; i++)  //选出最小的距离
    66             if(dp[i]<mm)
    67                 mm=dp[i];
    68         if(mm<inf)
    69             printf("%d
    ",mm);
    70         else
    71             printf("impossible
    ");
    72     }
    73     return 0;
    74 }
  • 相关阅读:
    IE, FireFox, Opera 浏览器支持CSS实现Alpha半透明的方法
    5个CSS3技术实现设计增强
    SQL Server 2005 中的分区表和索引
    推荐12款可用于前端开发的免费文本编辑器
    960 Grid System
    初识Byte
    在线制作网站
    sqlserver操作符篇 优化
    ASP.NET 异常处理
    Photoshop 隐藏的快捷键
  • 原文地址:https://www.cnblogs.com/pshw/p/5463281.html
Copyright © 2020-2023  润新知