现在的位置: 首页 > 综合 > 正文

hdu 3339 最短路+01背包

2013年03月23日 ⁄ 综合 ⁄ 共 2839字 ⁄ 字号 评论关闭

In Action

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2557    Accepted Submission(s): 838


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
 
这道题真是让我无比蛋疼~~先是理解错题~然后各种细节wa个不停~~~最后好不容易测试出~~
#include <iostream>
#include <cstring>
#include <cstdio>
#define inf 99999999
using namespace std;
int map[105][105];
int d[105],n;
int v[105];
int dp[100005];
void dijkstra()
{
    int i;
    int vis[105];
    memset(vis,0,sizeof(vis));
    for(i=0;i<=n;i++)
        d[i]=inf;
    d[0]=0;
    for(i=0;i<=n;i++)
    {
        int x,y,m=inf;
        for(y=0;y<=n;y++)
            if(d[y]<m&&!vis[y])
                m=d[x=y];
        vis[x]=1;
        for(y=0;y<=n;y++)
        {
            if(map[x][y]!=-1&&d[y]>d[x]+map[x][y])
                d[y]=d[x]+map[x][y];
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int m;
        scanf("%d%d",&n,&m);
        int i;
        memset(map,-1,sizeof(map));
        for(i=0;i<m;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            if(map[u][v]==-1||w<map[u][v])
                map[u][v]=map[v][u]=w;    //有重边,一定要细心额~~
        }
        int s=0;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&v[i]);
            s=s+v[i];
        }
        dijkstra();
        for(i=0;i<=s;i++)            //先是写成i 0-n~~~
            dp[i]=inf;
        dp[0]=0;
        int j;
        for(i=1;i<=n;i++)
        {
            for(j=s;j>=v[i];j--)
            {
                dp[j]=min(dp[j],dp[j-v[i]]+d[i]);
            }
        }
        int mm;
        mm=s/2+1;             //是大于一般 不是大于等于~~~额~汗 太不仔细了
        int min=inf;
        for(i=mm;i<=s;i++)
        {
            if(dp[i]<min)       //先是dp[i]!=inf break;后来仔细想想不对,
                 min=dp[i];     //有可能i大但dp[i]小~!
                                //原因是dp[i]表示恰好装满i最小价值,
                                //有可能dp[i]没有恰好装满导致dp[i]=inf,
                                //而dp[i+1]恰好装满dp[i+1]<inf!!

        }
        if(min==inf)
            printf("impossible\n");
        else
            printf("%d\n",min);

    }
    return 0;
}

抱歉!评论已关闭.