zoj 3031 / cdoj 430 - Robotruck - Southwestern Europe 2007
来源:互联网 发布:2303am网络打印 编辑:程序博客网 时间:2024/06/05 17:09
O(n^2)代码:
#define CLEAR(a) memset((a),0,sizeof((a)))const int maxn=1e5+10;const int inf=1<<30;struct Point{ int x,y; Point(){x=0;y=0;}; Point(int x,int y){this->x=x;this->y=y;};} point[maxn];int w[maxn];typedef pair<int,int> pr;int disp(Point p1,Point p2){ return abs(p1.x-p2.x)+abs(p1.y-p2.y);}int f[maxn];int main(){ int T,kase=0; scanf("%d",&T); while(T--) { CLEAR(f);CLEAR(w); int n,c; scanf("%d%d",&c,&n); for(int i=1;i<=n;i++) { int x,y; scanf("%d%d%d",&x,&y,&w[i]); point[i]=Point(x,y); } f[0]=0; //f[1]=2*disp(point[0],point[1]); for(int i=1;i<=n;++i)//若为i++,应如何修改? { int sw=w[i],d=disp(point[0],point[i]); f[i]=inf; for(int j=i-1;sw<=c&&j>=0;--j)//若为j--,应如何修改? { f[i]=min(f[i],f[j]+d+disp(point[0],point[j+1])); sw+=w[j];d+=disp(point[j],point[j+1]); } //cout<<f[i]<<endl; } printf("Case %d: %d\n",++kase,f[n]); } return 0;}
单调队列优化,状态转移方程做了一些调整
#define CLEAR(a) memset((a),0,sizeof((a)))typedef long long LL;const int maxn=1e5+10;const int inf=1<<30;struct Point{ int x,y; Point(){x=0;y=0;}; Point(int x,int y){this->x=x;this->y=y;};} point[maxn];struct pr{ LL first,second; pr(){}; pr(int a,int b):first(a),second(b){}; bool operator< (const pr &a) const { if (second==a.second) return first>a.first; else return second>a.second; }};LL disp(Point p1,Point p2){ return abs(p1.x-p2.x)+abs(p1.y-p2.y);}LL f[maxn],s[maxn],sw[maxn],w[maxn];template <class T>class Priority_Queue{private: T qnode[maxn]; int head,rear;public: Priority_Queue() { head=rear=0; }; bool empty() { return head==rear; } T top() { return qnode[head]; }; void pop() { if (head<rear) head++; } void pop(int id,int c) { while ( head < rear && sw[id] - sw[qnode[head].first-1] > c ) head++; } void push(T node) { while (rear > head && qnode[rear - 1].second > node.second) { rear--; } qnode[rear++]=node; }};int main(){ int T,kase=0; scanf("%d",&T); while(T--) { CLEAR(f);CLEAR(s);CLEAR(sw); int n,c; scanf("%d%d",&c,&n); bool reach=1; for(int i=1;i<=n;i++) { int x,y; scanf("%d%d%lld",&x,&y,&w[i]); if (w[i]>c) reach=0; point[i]=Point(x,y); sw[i]=sw[i-1]+w[i]; s[i]=s[i-1]+disp(point[i-1],point[i]); } f[0]=0; Priority_Queue<pr> q; //q.push(pr(0,0)); for(int i=1;i<=n;++i) { LL d=disp(point[0],point[i]); q.push(pr(i,f[i-1]-s[i]+d)); q.pop(i,c); pr k=q.top(); f[i]=k.second+s[i]+d; //cout<<f[i]<<endl; } if (reach) printf("%lld\n",f[n]); else printf("0\n"); } return 0;}
0 0
- zoj 3031 / cdoj 430 - Robotruck - Southwestern Europe 2007
- Europe - Southwestern - 2006/2007 简明解题报告
- Robotruck - ZOJ 3031 dp
- Regionals 2011 Europe - Southwestern
- LA 3644 X-Plosives (Europe - Southwestern - 2006/2007)
- Regionals 2007 >> Europe - Southwestern Ladies' Choice 稳定婚姻问题 uva live 3989
- 20131005组队赛-Regionals 2010, Europe - Southwestern
- 组队赛131005 Southwestern Europe Regional, SWERC 2010
- Robotruck
- Robotruck
- Regionals 2012, Europe - Southwestern A.Beehives(BFS求无向图最小环)
- Bits Equalizer 组队赛第二场Regionals 2012, Europe - Southwestern B题
- Sentry Robots 组队赛第二场 Regionals 2012, Europe - Southwestern F题
- LA6575 Odd and Even Zeroes (2013-2014 ACM-ICPC Southwestern Europe Regional Contest F题) 规律
- LA6576 VivoParc (2013-2014 ACM-ICPC Southwestern Europe Regional Contest G题) 搜索
- LA6578 Trending Topic (2013-2014 ACM-ICPC Southwestern Europe Regional Contest I题) 模拟
- 2015-2016 ACM-ICPC Southwestern Europe Regional Contest (SWERC 15) C
- Graham算法模板 F 题Saint John Festival 20152016-acmicpc-southwestern-europe-regional-
- 设计模式 ( 十八 ) 策略模式Strategy(对象行为型)
- java排序之shell排序(希尔排序)(四)
- 1009University
- [Egret学习笔记 三]Bitmap Font(位图文字)的使用
- go 速学 - 11 - 结构
- zoj 3031 / cdoj 430 - Robotruck - Southwestern Europe 2007
- uva 10651 状态压缩DP
- TCP/IP详解笔记(9)
- 黑马程序员——C语言基础——用printf函数、数组和循环结构输出特殊图形
- 杭电 HDU ACM 1562 Guess the number
- 第二章第二十题
- 5.9.2 初始化块和构造器
- 写得很好
- 机器导论