HDU 3660

    Alice and Bob's Trip

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. 
Input
There 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. 
Output
If 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

题意:有n个景点,形成了一棵树,Bob和Alice要从树根处走到一个叶子节点处,Bob和Alice轮流决定下一步走那一条路,Bob要使长度最长,Alice要使长度最短,但是总长度

不能超过R,不能小于L。这题一边dfs过的,加上输入挂,就过了

#include<cstdio>
#include<cstring>
#include<vector>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=500005;
const int INF=0x3f3f3f3f;
int ans[maxn];
int n,l,r;

struct node
{
    int u,w;
    node(int u=0,int w=0):u(u),w(w) {}
};
vector<node> v[maxn];

namespace fastIO
{
#define BUF_SIZE 100000
bool IOerror=0;
inline char nc()
{
    static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
    if(p1==pend)
    {
        p1=buf;
        pend=buf+fread(buf,1,BUF_SIZE,stdin);
        if(pend==p1)
        {
            IOerror=1;
            return -1;
        }
    }
    return *p1++;
}

inline bool blank(char ch)
{
    return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
}

inline void read(int &x)
{
    char ch;
    while(blank(ch=nc()));
    if(IOerror)
        return ;
    for(x=ch-'0'; (ch=nc())>='0'&&ch<='9'; x=x*10+ch-'0');
}
#undef BUF_SIZE
};

void dfs(int fa,int i,int p)
{
    if(ans[i]>r)
    {
        ans[i]=-1;
        return ;
    }
    if(fa!=-1&&v[i].size()==1)
    {
        if(ans[i]<l)
            ans[i]=-1;
        else
            ans[i]=0;
        return ;
    }
    int x=ans[i];
    int x1=-1,x2=INF;
    for(int j=0; j<v[i].size(); j++)
    {
        int u=v[i][j].u;
        if(u==fa)
            continue;
        int w=v[i][j].w;
        ans[u]=x+w;
        dfs(i,u,p+1);
        if(ans[u]>=0)
        {
            if(p%2==1)
                x1=max(x1,ans[u]+w);
            else
                x2=min(x2,ans[u]+w);
        }
    }
    if(p%2==1)
        ans[i]=x1;
    else
        ans[i]=x2;
    if(ans[i]==INF)
        ans[i]=-1;
}



int main()
{
    while(fastIO::read(n),!fastIO::IOerror)
    {
        fastIO::read(l);
        fastIO::read(r);
        for(int i=0; i<n; i++)
            v[i].clear();
        for(int i=0; i<n-1; i++)
        {
            int x,y,c;
            fastIO::read(x);
            fastIO::read(y);
            fastIO::read(c);
            v[x].push_back(node(y,c));
            v[y].push_back(node(x,c));
        }
        ans[0]=0;
        dfs(-1,0,1);
        if(ans[0]==-1)
            printf("Oh, my god!\n");
        else
            printf("%d\n",ans[0]);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值