敲了半天dij,一交TLE 3 个点,再粘了两篇dij题解,最好的T了一个点,除了手写堆的dalao跑过了
帮忙看看吧,自认马蜂氢气(感谢qwq
#include<bits/stdc++.h>
#define pii pair<int,int>
#define INF 233333333
#define pos second
#define dit first
#define M 500005
using namespace std;
inline int read(){
int x=0;int 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 x*f;
}
int n,m,s,t;
struct Bian_{
int to,val,flow,nxt;
}e[M];
int cnt=0,head[M];
inline void Lian(int fr,int to,int val,int flow){
cnt++;
e[cnt].to=to;
e[cnt].val=val;
e[cnt].flow=flow;
e[cnt].nxt=head[fr];
head[fr]=cnt;
}
bool vis[M];
int h[M],id[M],dis[M],pre[M],flow[M];
priority_queue<pii,vector<pii>,greater<pii> >q;
inline bool dij(int s,int t){
for(int i=1;i<=n;++i){
dis[i]=INF;flow[i]=INF;
id[i]=pre[i]=vis[i]=0;
}
dis[s]=0;q.push(pii(0,s));
while(!q.empty()){
pii now=q.top();q.pop();
int x=now.pos,d=now.dit;
if(vis[x])continue;
vis[x]=true;dis[x]=d;
for(int i=head[x];~i;i=e[i].nxt){
int y=e[i].to,v=e[i].val,flw=e[i].flow;
if(!vis[y]&&flw>0&&dis[y]>dis[x]+v+h[x]-h[y]){
dis[y]=dis[x]+v+h[x]-h[y];
pre[y]=x;id[y]=i;
flow[y]=min(flow[x],flw);
q.push(pii(dis[y],y));
}
}
}
return dis[t]!=INF;
}
int Mx_flow=0,Mi_cost=0;
/*inline void MCMA(int s,int t){
while(dij(s,t)){
int _flow=flow[t];
Mi_cost+=_flow*(dis[t]-h[s]+h[t]);
Mx_flow+=_flow;
for(int i=t;i!=s;i=pre[i]){
e[id[i]].flow-=_flow;
e[id[i]^1].flow+=_flow;
}
for(int i=1;i<=n;++i)h[i]+=dis[i];
}
}*/
void MCMA(int begin,int end)
{
while(dij(begin,end))
{
int _flow=flow[end];
Mi_cost+=_flow*(dis[end]-h[begin]+h[end]);
Mx_flow+=_flow;
for(int i=end;i!=begin;i=pre[i])
{
e[id[i]].flow-=_flow;
e[id[i]^1].flow+=_flow;
}
for(int i=1;i<=n;i++)
h[i]+=dis[i];
}
}
int main(){
n=read();m=read();s=read();t=read();
for(int i=1;i<=n;++i) head[i]=-1;
for(int i=1;i<=m;++i){
int a=read(),b=read();
int mx=read(),c=read();
Lian(a,b,c,mx);Lian(b,a,-c,0);
}
MCMA(s,t);
printf("%d %d",Mx_flow,Mi_cost);
return 0;
}
有哪位dalao也用的是dij,且无手写堆的,给个代码,给我个重拾dij光辉形象的机会吧QWQ