洛谷2149 Elaxia的路线(dp+最短路)
QwQ好久没更新博客了,颓废了好久啊,来补一点东西
题目大意
给定两个点对,求两对点间最短路的最长公共路径。
其中\(n,m\le 10^5\)
比较简单吧
就是跑四遍最短路,然后把最短路上的边拿出来,跑一遍拓扑排序加\(dp\)就OK
对于一条边\(u->v\),满足\(dis[u]+w+disn[v]=dis[t]\)那么这条边就是最短路上的边,其中\(disn[x]\)表示\(t\)到\(x\)的最短路,\(dis[x]\)表示\(s\)到\(x\)的最短路
直接上代码
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#define pa pair<int,int>
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while (!isdigit(ch)) {if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)) {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
const int maxn = 7010;
const int maxm = 2e6+1e2;
int point[maxn],nxt[maxm],to[maxm],val[maxm];
int dis[maxn];
int disn[maxn];
int dis1[maxn];
int disn1[maxn];
int vis[maxn];
int n,m,cnt;
int x[maxm],y[maxm],w[maxm];
int dp[maxn];
int s1,t1,s2,t2;
priority_queue<pa,vector<pa>,greater<pa> > q;
void addedge(int x,int y,int w)
{
nxt[++cnt]=point[x];
to[cnt]=y;
val[cnt]=w;
point[x]=cnt;
}
void dijkstra(int s)
{
memset(vis,0,sizeof(vis));
memset(dis,127/3,sizeof(dis));
dis[s]=0;
q.push(make_pair(0,s));
while (!q.empty())
{
int x = q.top().second;
q.pop();
if (vis[x]) continue;
vis[x]=1;
for (int i=point[x];i;i=nxt[i])
{
int p = to[i];
if (dis[p]>dis[x]+val[i])
{
dis[p]=dis[x]+val[i];
q.push(make_pair(dis[p],p));
}
}
}
}
void dijkstra1(int s)
{
memset(vis,0,sizeof(vis));
memset(disn,127/3,sizeof(disn));
disn[s]=0;
q.push(make_pair(0,s));
while (!q.empty())
{
int x = q.top().second;
q.pop();
if (vis[x]) continue;
vis[x]=1;
for (int i=point[x];i;i=nxt[i])
{
int p = to[i];
if (disn[p]>disn[x]+val[i])
{
disn[p]=disn[x]+val[i];
q.push(make_pair(disn[p],p));
}
}
}
}
void dijkstra2(int s)
{
memset(vis,0,sizeof(vis));
memset(dis1,127/3,sizeof(dis1));
dis1[s]=0;
q.push(make_pair(0,s));
while (!q.empty())
{
int x = q.top().second;
q.pop();
if (vis[x]) continue;
vis[x]=1;
for (int i=point[x];i;i=nxt[i])
{
int p = to[i];
if (dis1[p]>dis1[x]+val[i])
{
dis1[p]=dis1[x]+val[i];
q.push(make_pair(dis1[p],p));
}
}
}
}
void dijkstra3(int s)
{
memset(vis,0,sizeof(vis));
memset(disn1,127/3,sizeof(disn1));
disn1[s]=0;
q.push(make_pair(0,s));
while (!q.empty())
{
int x = q.top().second;
q.pop();
if (vis[x]) continue;
vis[x]=1;
for (int i=point[x];i;i=nxt[i])
{
int p = to[i];
if (disn1[p]>disn1[x]+val[i])
{
disn1[p]=disn1[x]+val[i];
q.push(make_pair(disn1[p],p));
}
}
}
}
int tmp = 0;
int num[maxm];
int ymh[maxm];
int in[maxn];
queue<int> que;
int a[2100][2100];
int main()
{
memset(a,-1,sizeof(a));
n=read(),m=read();
s1=read(),t1=read();
s2=read(),t2=read();
for (int i=1;i<=m;i++){
int u,v,ww;
u=read(),v=read(),ww=read();
addedge(u,v,ww);
addedge(v,u,ww);
x[++tmp]=u;
y[tmp]=v;
w[tmp]=ww;
x[++tmp]=v;
y[tmp]=u;
w[tmp]=ww;
}
dijkstra(s1);
dijkstra1(t1);
dijkstra2(s2);
dijkstra3(t2);
memset(point,0,sizeof(point));
cnt=0;
for (int i=1;i<=tmp;i++)
{
if (dis[x[i]]+w[i]+disn[y[i]]==dis[t1]) addedge(x[i],y[i],w[i]),num[i]=1,in[y[i]]++,a[x[i]][y[i]]++,a[y[i]][x[i]]++;
}
for (int i=1;i<=tmp;i++)
{
if (dis1[x[i]]+w[i]+disn1[y[i]]==dis1[t2]) {a[x[i]][y[i]]++,a[y[i]][x[i]]++;}
}
for (int i=1;i<=n;i++) if (!in[i]) que.push(i);
while (!que.empty())
{
int x = que.front();
que.pop();
for (int i=point[x];i;i=nxt[i])
{
int p = to[i];
//cout<<x<<" "<<p<<" "<<a[x][p]<<endl;
dp[p]=max(dp[p],dp[x]+a[x][p]*val[i]);
in[p]--;
if (!in[p]) que.push(p);
}
}
int ans=-1e9;
for (int i=1;i<=n;i++) ans=max(ans,dp[i]);
cout<<ans;
return 0;
}