1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
| #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<queue> #include<map> #include<stack> #include<cmath> #include<set> #include<bitset> #include<assert.h> #define inc(i,l,r) for(int i=l;i<=r;i++) #define dec(i,l,r) for(int i=l;i>=r;i--) #define link(x) for(edge *j=h[x];j;j=j->next) #define mem(a) memset(a,0,sizeof(a)) #define ll long long #define eps 1e-6 #define succ(x) (1<<x) #define lowbit(x) (x&(-x)) #define sqr(x) ((x)*(x)) #define NM 5005 #define nm 100005 #define pi 3.1415926535897931 using namespace std; ll read(){ ll x=0,f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); return f*x; }
struct edge{int t,v;edge*next;}e[nm],*h[NM],*o=e,*_h[NM]; void add(int x,int y,int v){o->t=y;o->v=v;o->next=h[x];h[x]=o++;} void _add(int x,int y,int v){o->t=y;o->v=v;o->next=_h[x];_h[x]=o++;} int n,m,_p,_x,_y,qh,qt,q[NM],_t,v[NM],_n,_d[NM],__d[NM]; ll d[NM],dp[NM],ans; int p[NM]; struct tmp{ int x;ll d; bool operator<(const tmp&o)const{return d>o.d;} };
void dij(){ priority_queue<tmp>q; inc(i,1,n)__d[i]=1e9; q.push(tmp{_n+1,__d[_n+1]=0}); while(!q.empty()){ int t=q.top().x,cnt=q.top().d;q.pop(); if(cnt!=__d[t])continue; link(t)if(__d[j->t]>__d[t]+j->v)q.push(tmp{j->t,__d[j->t]=__d[t]+j->v}); } } #define _link(x) for(edge *j=_h[x];j;j=j->next) void _dij(){ priority_queue<tmp>q; inc(i,1,n)_d[i]=1e9; q.push(tmp{_n+1,_d[_n+1]=0}); while(!q.empty()){ int t=q.top().x,cnt=q.top().d;q.pop(); if(cnt!=_d[t])continue; _link(t)if(_d[j->t]>_d[t]+j->v)q.push(tmp{j->t,_d[j->t]=_d[t]+j->v}); } }
bool check(ll t){ q[qh=qt=1]=0;v[0]=0; inc(i,1,n){ while(qh<qt&&v[q[qh+1]]<=i)qh++; v[q[qh]]=max(v[q[qh]],i+1); dp[i]=dp[q[qh]]+(i-q[qh]-1)*(d[i]-d[q[qh]])+t; p[i]=p[q[qh]]+1; while(qh<=qt&&dp[q[qt]]+(v[q[qt]]-q[qt]-1)*(d[v[q[qt]]]-d[q[qt]])>=dp[i]+(v[q[qt]]-i-1)*(d[v[q[qt]]]-d[i]))qt--; int s=i; if(qh<=qt){ s=n+1; for(int x=v[q[qt]]+1,y=n;x<=y;){ int mid=x+y>>1; if(dp[q[qt]]+(mid-q[qt]-1)*(d[mid]-d[q[qt]])>=dp[i]+(mid-i-1)*(d[mid]-d[i])) s=mid,y=mid-1;else x=mid+1; } } if(s<=n)v[i]=s,q[++qt]=i; } return p[n]<m; }
int main(){ n=read();_n=read();m=read();_p=read(); while(_p--){_x=read();_y=read();_t=read();add(_x,_y,_t);_add(_y,_x,_t);} dij();_dij(); n=_n; inc(i,1,n)d[i]+=__d[i]+_d[i]; sort(d+1,d+1+n);reverse(d+1,d+1+n); inc(i,1,n)d[i]+=d[i-1]; for(ll x=0,y=d[n]*n;x<=y;){ ll mid=x+y>>1; if(check(mid))y=mid-1;else ans=dp[n]-mid*m,x=mid+1; } printf("%lld\n",ans); return 0; }
|