POJ 3660 Alice and Bob's Trip //2010 Asia Regional Harbin
2010-10-03 09:37
260 查看
Alice and Bob's Trip
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 617 Accepted Submission(s): 169
Problem Description
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
Source
2010 Asia Regional Harbin
Recommend
lcy
好水的树状DP,竟然写完了,编译没错误,而且1Y
#include<cstdio>
#include<cstring>
const int V=500010;
const int inf=0x7fffffff;
struct EDGE
{
int v,val,next;
}edge[V*2];
int head[V];
int n,l,r;
int e;
void addedge(int u,int v,int c)
{
edge[e].v=v;
edge[e].val=c;
edge[e].next=head[u];
head[u]=e++;
}
int dfs(int u,int sum,int mark)
{
int v,w;
if(mark==1)
{
int big=-1;
for(int i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].v;
w=edge[i].val;
if(sum+w>r) continue;
if(head[v]==-1)
{
if(sum+w<l) continue;
if(sum+w>big) big=sum+w;
}
else
{
int t=dfs(v,sum+w,0);
if(t==-1) continue;
if(t>big) big=t;
}
}
if(big==-1) return -1;
else return big;
}
else
{
int sam=inf;
for(int i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].v;
w=edge[i].val;
if(sum+w>r) continue;
if(head[v]==-1)
{
if(sum+w<l) continue;
if(sum+w<sam) sam=sum+w;
}
else
{
int t=dfs(v,sum+w,1);
if(t==-1) continue;
if(t<sam) sam=t;
}
}
if(sam!=inf) return sam;
else return -1;
}
}
int main()
{
while(scanf("%d%d%d",&n,&l,&r)!=EOF)
{
e=0;
memset(head,-1,sizeof(head));
for(int i=1;i<n;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
addedge(a,b,c);
}
int t=dfs(0,0,1);
if(t<=r&&t>=l) printf("%d/n",t);
else printf("Oh, my god!/n");
}
return 0;
}
相关文章推荐
- HDU/HDOJ 3666 差分约束 2010 Asia Regional Harbin
- HDU 3724 Encoded Barcodes (2010 Asia Tianjin Regional Contest )
- HDU 3721 Building Roads (2010 Asia Tianjin Regional Contest) - from lanshui_Yang
- 2010 Asia Hangzhou/Fuzhou Regional Contest
- 【拆边最小费用流】【Asia - Harbin - 2010/2011】【Transportation】
- 2010 Asia Regional Tianjin Site —— Online Contest (线段树二维转一维,2-SAT,floyed变形)hdu3621-3631
- HDU 3695 / POJ 3987 Computer Virus on Planet Pandora(AC自动机)(2010 Asia Fuzhou Regional Contest)
- HDU/HDOJ 2446 Shell Pyramid 2008 Asia Regional Harbin
- The ACM/ICPC Asia Harbin Online First Round Contest 2010 Warmup-2
- HDU 3260/POJ 3827 Facer is learning to swim(DP+搜索)(2009 Asia Ningbo Regional)
- hdu 5023 && poj 2777(线段染色)2014 ACM/ICPC Asia Regional 广州 Online
- HDU3718/ZOJ3425 Similarity(The 2010 ACM-ICPC Asia Chengdu Regional Contest,加权二分图的最优匹配)
- HDU3714 Error Curves(2010 Asia Chengdu Regional Contest)
- 【DP】【Asia - Harbin - 2010/2011】【Permutation Counting】
- HDU 3726 Graph and Queries(平衡二叉树)(2010 Asia Tianjin Regional Contest)
- HDU 3265/POJ 3832 Posters(扫描线+线段树)(2009 Asia Ningbo Regional)
- 【差分约束系统】【仍未AC】【Asia - Harbin - 2010/2011】【THE MATRIX PROBLEM】
- hdu 3650 2010 Asia Regional Hangzhou Site —— Online Contest
- HDU 3729 I'm Telling the Truth(2010 Asia Tianjin Regional Contest )
- HDU 3696 Farm Game(拓扑+DP)(2010 Asia Fuzhou Regional Contest)