杭电3339(Floyd+dijkstra最短路+01背包)
In Action(难度:1)
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
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和n个发电站,每个发电站有一定的能量,从0出发到发电站需要消耗一定的油量。每一次都从基地出发,到达某个发电站就摧毁它,当至少摧毁所有发电站的能量总和的一半时,就输出所花费的油量的总和。
分析:
(1)每个发电站到基地的最短距离:dijkstra算法;
(2)从n个发电站中找出几个发电站的能量和至少为总能量和的一半并且这几个站到基地的距离和最小:01背包,把n个站到基地的总距离和看成背包的最大容量,能量看成价值,每个背包恰好被装满,求花最少的费用能得到的最大价值。
递推公式:dp[k]=min(dp[k],dp[k-pw[j]]+dis[j]),dp[k]为当走到第k个基站时所得到的最大价值。
AC代码:
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <math.h>
using namespace std;
#define INF 0x3f3f3f3f
#define maxn 105
int path[maxn][maxn];//两点间距离
int dp[11000];
int dis[maxn];
int vis[maxn];
int pw[maxn];
int n;
void floyd()
{
for(int k=0;k<=n;k++)
{
for(int i=0;i<=n;i++)
{
if(path[i][k]!=INF)
{
for(int j=0;j<=n;j++)
{
if(path[i][j]>path[i][k]+path[k][j])
{
path[i][j]=path[i][k]+path[k][j];
}
}
}
}
}
}
void dijkstra()//src为起始结点
{
int k=-1,min;
//初始化所有节点都不在最短路径中
memset(vis,0,sizeof(vis));
//初始化从起始结点到结点i的时间
for(int i=1;i<=100;i++)
{
dis[i]=path[0][i];
}
//起始结点到起始结点时间为0,并标记为已走
dis[0]=0;
vis[0]=1;
//依次标记n-1个结点
for(int i=1;i<=n;i++)
{
//初始化
min=INF;
//找到i所直接连通的结点中具有最短时间的结点
for(int j=1;j<=n;j++)
{
if(!vis[j]&&min>dis[j])
{
//更新最短时间,保存达这个点
min=dis[j];
k=j;
}
}
if(k==-1) break;
//标记这个点
vis[k]=1;
//判断是起始结点到j短,还是经过k连接j更短
for(int j=1;j<=n;j++)
{
if(!vis[j]&&dis[j]>dis[k]+path[k][j])
{
dis[j]=dis[k]+path[k][j];
}
}
}
}
int main()
{
int m,t,st,ed,dist;
cin>>t;
while(t--)
{
cin>>n>>m;
//init
for(int i=0;i<=n;i++)
{
for(int j=0;j<=n;j++)
{
if(i==j) path[i][j]=0;
else path[i][j]=INF;
}
}
for(int i=0;i<m;i++)
{
cin>>st>>ed>>dist;
if(path[st][ed]>dist) path[st][ed]=path[ed][st]=dist;
}
int total=0;
for(int i=1;i<=n;i++)
{
cin>>pw[i];
total+=pw[i];
}
/*dijkstra();
memset(dp,0x3f,sizeof(dp));
dp[0]=0;
for(int i=1;i<=n;i++)
{
for(int j=total;j>=pw[i];j--)
{
dp[j]=min(dp[j],dp[j-pw[i]]+dis[i]);
}
}*/
floyd();
memset(dp,INF,sizeof(dp));
dp[0]=0;
for(int i=1;i<=n;i++)
{
for(int j=total;j>=pw[i];j--)
{
if(dp[j]>dp[j-pw[i]]+path[0][i])
{
dp[j]=dp[j-pw[i]]+path[0][i];
}
}
}
int ans=INF;
for(int i=total/2+1;i<=total;i++)
{
if(ans>dp[i]) ans=dp[i];
}
if(ans!=INF) cout<<ans<<endl;
else cout<<"impossible"<<endl;
}
return 0;
}