HDU 3339 in Action (minimum short circuit + 01 backpack)

Source: Internet
Author: User

There are N power stations, each of which has a certain amount of power, and there is a certain distance between the power stations. We need to take some power stations out of 0, make the total power of the occupied power station more than half of the total power, and find the shortest distance to meet the conditions. If possible, the output distance; otherwise, the output is impossible. We know that power stations are connected. As long as is connected to any power station, we can occupy all power stations. If is not connected to any power station, it is impossible, that is to say, the distance between 0 and any power station is infinite.

We have dispatched some tanks to occupy some power stations from, and there is a certain distance between tanks and each power station. After occupying each power station, we can get a certain amount of power, and the distance is equivalent to the volume, power is equivalent to value. Isn't this a 01 backpack? 01 the general question of a backpack is to obtain the maximum value for a given volume. The question here is to obtain the minimum volume that is equal to or greater than the given value. We only need to search from the past to the back and find the first volume greater than the value.

PS: There are two main reasons for the infinite wa at the beginning. One is that the meaning of the question is wrong, ignoring that the selected power station should have a tank there, after saving the shortest path, I traverse all the paths to find the minimum consumption.

In addition, the array size is reduced.

View code

# Include <iostream>
# Include <algorithm>
# Define Maxn (100 + 10)
# Define INF 10000000
Using Namespace STD;
Int G [maxn] [maxn], N;
Int Dist [maxn], p [maxn], DP [maxn * maxn];
Bool Vis [maxn];
Void Dijkstra ()
{
Int I, j, temp, min;
For (I = 1 ; I <= N; I ++)
{
Vis [I] =False ;
Dist [I] = G [ 0 ] [I];
}
For (I = 1 ; I <n; I ++)
{
Min = inf;
For (J = 1 ; J <= N; j ++)
If (! Vis [J] & Dist [J] <min)
{
Temp = J;
Min = DIST [J];
}
If (Min = inf)
Break ;
Vis [temp] = True ;
For (J = 1 ; J <= N; j ++)
If (! Vis [J] & Dist [J]> Dist [temp] + G [temp] [J])
Dist [J] = DIST [temp] + G [temp] [J];
}
}
Int Main ()
{
Int T, M, A, B, C;
Scanf ( " % D " , & T );
While (T --)
{
Scanf ( " % D " , & N, & M );
For ( Int I = 0 ; I <= N; I ++)
For ( Int J =0 ; J <= N; j ++)
{
If (I = J) g [I] [J] = 0 ;
Else
G [I] [J] = inf;
}
While (M --)
{
Scanf ( " % D " , & A, & B, & C );
If (C <G [a] [B])
G [a] [B] = G [B] [a] = C;
}
For ( Int I = 1 ; I <= N; I ++)
Scanf ( " % D " , & P [I]);
Int W = 0 , Sum = 0 ;
Dijkstra ();
Bool Flag =True ;
For ( Int I = 1 ; I <= N; I ++)
{
W + = P [I];
Sum + = DIST [I];
If (Dist [I] = inf)
{
Flag = False ;
Break ;
}
}
If (! Flag)
{
Printf ( " Impossible \ n " );
Continue ;
}
For ( Int I = 0 ; I <= sum; I ++)
DP [I] = 0 ;
For ( Int I =1 ; I <= N; I ++)
For ( Int J = sum; j> = DIST [I]; j --)
DP [J] = max (DP [J], DP [J-Dist [I] + P [I]);
W = W/ 2 + 1 ;
For ( Int I = 1 ; I <= sum; I ++)
If (DP [I]> = W)
{
Printf (" % D \ n " , I );
Break ;
}
}
Return 0 ;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.