题解:P4524 [COCI2017-2018#4] Ceste
将代价中
容易发现,对终点相同的任意三种路径,记它们的代价点分别为
因此考虑如何求出以每个点
类似 wqs 二分,从大向小(即从贴近
然后找到一条边
因为每条边最多只会被我们找到一次,所以时间复杂度
#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;
}