牛客 - Minimum-cost Flow


传送门:牛客 - Minimum-cost

思路分析

先求出容量为1的时候的最小费用最大流,记录每一次增广路的权值,对于询问只需要看需要几条增广路,前缀和处理一下就行了

AC代码


#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))

namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
    bool IOerror=0;
    inline char nc() {
        static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
        if(p1==pend) {
            p1=buf;
            pend=buf+fread(buf,1,BUF_SIZE,stdin);
            if(pend==p1) {
                IOerror=1;
                return -1;
            }
        }
        return *p1++;
    }
    inline bool blank(char ch) {
        return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
    }
    template<class T> inline bool read(T &x) {
        bool sign=0;
        char ch=nc();
        x=0;
        for(; blank(ch); ch=nc());
        if(IOerror)return false;
        if(ch=='-')sign=1,ch=nc();
        for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
        if(sign)x=-x;
        return true;
    }
    template<class T,class... U>bool read(T& h,U&... t) {
        return read(h)&&read(t...);
    }
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int INF = 0x3f3f3f3f;
const int N = 55;
const int M = 333;
#define int long long
struct node {
    int v,flow,w,next;
} e[M];

int head[N],cnt;
int n,m,s,t,k,maxflow,mincost,path[N],pre[N];
int dis[N],vis[N],ss[333];
vector<int>ans;

inline void ade(int u,int v,int f,int w) {
    e[++cnt].v=v;
    e[cnt].flow=f;
    e[cnt].w=w;
    e[cnt].next=head[u];
    head[u]=cnt;
}

inline void add(int u,int v,int f,int w) {
    ade(u,v,f,w);
    ade(v,u,0,-w);
}

inline bool spfa() {
    mem(dis,INF);
    mem(vis,0);
    mem(pre,-1);
    dis[s]=0;
    vis[s]=1;
    queue<int>q;
    q.push(s);
    while(!q.empty()) {
        int u=q.front();
        vis[u]=0;
        q.pop();
        for(int i=head[u]; i; i=e[i].next) {
            int v=e[i].v;
            int w=e[i].w;
            if(e[i].flow && dis[v]>dis[u]+w) {
                dis[v]=dis[u]+w;
                pre[v]=u;
                path[v]=i;
                if(!vis[v]) {
                    q.push(v);
                    vis[v]=1;
                }
            }
        }
    }
    return pre[t]!=-1;
}

inline void EK() {
    while(spfa()) {
        ans.pb(dis[t]);
        int mi=INF;
        for(int i=t; i!=s; i=pre[i])
            mi=min(mi,e[path[i]].flow);
        for(int i=t; i!=s; i=pre[i]) {
            e[path[i]].flow-=mi;
            e[path[i]^1].flow+=mi;
        }
        maxflow+=mi;
        mincost+=dis[t]*mi;
    }
}


signed main() {
    
    while(read(n,m)){
        s=1,t=n;
        mem(head,0);
        cnt=1;
        ans.clear();
        for(int i=1;i<=m;i++){
            int u,v,w;
            read(u,v,w);
            add(u,v,1,w);
        }
        EK();
        int q;
        read(q);
        int len=sz(ans);
        for(int i=0;i<len;i++) ss[i+1]=ss[i]+ans[i];
        while(q--){
            int u,v;
            read(u,v);
            if(u*len<v) {
                puts("NaN");
                continue;
            }
            int k=v/u;
            int w=ss[k];
            int z=v-k*u;
            w=w*u+ans[k]*z;
            int g=__gcd(w,v);
            printf("%lld/%lld\n",w/g,v/g);
        }
        
        
    }

    return 0;
}

文章作者: 小凡
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 小凡 !
评论
  目录
隐藏
{% if theme.sakura.enable %}{% endif %}