UESTC 482 Charitable Exchange (线段树)
来源:互联网 发布:ps照片水彩效果知乎 编辑:程序博客网 时间:2024/06/05 09:06
Description
Have you ever heard a star charity show called Charitable Exchange? In this show, a famous star starts with a small item which values
In each exchange, one can exchange for an item of Vi yuan if he (she) has an item values more than or equal to
Now, you task is help the star to exchange for an item which values more than or equal to
Input
The first line of the input is
For each case, two integers
Output
For every test case, you should output Case #k:
first, where
Sample Input
3
3 10
5 1 3
8 2 5
10 9 2
4 5
2 1 1
3 2 1
4 3 1
8 4 1
5 9
5 1 1
10 4 10
8 1 10
11 6 1
7 3 8
Sample Output
Case #1: -1
Case #2: 4
Case #3: 10
这题需要对于每个价值离散化,价值只有大小关系有用,具体数值是没用的。离散化后就变成了n条边,最多有200000个点,每个点的最小花费是该点到最大值的点之间的花费得的最小值,从左到右扫一便,更新和询问最小值用线段树维护。
#include<cstdio>#include<cstring>#include<cmath>#include<iostream>#include<map>#include<queue>#include<algorithm>using namespace std;struct node{ int l,r,v,next;}p[200005];long long tree[300005*2];long long inf;void update(int rt,int l,int r,int rr,long long v){ if(l>rr||r<rr) return ; if(l==rr&&r==rr) { tree[rt]=v; return ; } int mid=(l+r)/2; update(2*rt,l,mid,rr,v); update(2*rt+1,mid+1,r,rr,v); tree[rt]=min(tree[2*rt+1],tree[2*rt]);}long long query(int rt,int l,int r,int ll,int rr){ if(ll>r||rr<l) return inf; if(ll<=l&&rr>=r) return tree[rt]; int mid=(l+r)/2; long long ret=inf; ret=min(ret,query(2*rt,l,mid,ll,rr)); ret=min(ret,query(2*rt+1,mid+1,r,ll,rr)); return ret;}int h[300005],vis[300005],a[300005],v[3][100005],cnt,r[300005];int n,m,mx;void add(int l,int r,int t){ p[++cnt].l=l; p[cnt].r=r; p[cnt].next=h[l];h[l]=cnt; p[cnt].v=t;}void spfa(int st){ int i,j; for(i=1;i<=mx;i++) { update(1,1,mx,i,inf); vis[i]=0; r[i]=i; } update(1,1,mx,1,0); for(i=1;i<=mx;i++) { int now=i; long long mi=query(1,1,mx,i,mx); for(j=h[now];j>0;j=p[j].next) { int r=p[j].r; long long tp=query(1,1,mx,r,mx); if(tp>mi+p[j].v) { update(1,1,mx,r,mi+p[j].v); } } }}int main(){ int T; scanf("%d",&T); int i,j,k; inf=1000000000; inf*=inf; for(int Case=1;Case<=T;Case++) { scanf("%d%d",&n,&m); cnt=0;memset(h,0,sizeof(h)); map<int,int> f; f[m]=1; a[++cnt]=m; a[++cnt]=1; f[1]=1; for(i=1;i<=n;i++) { scanf("%d%d%d",&v[0][i],&v[1][i],&v[2][i]); for(j=0;j<2;j++) { if(f[v[j][i]]==0) { f[v[j][i]]=1; a[++cnt]=v[j][i]; } } } sort(a+1,a+1+cnt); for(i=1;i<=cnt;i++) f[a[i]]=i; m=f[m]; mx=cnt; cnt=0; for(i=1;i<=n;i++) { add(f[v[1][i]],f[v[0][i]],v[2][i]); } spfa(f[1]); long long ans=inf; ans=query(1,1,mx,m,mx); printf("Case #%d: ",Case); if(ans==inf) printf("-1\n"); else printf("%lld\n",ans); } return 0;}
- UESTC 482 Charitable Exchange (线段树)
- UESTC 482 Charitable Exchange
- UESTC 482 Charitable Exchange(四川省赛B题)
- UESTC 482 Charitable Exchange(优先队列+bfs)
- UESTC 1558 Charitable Exchange
- UESTC 1558 Charitable Exchange
- 最短路,前缀和优化连边,Dijkstra(UESTC 482,Charitable Exchange)
- uestc 482 Charitable Exchange 广搜+优先队列优化
- UESTC 1425(线段树)
- UESTC 1324线段树
- UESTC_1558 Charitable Exchange 弦断树优化dp
- UESTC 841 休生伤杜景死惊开 【线段树】
- uestc 1425 Another LCIS(线段树 最长递增序列)
- uestc 1546 Bracket Sequence(线段树 括号匹配)
- UESTC 1546(线段树,成段更新,区间合并)
- UESTC 1546 Bracket Sequence(线段树 成段更新)
- UESTC -- 1057 秋实大哥与花(线段树)
- UESTC 360 Another LCIS(线段树区间更新)
- ace admin java 整合 开发 后台框架,aceadmin_HTML5 java
- 安装xampp后apache不能启动解决方法
- free()后程序崩溃问题
- 数组与指针
- Hadoop 基本数据类型
- UESTC 482 Charitable Exchange (线段树)
- u-boot 清bss段
- 进程(读写锁)
- 一个APP是否可以没有Activity?
- 之前做的一个关于支付安全的ppt,内容比较简单,分享给有需要的朋友。
- ios数据存储(三)
- Android屏幕density, dip等相关概念总结
- Java 基础——String
- 240. Search a 2D Matrix II