感觉空间没炸啊,也没看到哪里越界了。
和普通的 dijkstra 方法一样,边全加上 hu−hv,每跑一遍,hi←hi+disi。
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define REP(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back
#define mkpr make_pair
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
template<class T>
void ckmx(T& a,T b){
a=max(a,b);
}
template<class T>
void ckmn(T& a,T b){
a=min(a,b);
}
template<class T>
T gcd(T a,T b){
return !b?a:gcd(b,a%b);
}
template<class T>
T lcm(T a,T b){
return a/gcd(a,b)*b;
}
#define gc getchar()
#define eb emplace_back
#define pc putchar
#define ep empty()
#define fi first
#define se second
#define pln pc('\n');
template<class T>
void wrint(T x){
if(x<0){
x=-x;
pc('-');
}
if(x>=10){
wrint(x/10);
}
pc(x%10^48);
}
template<class T>
void wrintln(T x){
wrint(x);
pln
}
template<class T>
void read(T& x){
x=0;
int f=1;
char ch=gc;
while(!isdigit(ch)){
if(ch=='-')f=-1;
ch=gc;
}
while(isdigit(ch)){
x=(x<<1)+(x<<3)+(ch^48);
ch=gc;
}
x*=f;
}
const int maxn=5e3+5,maxm=5e4+5;
int n,m,s,t;
struct EDGE{
int to,nxt;
ll w,c;
}edge[maxm<<1];
int cnt=1;
int head[maxn];
void add(int u,int to,ll w,ll c){
edge[++cnt].to=to;
edge[cnt].w=w;
edge[cnt].c=c;
edge[cnt].nxt=head[u];
head[u]=cnt;
}
ll dis[maxn];
bitset<maxn> vis;
ll h[maxn];
bool shortdis(){
vis.reset();
memset(dis,0x7f,sizeof dis);
dis[s]=0;
priority_queue<pair<ll,int>,vector<pair<ll,int> > ,greater<pair<ll,int> > > q;
q.push(mkpr(0,s));
while(!q.empty()){
int u=q.top().se;
q.pop();
if(vis[u])continue;
vis[u]=1;
for(int i=head[u];i;i=edge[i].nxt){
if(edge[i].w<=0)continue;
int to=edge[i].to;
if(dis[to]>dis[u]+edge[i].c+h[u]-h[to]){
dis[to]=dis[u]+edge[i].c+h[u]-h[to];
if(!vis[to])q.push(mkpr(dis[to],to));
}
}
}
return (dis[t]<1e18);
}
int cur[maxn];
ll dfs(int u,ll lim){
if(u==t){
return lim;
}
ll totuse=0;
for(int&i=cur[u];i;i=edge[i].nxt){
int to=edge[i].to;
if(dis[to]!=dis[u]+edge[i].c+h[u]-h[to]||!edge[i].w)continue;
ll nwuse=dfs(to,min(lim-totuse,edge[i].w));
edge[i].w-=nwuse;
edge[i^1].w+=nwuse;
totuse+=nwuse;
if(totuse==lim)break;
}
return totuse;
}
void Dinic(){
ll mxflow=0,mxcost=0;
while(shortdis()){
// puts("???");
memcpy(cur,head,sizeof head);
ll _=dfs(s,1e18);
mxflow+=_;
mxcost+=_*(dis[t]-(h[s]-h[t]));
FOR(i,1,n){
h[i]+=dis[i];
}
}
printf("%lld %lld\n",mxflow,mxcost);
}
void solve(int id_of_test){
read(n);
read(m);
read(s);
read(t);
FOR(i,1,m){
int u,v,w,c;
read(u);
read(v);
read(w);
read(c);
add(u,v,w,c);
add(v,u,0,-c);
}
Dinic();
}
int main()
{
int T;
T=1;
FOR(_,1,T){
solve(_);
}
return 0;
}