题目链接:https://www.luogu.org/problemnew/show/P3381
思路:模板题,改都不带改的
#pragma GCC optimize(2) #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef unsigned long long ull; const ll inff = 0x3f3f3f3f3f3f3f3f; #define FOR(i,a,b) for(int i(a);i<=(b);++i) #define FOL(i,a,b) for(int i(a);i>=(b);--i) #define REW(a,b) memset(a,b,sizeof(a)) #define inf int(0x3f3f3f3f) #define si(a) scanf("%d",&a) #define sl(a) scanf("%lld",&a) #define sd(a) scanf("%lf",&a) #define ss(a) scanf("%s",a) #define mod ll(6666666) #define pb push_back #define eps 1e-6 #define lc d<<1 #define rc d<<1|1 #define Pll pair #define P pair #define pi acos(-1) const int N=1e4+7; struct edge{int to,cap,cost,rev;}; vectorg[N]; int h[N],d[N],pre[N],prv[N],n,m,res,s,t,flow; void add(int from,int to,int cap,int cost) { g[from].pb(edge{to,cap,cost,(int)g[to].size()}); g[to].pb(edge{from,0,-cost,(int)g[from].size()-1}); } void slove(int s,int t,int f,int n) { //int res=0; fill(h,h+n+1,0); while(f>0) { priority_queue,greater > q; fill(d,d+n+1,inf); d[s]=0; q.push(P(0,s)); while(!q.empty()) { P p=q.top();q.pop(); int v=p.second; if(d[v]0&&d[e.to]>d[v]+e.cost+h[v]-h[e.to]) { d[e.to]=d[v]+e.cost+h[v]-h[e.to]; prv[e.to]=v;pre[e.to]=i; q.push(P(d[e.to],e.to)); } } } if(d[t]==inf) break; FOR(i,0,n) h[i]+=d[i]; int d=f; for(int i=t;i!=s;i=prv[i]) d=min(d,g[prv[i]][pre[i]].cap); f-=d,flow+=d; res+=d*h[t]; for(int i=t;i!=s;i=prv[i]) { edge &e=g[prv[i]][pre[i]]; e.cap-=d; g[i][e.rev].cap+=d; } } //return res; } int main() { cin.tie(0); cout.tie(0); cin>>n>>m>>s>>t; int x,y,z,q; FOR(i,1,m) { si(x),si(y),si(z),si(q); add(x,y,z,q); } slove(s,t,inf,n); cout<
,greater
> q; fill(d,d+n+1,inf); d[s]=0; q.push(P(0,s)); while(!q.empty()) { P p=q.top();q.pop(); int v=p.second; if(d[v]