QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#744197#602. 最小费用最大流(随机数据)dreamjoker100 ✓5ms7952kbC++237.9kb2024-11-13 21:07:532024-11-13 21:08:01

Judging History

你现在查看的是最新测评结果

  • [2024-11-13 21:08:01]
  • 评测
  • 测评结果:100
  • 用时:5ms
  • 内存:7952kb
  • [2024-11-13 21:07:53]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

template<typename T>
struct Dinic {
    int n;
    struct edge {
        int to,nxt;
        T cap;
        T cost;
    };

    std::vector<edge> e;
    std::vector<int> head,now;
    std::vector<T> h,dep;
    std::vector<bool> vis;
    T inf=std::numeric_limits<T>::max()>>1;

    Dinic(int _n) : n(_n),head(n+1,-1) {}

    void add_edge(int u,int v,T cap,T cost=0) {
        e.push_back({v,head[u],cap,cost});
        head[u]=e.size()-1;
        e.push_back({u,head[v],0,-cost});
        head[v]=e.size()-1;
    }

    // Johnson algorithm
    void spfa(int s) {
        fill(h.begin(),h.end(),inf);
        std::queue<int> q;

        h[s]=0;
        q.push(0);
        vis[s]=true;
        while(!q.empty()) {
            int u=q.front();
            q.pop();
            vis[u]=false;

            for(int i=head[u];~i;i=e[i].nxt) {
                int v=e[i].to;
                if(e[i].cap>0&&h[v]>h[u]+e[i].cost) {
                    h[v]=h[u]+e[i].cost;
                    if(!vis[v]) {
                        vis[v]=true;
                        q.push(v);
                    }
                }
            }
        }
    }

    bool dij(int s,int t) {
        fill(dep.begin(),dep.end(),inf);
        fill(vis.begin(),vis.end(),false);
        
        std::priority_queue<std::pair<T,int>,std::vector<std::pair<T,int>>,std::greater<std::pair<T,int>>> pq;

        dep[s]=0;
        pq.push({0,s});
        while(!pq.empty()) {
            int u=pq.top().second;
            pq.pop();
            if(vis[u]) continue;
            vis[u]=true;

            for(int i=head[u];~i;i=e[i].nxt) if(e[i].cap>0){
                int v=e[i].to;
                auto newdep=dep[u]+e[i].cost+h[u]-h[v];
                if(dep[v]>newdep) {
                    dep[v]=newdep;
                    pq.push({dep[v],v});
                }
            }
        }

        return dep[t]!=inf;
    }

    T dfs(int u,int t,T flow) {

        if(u==t||!flow) return flow;
        T rest=flow;
        vis[u]=true;
        for(int i=now[u];~i&&rest;i=e[i].nxt) {
            now[u]=i;
            int v=e[i].to;
            if(!vis[v]&&dep[v]==dep[u]+e[i].cost+h[u]-h[v]&&e[i].cap>0) {
                T delta=dfs(v,t,std::min(rest,e[i].cap));
                if(!delta) continue;
                e[i].cap-=delta;
                e[i^1].cap+=delta;
                rest-=delta;
            }
        }
        vis[u]=false;
        return flow-rest;
    }
  
    // flag 表示初始图中是否可能存在负权边,如果存在的话需要先Johnson算法求出h函数
    auto MCMF(int s,int t,int flag=0) {
        h.assign(n+1,0); vis.assign(n+1,false); dep.resize(n+1);
        T flow=0,cost=0;
        if(flag) spfa(s);
        while(dij(s,t)) {
            std::fill(vis.begin(),vis.end(),false);
            now=head;
            T delta=0,tmp;

            while(tmp=dfs(s,t,inf)) 
                delta+=tmp;

            for(int i=0;i<=n;i++) 
                h[i]+=dep[i];
      
            flow+=delta,cost+=h[t]*delta;
        }
        return std::make_pair(flow,cost);
    }
  
    void resetflow(T x=0) {
        for(auto &it:e) 
            it.flow=x;
    }
};

namespace IO
{
    char iobuf[1<<25],*p1=iobuf,*p2=iobuf;
    #define getchar() (p1==p2&&(p2=(p1=iobuf)+fread(iobuf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
    void read(){} template <typename T,typename... other>
    inline void read(T &f,other &...y)
    {
        f=0;T fu=1;char c=getchar();
        while(c<'0'||c>'9') {if(c=='-'){fu=-1;}c=getchar();}
        while(c>='0'&&c<='9') {f=(f<<3)+(f<<1)+(c&15);c=getchar();}
        f*=fu;
        read(y...);
    }
    template <typename T> 
    void print(T x,char c=0)
    {   
        if(x<0) putchar('-'),x=-x;
        if(x<10) putchar(x+48);
        else print(x/10),putchar(x%10+48);
        if(c) putchar(c);
    }
    inline void reads(std::string &f)
    {
        std::string str="";char ch=getchar();
        while(ch<'!'||ch>'~') ch=getchar();
        while((ch>='!')&&(ch<= '~')) str+=ch,ch=getchar();
        f=str;
    }
    void prints(std::string s)
    {
        for(int i=0;s[i];++i) 
        putchar(s[i]);
    } 

#ifndef endl 
#define endl '\n'
#endif
    struct Fastiostream {
        friend Fastiostream &operator>>(Fastiostream &in,std::string &s) {reads(s); return in;}
        friend Fastiostream &operator>>(Fastiostream &in,auto &x) {read(x); return in;}
        friend Fastiostream &operator<<(Fastiostream &out,const char c) {putchar(c); return out;}
        friend Fastiostream &operator<<(Fastiostream &out,const char *c) {while(*c!=0) putchar(*(c++)); return out;}
        friend Fastiostream &operator<<(Fastiostream &out,const std::string &s) {prints(s); return out;}
        friend Fastiostream &operator<<(Fastiostream &out,const auto &x) {print(x); return out;}
    } fio;
}

namespace MCMF {   
    using i64 = long long;
	using cap = i64;

	const int N = 1205;
	const int M = 120005;

	const i64 flow_INF = 1145141919810114514ll;
	const i64 cost_offset = 1145141919;

	int n, m, s, t;

	struct edge {
		int from, to, nxt;
		cap flow, cost;
		bool origin;
	} e[M << 1];
	int cnt;
	int h[N];

	int add_edge(int u, int v, cap flow, cap cost, bool directed = true) {
		++m;
        e[++cnt] = {u, v, h[u], flow, cost, 1};
        h[u] = cnt;
        e[++cnt] = {v, u, h[v], directed ? 0 : flow, -cost, 0};
        h[v] = cnt;
		return cnt;
	}
	int tme;
	int vis[N], fa[N], fe[N], circle[N], mark[N];
	cap pi[N];

	void dfs(int u, int fi) {
		fa[u] = e[fi].from, fe[u] = fi;
		mark[u] = 1;
		for (int i = h[u]; i; i = e[i].nxt) {
			int v = e[i].to;
			if (e[i].origin && !mark[v])
				dfs(v, i);
		}
	}

	cap phi(int u) {
		if (mark[u] == tme)
			return pi[u];
		mark[u] = tme, pi[u] = phi(fa[u]) + e[fe[u]].cost;
		return pi[u];
	}

	cap pushflow(int eg) {
		int rt = e[eg].from, lca = e[eg].to;
		++tme;
		int circle_cnt = 0;
		while (rt)
			mark[rt] = tme, rt = fa[rt];
		while (mark[lca] ^ tme)
			mark[lca] = tme, lca = fa[lca];
		cap minflow = e[eg].flow, p = 2, del_u = 0;
		for (int u = e[eg].from; u ^ lca; u = fa[u]) {
			circle[++circle_cnt] = fe[u];
			if (e[fe[u]].flow < minflow)
				minflow = e[fe[u]].flow, del_u = u, p = 0;
		}
		for (int u = e[eg].to; u ^ lca; u = fa[u]) {
			int ne = fe[u] ^ 1;
			circle[++circle_cnt] = ne;
			if (e[ne].flow < minflow)
				minflow = e[ne].flow, del_u = u, p = 1;
		}
		circle[++circle_cnt] = eg;

		cap cost = 0;
		for (int i = 1; i <= circle_cnt; ++i) {
			cost += e[circle[i]].cost * minflow;
			e[circle[i]].flow -= minflow, e[circle[i] ^ 1].flow += minflow;
		}
		if (p == 2) return cost;

		int u = e[eg].from, v = e[eg].to;
		if (p == 1) std::swap(u, v);

		int last_e = eg ^ p, last_u = v;
		while (last_u ^ del_u) {
			last_e ^= 1, --mark[u], std::swap(fe[u], last_e);
			int nu = fa[u];
			fa[u] = last_u, last_u = u, u = nu;
		}
		return cost;
	}
	void init_sz(int _n) { n = _n, m = 0, cnt = 1, tme = 1; }
	std::pair<cap, cap> solve(int _s, int _t) {
		s = _s, t = _t;
		add_edge(t, s, flow_INF, -cost_offset);
		dfs(t, 0), mark[t] = ++tme;
		fa[t] = 0;
		cap cost = 0, flow = 0;
		bool run = 1;
		while (run) {
			run = 0;
			for (int i = 2; i <= cnt; ++i)
				if (e[i].flow && e[i].cost + phi(e[i].from) - phi(e[i].to) < 0)
					cost += pushflow(i), run = 1;
		}
		flow = e[cnt].flow;
		return std::make_pair(flow, cost + flow * cost_offset);
	}
}

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    int n,m; 
    IO::fio>>n>>m;

    MCMF::init_sz(n);
    for(int i=0;i<m;i++) {
        int u,v,c,w; 
        IO::fio>>u>>v>>c>>w;
        MCMF::add_edge(u,v,c,w);
    }

    auto [flow,cost]=MCMF::solve(1,n);
    cout<<flow<<" "<<cost<<endl;
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Pretests


Final Tests

Test #1:

score: 10
Accepted
time: 0ms
memory: 5736kb

input:

8 27
2 3 2147483647 100
1 3 1 100
2 4 2147483647 10
1 4 1 10
2 4 2147483647 10
1 4 1 10
2 8 3 0
3 5 2147483647 100
1 5 1 100
3 8 1 0
3 2 2147483647 0
4 5 2147483647 10
1 5 1 10
4 8 1 0
4 2 2147483647 0
5 6 2147483647 1
1 6 1 1
5 6 2147483647 1
1 6 1 1
5 7 2147483647 1
1 7 1 1
5 8 3 0
5 2 2147483647 ...

output:

8 243

result:

ok 2 number(s): "8 243"

Test #2:

score: 10
Accepted
time: 1ms
memory: 5736kb

input:

12 49
2 10 2147483647 5
1 10 1 5
2 5 2147483647 50
1 5 1 50
2 9 2147483647 8
1 9 1 8
2 8 2147483647 47
1 8 1 47
2 11 2147483647 17
1 11 1 17
2 12 5 0
3 12 0 0
3 2 2147483647 0
4 6 2147483647 18
1 6 1 18
4 11 2147483647 12
1 11 1 12
4 9 2147483647 14
1 9 1 14
4 12 3 0
4 2 2147483647 0
5 11 2147483647...

output:

15 436

result:

ok 2 number(s): "15 436"

Test #3:

score: 10
Accepted
time: 1ms
memory: 5752kb

input:

27 169
2 15 2147483647 24
1 15 1 24
2 19 2147483647 96
1 19 1 96
2 12 2147483647 49
1 12 1 49
2 13 2147483647 75
1 13 1 75
2 24 2147483647 2
1 24 1 2
2 27 5 0
3 27 0 0
3 2 2147483647 0
4 11 2147483647 99
1 11 1 99
4 3 2147483647 85
1 3 1 85
4 27 2 0
4 2 2147483647 0
5 27 0 0
5 2 2147483647 0
6 9 214...

output:

60 4338

result:

ok 2 number(s): "60 4338"

Test #4:

score: 10
Accepted
time: 1ms
memory: 5948kb

input:

77 2149
2 42 2147483647 33
1 42 1 33
2 68 2147483647 30
1 68 1 30
2 76 2147483647 13
1 76 1 13
2 51 2147483647 93
1 51 1 93
2 12 2147483647 39
1 12 1 39
2 57 2147483647 74
1 57 1 74
2 70 2147483647 21
1 70 1 21
2 73 2147483647 24
1 73 1 24
2 52 2147483647 54
1 52 1 54
2 15 2147483647 99
1 15 1 99
2 ...

output:

1000 74606

result:

ok 2 number(s): "1000 74606"

Test #5:

score: 10
Accepted
time: 0ms
memory: 5980kb

input:

102 4199
2 48 2147483647 42
1 48 1 42
2 85 2147483647 50
1 85 1 50
2 22 2147483647 83
1 22 1 83
2 95 2147483647 97
1 95 1 97
2 82 2147483647 34
1 82 1 34
2 25 2147483647 72
1 25 1 72
2 4 2147483647 17
1 4 1 17
2 47 2147483647 10
1 47 1 10
2 71 2147483647 12
1 71 1 12
2 68 2147483647 39
1 68 1 39
2 2...

output:

2000 161420

result:

ok 2 number(s): "2000 161420"

Test #6:

score: 10
Accepted
time: 0ms
memory: 7952kb

input:

102 4199
2 79 2147483647 13
1 79 1 13
2 83 2147483647 73
1 83 1 73
2 75 2147483647 90
1 75 1 90
2 30 2147483647 92
1 30 1 92
2 54 2147483647 25
1 54 1 25
2 66 2147483647 53
1 66 1 53
2 52 2147483647 37
1 52 1 37
2 63 2147483647 46
1 63 1 46
2 11 2147483647 20
1 11 1 20
2 55 2147483647 53
1 55 1 53
2...

output:

2000 143072

result:

ok 2 number(s): "2000 143072"

Test #7:

score: 10
Accepted
time: 2ms
memory: 5992kb

input:

102 4199
2 39 2147483647 45
1 39 1 45
2 51 2147483647 11
1 51 1 11
2 86 2147483647 63
1 86 1 63
2 23 2147483647 46
1 23 1 46
2 48 2147483647 63
1 48 1 63
2 87 2147483647 8
1 87 1 8
2 73 2147483647 63
1 73 1 63
2 5 2147483647 52
1 5 1 52
2 80 2147483647 21
1 80 1 21
2 31 2147483647 44
1 31 1 44
2 101...

output:

2000 146132

result:

ok 2 number(s): "2000 146132"

Test #8:

score: 10
Accepted
time: 5ms
memory: 6492kb

input:

302 10599
2 72 2147483647 169
1 72 1 169
2 260 2147483647 165
1 260 1 165
2 12 2147483647 108
1 12 1 108
2 16 2147483647 26
1 16 1 26
2 28 2147483647 148
1 28 1 148
2 7 2147483647 74
1 7 1 74
2 139 2147483647 199
1 139 1 199
2 231 2147483647 9
1 231 1 9
2 287 2147483647 123
1 287 1 123
2 135 2147483...

output:

5000 1106316

result:

ok 2 number(s): "5000 1106316"

Test #9:

score: 10
Accepted
time: 4ms
memory: 6576kb

input:

302 10599
2 222 2147483647 132
1 222 1 132
2 17 2147483647 7
1 17 1 7
2 177 2147483647 253
1 177 1 253
2 90 2147483647 195
1 90 1 195
2 128 2147483647 289
1 128 1 289
2 42 2147483647 193
1 42 1 193
2 213 2147483647 133
1 213 1 133
2 263 2147483647 293
1 263 1 293
2 50 2147483647 155
1 50 1 155
2 228...

output:

5000 1290871

result:

ok 2 number(s): "5000 1290871"

Test #10:

score: 10
Accepted
time: 4ms
memory: 7916kb

input:

302 10599
2 176 2147483647 289
1 176 1 289
2 190 2147483647 99
1 190 1 99
2 10 2147483647 96
1 10 1 96
2 240 2147483647 165
1 240 1 165
2 273 2147483647 205
1 273 1 205
2 248 2147483647 194
1 248 1 194
2 220 2147483647 122
1 220 1 122
2 194 2147483647 167
1 194 1 167
2 8 2147483647 67
1 8 1 67
2 227...

output:

5000 1395897

result:

ok 2 number(s): "5000 1395897"