树上对抗搜索 - 树形dp

Alice and Bob are going on a trip. Alice is a lazy girl who wants to minimize the total travelling distance, while Bob as an active boy wants to maximize it. At the same time, they cannot let the value to be less than a given integer L since that will make them miss too much pleasure, and they cannot let the value to be greater than a given integer R since they don't want to get too exhausted.
The city they are visiting has n spots and the spots are connected by directed edges. The spots are connected in such a way that they form a tree and the root will always be at spot 0. They take turns to select which edge to go. Both of them choose optimally. Bob will go first.

InputThere are multiple test cases. For every test case, the first line has three integers, n, L and R (1<=n<=500000, 0<=L, R<=1000000000). The next n-1 lines each has three integers a, b and c, indicating that there is an edge going from spot a to spot b with length c (1<=c<=1000). The spots are labeled from 0 to n-1.
There is a blank line after each test case.
Proceed to the end of file.
OutputIf the total distance is not within the range [L, R], print "Oh, my god!" on a single line. Otherwise, print the most value Bob can get.Sample Input
3 2 4
0 1 1
0 2 5

7 2 8
0 1 1
0 2 1
1 3 1
1 4 10
2 5 1
2 6 5

7 4 8
0 1 1
0 2 1
1 3 1
1 4 2
2 5 1
2 6 5

4 2 6
0 1 1
1 2 1
1 3 5
Sample Output
Oh, my god!
2
6
2

题意 :给你一棵树,以及树上的边权,两人轮流取走,先走的总是希望总路程最大,后走的人则希望总路程最小,但要求总的范围在(l, r)

思路分析:

  思路比较好想,类似数字三角形,只不过这是在树上,从叶子向上去递推,明确在每层是谁去走,去怎么更新当前的结点,但是我的代码超时了...

待更新

代码示例:

const int maxn = 5e5+5;

void in(int &a)
{
    char c;
    while ((c = getchar()) == ' ' || c == '\n');
    a = c - 48;
    while ((c = getchar()) >= '0' && c <= '9')
        a = (a << 3) + (a << 1) + c - 48;
}
int n, l, r;
struct node
{
    int to, cost;
    
    node(int _to=0, int _cost=0):to(_to), cost(_cost){}
};
vector<node>ve[maxn];
int dis[maxn];
int dp[maxn];

void dfs(int x, int fa, int pt){
    if (dis[x] > r) {dp[x]=-1; return;}
    if (ve[x].size() == 1) dp[x] = 0;
    else dp[x] = pt?-1:inf;
    
    for(int i = 0; i < ve[x].size(); i++){
        int to = ve[x][i].to;
        int cost = ve[x][i].cost;
        if (to == fa) continue;
        dis[to] = dis[x]+cost;
        
        dfs(to, x, !pt);
        if (dp[to] == -1 || dp[to] == inf) continue;    
        if (dp[to]+dis[x]+cost >= l && dp[to]+dis[x]+cost <= r){ 
            if (pt == 1) dp[x] = max(dp[x], dp[to]+cost);
            else dp[x] = min(dp[x], dp[to]+cost);
        }
    }
    //printf("++++ x = %d ,pt = %d , dis = %d,  %d\n", x, pt, dis[x], dp[x]);
}

int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    int a, b, c;
    
   while(~scanf("%d%d%d", &n, &l, &r)){
        for(int i = 0; i <= n; i++) ve[i].clear();
        for(int i = 1; i < n; i++){
            in(a), in(b), in(c);  
            ve[a].push_back(node(b, c));
            ve[b].push_back(node(a, c));            
        }
        //memset(dis, 0, sizeof(dis)); 
        //memset(dp, 0, sizeof(dp));
        dfs(0, -1, 1);
        //printf("+++ %d \n", dp[0]);
        if (dp[0] >= l && dp[0] <= r) printf("%d\n", dp[0]);
        else printf("Oh, my god!\n");
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/ccut-ry/p/9193950.html
今日推荐