In Action(SPFA+01背包)

来源:互联网 发布:java 运行jar 依赖包 编辑:程序博客网 时间:2024/06/05 04:40

In Action
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4720 Accepted Submission(s): 1553

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
开始没有读懂题意,就是有一个电网,每一个坦克可以控制一个电站,现在这些坦克都在0处,问怎样安排坦克去的电站使的耗油量最少并且能控制多于一半的电量.
SPFA求出0到每个点的距离,然后01背包;

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <queue>#include <map>#include <algorithm>#define INF 0x3f3f3f3fusing namespace std;typedef long long LL;const int MAX = 1e5+10;int n,m;int Map[110][110],Dp[10010];int Dis[110];bool vis[110];int va[110];void SPFA()//求最短路{    memset(vis,false,sizeof(vis));    memset(Dis,INF,sizeof(Dis));    Dis[0]=0;    vis[0]=true;    queue<int>Q;    Q.push(0);    while(!Q.empty())    {        int u=Q.front();        Q.pop();        for(int i=0;i<=n;i++)        {            if(Dis[u]+Map[u][i]<Dis[i])            {                Dis[i]=Map[u][i]+Dis[u];                if(!vis[i])                {                    Q.push(i);                    vis[i]=true;                }            }        }        vis[u]=false;    }}int main(){    int T;    int u,v,w;    int sum;    scanf("%d",&T);    while(T--)    {        memset(Map,INF,sizeof(Map));        scanf("%d %d",&n,&m);        sum=0;        for(int i=1;i<=m;i++)        {            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",&va[i]);            sum+=va[i];        }        SPFA();        memset(Dp,INF,sizeof(Dp));        Dp[0]=0;        for(int i=1;i<=n;i++)//01背包        {            for(int j=sum;j>=va[i];j--)            {                Dp[j]=min(Dp[j],Dp[j-va[i]]+Dis[i]);            }        }        int Max=INF;        for(int i=sum/2+1;i<=sum;i++)        {            if(Dp[i]<Max)            {                Max=Dp[i];            }        }        if(Max==INF)        {            printf("impossible\n");        }        else        {            printf("%d\n",Max);        }    }    return 0;}
0 0
原创粉丝点击