HDU 3339 In Action

来源:互联网 发布:中小型网络拓扑及搭建 编辑:程序博客网 时间:2024/05/20 05:56

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3339

In Action

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

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
HDOJ Monthly Contest – 2010.03.06

题意:就是给你n个点,m条边,这n个点的编号分别是1,2,3,,,,,,,,,n. 还有一个起始点的编号一直是0,m行数据每行都有3个数st,ed,dis,表示路的起点,路的终点,路的长度,1单位的长度消耗1单位的油量。最后n行给的数据是编号1,,,,n对应的地点所获得的能量,最后停的位置应该是大于总能量的一般的位置,那么请输出消耗最少的油量,不能到达的话输出impossible

在最短路的基础上用01背包

代码:

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define INF 0x3f3f3f3fint map[1005][1005];int x[1005],way[1005];int flag[1005],sum;int dp[2000000];void dij(int n,int s){    for(int i=0; i<=n; i++)    {        way[i]=map[s][i];    }    for(int i=0; i<n; i++)    {        int min=INF;        int k=0;        for(int j=0; j<=n; j++)        {            if(flag[j]==0&&way[j]<min)            {                min=way[j];                k=j;            }        }        flag[k]=1;        for(int j=0; j<=n; j++)        {            if(flag[j]==0&&way[k]+map[k][j]<way[j])            {                way[j]=way[k]+map[k][j];            }        }    }}int main(){    int t;    scanf("%d",&t);    while(t--)    {        for(int i=0;i<=100;i++)            for(int j=0;j<=100;j++)        {            if(i!=j)                map[i][j]=INF;            else                map[i][j]=0;        }        int n,m;        sum=0;        scanf("%d %d",&n,&m);        for(int i=0;i<m;i++)        {            int u,v,w;            scanf("%d %d %d",&u,&v,&w);            if(map[u][v]>w)            {                map[u][v]=w;                map[v][u]=w;            }        }        for(int i=1;i<=n;i++)        {            scanf("%d",&x[i]);            sum+=x[i];        }        memset(flag,0,sizeof(flag));        flag[0]=1;        dij(n,0);        int minn=INF;        for(int i=0;i<=sum;i++)            dp[i]=INF;        dp[0]=0;//背包里面放的是能量和,背包表示两点之间的最小距离        for(int i=1;i<=n;i++)            for(int j=sum;j>=x[i];j--)            dp[j]=min(dp[j],dp[j-x[i]]+way[i]);        int ss=sum/2+1;        for(int i=ss;i<=sum;i++)//最后满足条件的肯定是能量大于等于总和一半的,所以从ss开始判断            if(dp[i]<minn)            minn=dp[i];        if(minn==INF)            printf("impossible\n");        else            printf("%d\n",minn);    }    return 0;}
原创粉丝点击