QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#21329 | #1271. In Search of Gold | FudanU1# | AC ✓ | 1852ms | 7700kb | C++17 | 2.7kb | 2022-03-04 15:49:22 | 2022-05-08 02:53:29 |
Judging History
answer
#include <stdio.h>
#include <algorithm>
using namespace std;
struct node {
long long v , a , b;
node *next;
} pool[41000] , *g[21000];
long long top;
long long n , k;
long long dp[21000][22];
long long lim;
void clear () {
long long i;
for ( i = 1 ; i <= top ; i++ ) pool[i] = pool[0];
for ( i = 1 ; i <= n ; i++ ) {
g[i] = NULL;
}
top = 0;
}
void add ( long long u , long long v , long long a , long long b ) {
node *tmp = &pool[++top];
tmp -> v = v; tmp -> a = a; tmp -> b = b; tmp -> next = g[u]; g[u] = tmp;
}
void merge ( long long *now , long long *child ) {
long long i , j;
static long long tmp[22];
for ( i = 0 ; i <= k ; i++ ) tmp[i] = -1;
for ( i = 0 ; i <= k ; i++ ) {
for ( j = 0 ; i + j <= k ; j++ ) {
if ( now[i] != -1 && child[j] != -1 ) {
if ( now[i] + child[j] <= lim ) {
if ( tmp[i+j] == -1 ) tmp[i+j] = max ( now[i] , child[j] );
else tmp[i+j] = min ( tmp[i+j] , max ( now[i] , child[j] ) );
}
}
}
}
for ( i = 0 ; i <= k ; i++ ) {
now[i] = tmp[i];
}
}
void dfs ( long long i , long long from , long long a , long long b ) {
for ( node *j = g[i] ; j ; j = j -> next ) if ( j -> v != from ) {
dfs ( j -> v , i , j -> a , j -> b );
merge ( dp[i] , dp[j->v] );
}
if ( i != 1 ) {
for ( long long j = k ; j >= 1 ; j-- ) {
if ( dp[i][j] == -1 && dp[i][j-1] == -1 ) dp[i][j] = -1;
else if ( dp[i][j] == -1 ) dp[i][j] = dp[i][j-1] + a;
else if ( dp[i][j-1] == -1 ) dp[i][j] = dp[i][j] + b;
else dp[i][j] = min ( dp[i][j] + b , dp[i][j-1] + a );
if ( dp[i][j] > lim ) dp[i][j] = -1;
}
if ( dp[i][0] != -1 ) dp[i][0] = dp[i][0] + b;
if ( dp[i][0] > lim ) dp[i][0] = -1;
}
//printf ( "%lld %lld %lld %lld %lld\n" , i , from , a , b , dp[3][1] );
}
bool check ( long long x ) {
long long i , j;
lim = x;
//printf ( "check %lld\n" , lim );
for ( i = 1 ; i <= n ; i++ ) {
for ( j = 0 ; j <= k ; j++ ) {
dp[i][j] = -1;
}
dp[i][0] = 0;
}
dfs ( 1 , -1 , -1 , -1 );
/*for ( i = 1 ; i <= n ; i++ ) {
for ( j = 0 ; j <= k ; j++ ) {
printf ( "%lld %lld %lld\n" , i , j , dp[i][j] );
}
}*/
if ( dp[1][k] != -1 ) return true;
return false;
}
void work () {
long long i , u , v , a , b;
long long l , r , mid;
scanf ( "%lld%lld" , &n , &k );
for ( i = 1 ; i < n ; i++ ) {
scanf ( "%lld%lld%lld%lld" , &u , &v , &a , &b );
add ( u , v , a , b );
add ( v , u , a , b );
}
l = 0; r = 1000000000000000ll;
while ( l < r - 1 ) {
mid = (l+r) / 2;
if ( check ( mid ) == true ) r = mid;
else l = mid;
}
printf ( "%lld\n" , r );
}
int main () {
int t;
scanf ( "%d" , &t );
while ( t-- ) {
work ();
clear ();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3688kb
input:
1 4 1 1 2 1 3 2 3 4 2 2 4 3 5
output:
6
result:
ok single line: '6'
Test #2:
score: 0
Accepted
time: 1852ms
memory: 7700kb
input:
1118 10 5 1 2 557878805 99156035 2 3 170460737 198842212 3 4 473592718 245654078 4 5 774731915 3786984 1 6 817584282 305447690 1 7 308601560 633840726 3 8 718662215 102379861 3 9 26761157 849561804 6 10 617758160 117754666 10 6 1 2 952221943 224077095 2 3 101056818 462900286 3 4 760307950 560511059 ...
output:
1411481343 3753603561 2451798440 2414772415 3307453190 4490065261 4414121261 2816978868 2555185013 3116086232 3159869324 1582942446 1213751604 1927788364 2504746732 2508553278 3014059043 2439597035 2303205388 2110653290 3081993716 3699114788 1916042583 2021128541 2303200787 3850983146 2870883724 319...
result:
ok 1118 lines