跳到正文

题解

题解:P4524 [COCI2017-2018#4] Ceste

sunzihang约 5 分钟
原文
文章信息与版本

文章信息

发布
2024年9月18日
收录
2025年12月12日
更新
2026年11月20日
篇幅
约 5 分钟
获赞
7
题目
P4524

版本

  1. v1当前

将代价中 TT 之和看作横坐标,CC 之和看作纵坐标。

容易发现,对终点相同的任意三种路径,记它们的代价点分别为 a,b,ca,b,c,若存在 ba→×bc→≥0\overrightarrow{ba}\times\overrightarrow{bc}\ge0 ,则 bb 对应的路径一定不优。

因此考虑如何求出以每个点 i(2≤i≤n)i(2\le i\le n) 为终点的路径,其代价点构成的凸包。

类似 wqs 二分,从大向小(即从贴近 xx 轴向远离 xx 轴)处理凸包上的每种斜率,每次用 dijkstradijkstra 跑出当前的最短路。

然后找到一条边 (u,v)(u,v),使得用其从 uu 转移到 vv 得到的代价点 QQ,在 vv 最短路径的代价点 PP 的左上方,且构成的向量 PQ→\overrightarrow{PQ} 的斜率最大(即最贴近 xx 轴),并用这个斜率进行下一次处理。

因为每条边最多只会被我们找到一次,所以时间复杂度 O(m2log⁡n)O(m^2\log n)。

C++
#include<bits/stdc++.h>
using namespace std;
#define LL long long
struct op{
	int x,y;
	op(const int&aa=0,const int&bb=0):x(aa),y(bb){}
	op operator+(const op&aa)const{return op(x+aa.x,y+aa.y);}
	LL operator*(const op&aa)const{return 1ll*x*aa.x+1ll*y*aa.y;}
}ux[4005],li[2005];
struct po{
	LL x;int y;
	po(const LL&aa=0,const int&bb=0):x(aa),y(bb){}
	bool operator<(const po&aa)const{return x>aa.x;}
};
int s,d,o,i,bx[4005],ax[2005],cx[4005];priority_queue<po>A;const int inf=5e6;bool us[2005];LL an[2005];
const LL nf=2e13;
template<typename T>inline void read(T &n){
	T w=1;n=0;char ch=getchar();
	while(!isdigit(ch)&&ch!=EOF){if(ch=='-')w=-1;ch=getchar();}
	while(isdigit(ch)&&ch!=EOF)n=(n<<1)+(n<<3)+(ch&15),ch=getchar();
	n*=w;
}
void wk(op x){
	A.push(po(0,1)),memset(us,0,sizeof us);for(int g=2;g<=s;g++)li[g]=op(inf,inf);
	while(!A.empty()){
		po kk=A.top();A.pop();while(us[kk.y]&&!A.empty())kk=A.top(),A.pop();if(us[kk.y])break;us[kk.y]=1;
		for(int g=ax[kk.y];g;g=bx[g]){
			op ki=li[kk.y]+ux[g];LL kz=ki*x;
			if(kz<li[cx[g]]*x||kz==li[cx[g]]*x&&ki.y>li[cx[g]].y)li[cx[g]]=ki,A.push(po(kz,cx[g]));
		}
	}
	for(int g=2;g<=s;g++)if(li[g].x<inf)an[g]=min(an[g],1ll*li[g].x*li[g].y);
}
int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	read(s),read(d);for(int g=2;g<=s;g++)an[g]=nf;
	for(int g=1;g<=d;g++)read(o),read(i),read(ux[g*2].x),read(ux[g*2].y),ux[g*2-1]=ux[g*2],bx[g*2-1]=ax[o],
	ax[o]=g*2-1,cx[g*2-1]=i,bx[g*2]=ax[i],ax[i]=g*2,cx[g*2]=o;
	wk(op(1,inf));
	while(1){
		op kz(0,1);
		for(int g=1;g<=s;g++)for(int h=ax[g];h;h=bx[h]){
			op ki=li[g]+ux[h];
			if(ki.x<li[cx[h]].x&&ki.y>li[cx[h]].y){
				op k1(ki.x-li[cx[h]].x,ki.y-li[cx[h]].y);if(1ll*kz.x*k1.y-1ll*kz.y*k1.x>0)kz=k1;
			}
		}
		if(!kz.x)break;wk(op(kz.y,-kz.x));
	}
	for(int g=2;g<=s;g++)if(an[g]<nf)cout<<an[g]<<'\n';else cout<<"-1\n";
	return 0;
}
原文
存档评论5 条抓取于