您的位置:首页 > 其它

hdoj 3339 In Action【最短路+01背包】

2015-11-02 17:52 288 查看

In Action

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 4777 Accepted Submission(s): 1575



[align=left]Problem Description[/align]



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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
The minimal oil cost in this action.

If not exist print "impossible"(without quotes).

[align=left]Sample Input[/align]

2
2 3
0 2 9
2 1 3
1 0 2
1
3
2 1
2 1 3
1
3


[align=left]Sample Output[/align]

5
impossible


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int inf = 0x3f3f3f;
const int mx = 123;
int vis[mx], dis[mx], map[mx][mx], p[mx];
int dp[mx*2000];
int n,m;
void dij(int st)
{
memset(vis, 0, sizeof(vis));
int i,j,k;
for(i = 0; i <= n; i++)
dis[i] = map[st][i];
for(k = 0; k <= n; k++)
{
int minn = inf;
for(i = 0; i<=n; i++)
{
if(dis[i] < minn&& !vis[i])
{
minn = dis[i];
j = i;
}
}
vis[j] = 1;
for(i = 0; i <= n; i++)
{
if(!vis[i] && dis[i] > dis[j]+map[j][i])
dis[i] = dis[j]+map[j][i];
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n, &m);
int i,j,k;
for(i = 0; i <= n; i++)
{
for( j = 0; j <= n; j++)
map[i][j] = inf;
map[i][i] = 0;
}
int a, b, c;
while(m--)
{
scanf("%d%d%d", &a, &b, &c);
if(map[a][b] > c)
map[a][b] = map[b][a] = c;
}
int sum = 0;
for(i = 1; i<=n; i++)
{
scanf("%d" , &p[i]);
sum += p[i];
}
dij(0);
for(i = 1; i <= sum; i++)
dp[i] = inf;
dp[0] = 0;
for(i = 1; i <= n; i++)
{
for(j = sum; j >= p[i]; j--)
{
dp[j] = min(dp[j], dp[j-p[i]]+dis[i]);
}
}
int minn = inf;
int ave = sum/2+1;//一定要比一半多一!
for(i =	ave; i <= sum; i++)
{
if(minn > dp[i])
minn = dp[i];
}
if(minn == inf)
puts("impossible");
else
printf("%d\n",minn);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: