HDU杭电3339 In Action 【Dijistra+0-1背包】

http://acm.hdu.edu.cn/showproblem.php?pid=3339


Problem Description
HDU杭电3339 In Action 【Dijistra+0-1背包】_第1张图片
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
 
//这也是一大坑题,我一开始想着,只要一直走,走路线最短的路,然后摧毁的power值超过一半  ,消耗的油量就会是最少的,因为每个城市的power值不一样,这也是需要考虑的一方面,所以这里就涉及到取还是不取的问题,0-1背包

这是我一开始的代码

#include <stdio.h>
#include <string.h>
#define INF 0x3f3f3f3f
int map[220][220];
int low[220];
bool vis[220];
int N,M;
int power[220];
int sum;
int Dijistra()
{
	int cnt=0;
	int res=0;
	int pos=0;
	int i,j;
	memset(vis,0,sizeof(vis));
	memset(low,INF,sizeof(low));
	for(i=1;i<=N;++i)
	{
		low[i]=map[0][i];
	}
	vis[0]=1;
	low[0]=0;
	for(i=1;i<=N;++i)
	{
		int min=INF;
		for(j=0;j<=N;++j)
		{
			if(!vis[j]&&min>low[j])
			{
				min=low[j];
				pos=j;
			}
		}
		if(min==INF) return -1;
		vis[pos]=1;
		cnt+=power[pos];
		if(cnt>sum/2) return low[pos];
		for(j=1;j<=N;++j)
		{
			if(!vis[j]&&low[j]>map[pos][j]+low[pos])
			{
				low[j]=map[pos][j]+low[pos];
			}
		}
	}
	return -1;
}
int main()
{
	int T;
	int u,v,w;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&N,&M);
		memset(map,INF,sizeof(map));
		while(M--)
		{
			scanf("%d%d%d",&u,&v,&w);
			if(map[u][v]>w)
				map[u][v]=map[v][u]=w;
		}
		sum=0;
		for(int i=1;i<=N;++i)
		{
			scanf("%d",power+i);
			sum+=power[i];
		}
		int ans=Dijistra();
		if(ans==-1) printf("impossible\n");
		else printf("%d\n",ans);
	}
	return 0;
}

//AC代码

#include <stdio.h>
#include <string.h>
#define min(a,b) (a<b?a:b)
#define INF 0x3f3f3f3f
int dp[210010];
int map[220][220];
int low[220];
bool vis[220];
int N,M;
int power[220];
int sum;
int Dijistra()
{
    int pos=0;
    int i,j;
    memset(vis,0,sizeof(vis));
    memset(low,INF,sizeof(low));
    for(i=1;i<=N;++i)
    {
        low[i]=map[0][i];
    }
    vis[0]=1;
    low[0]=0;
    for(i=1;i<=N;++i)
    {
        int min=INF;
        for(j=0;j<=N;++j)
        {
            if(!vis[j]&&min>low[j])
            {
                min=low[j];
                pos=j;
            }
        }
//        if(min==INF) return -1;
        vis[pos]=1;
//        cnt+=power[pos];
//        if(cnt>sum/2) return low[pos];
        for(j=1;j<=N;++j)
        {
            if(!vis[j]&&low[j]>map[pos][j]+low[pos])
            {
                low[j]=map[pos][j]+low[pos];
            }
        }
    }
//    return -1;
}
int main()
{
    int T;
    int u,v,w;
    int i,j;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&N,&M);
        memset(map,INF,sizeof(map));
        while(M--)
        {
            scanf("%d%d%d",&u,&v,&w);
            if(map[u][v]>w)
                map[u][v]=map[v][u]=w;
        }
        sum=0;
        for(i=1;i<=N;++i)
        {
            scanf("%d",power+i);
            sum+=power[i];
        }
        Dijistra();
        for(i = 1; i<=sum; i++)
            dp[i] = INF;
        dp[0] = 0;
        for(i = 1; i<=N; i++)//01背包找出最小油耗量
        {
            for(j = sum; j>=power[i]; j--)
                dp[j] = min(dp[j],dp[j-power[i]]+low[i]);
        }
        int x = sum/2+1;//必须大于一半,等于都不行
        int minn = INF;
        for(i = x; i<=sum; i++)
        {
            if(dp[i]<minn)
                minn = dp[i];
        }
        if(minn == INF)
            printf("impossible\n");
        else
            printf("%d\n",minn);
    }
    return 0;
}



你可能感兴趣的:(HDU杭电3339 In Action 【Dijistra+0-1背包】)