• P4043 [AHOI2014/JSOI2014]支线剧情 上下界费用流


      题意: 有个人每次可以从1出发(可以无限次)  走有向边  耗费的时间为有向边的长度   问最少耗费的时间遍历所有的边至少一次 

    有点像滑雪那题  不过那题求得是最少的次数  

    这题很显然可以转化为上下界费用流  只要设置边的容量为1-inf 即可

    注意:

    上下界费用流的答案为:

    答案即为(求出的费用+原图中边的下界*边的费用)

    答案即为(求出的费用+原图中边的下界*边的费用)

    答案即为(求出的费用+原图中边的下界*边的费用)

    答案即为(求出的费用+原图中边的下界*边的费用)

    #include<bits/stdc++.h>
    using namespace std;
    //input by bxd
    #define rep(i,a,b) for(int i=(a);i<=(b);i++)
    #define repp(i,a,b) for(int i=(a);i>=(b);--i)
    #define RI(n) scanf("%d",&(n))
    #define RII(n,m) scanf("%d%d",&n,&m)
    #define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
    #define RS(s) scanf("%s",s);
    #define ll long long
    #define pb push_back
    #define inf 0x3f3f3f3f
    #define CLR(A,v)  memset(A,v,sizeof A)
    //////////////////////////////////
    const int N=100001;
    
    ll maxflow,mincost;
    int last[N],pre[N],dis[N],flow[N],d[N];
    bool vis[N];
    struct Edge{
        int next,to,flow,dis;
    }edge[N<<1];
    int pos=1,head[N];
    void init()
    {
        pos=1;
        CLR(head,0);
        mincost=maxflow=0;
    }
    queue <int> q;
    inline void add(int from,int to,int flow,int dis)//flow流量 dis费用
    {
        edge[++pos].next=head[from];
        edge[pos].flow=flow;
        edge[pos].dis=dis;
        edge[pos].to=to;
        head[from]=pos;
    
        edge[++pos].next=head[to];
        edge[pos].flow=0;
        edge[pos].dis=-dis;
        edge[pos].to=from;
        head[to]=pos;
    
    }
    void ins(int x,int y,int down,int up,int cost)
    {
        add(x,y,up-down,cost);
        d[x]-=down;
        d[y]+=down;
    }
    
    bool spfa(int s,int t)
    {
        CLR(dis,0x3f);
        CLR(flow,0x3f);
        CLR(vis,0);
        while (!q.empty()) q.pop();
        dis[s]=0; pre[t]=-1; q.push(s); vis[s]=1;
        int tot=0;
        while (!q.empty())
        {
            int now=q.front(); q.pop(); vis[now]=0;
            for (int i=head[now]; i; i=edge[i].next)
            {
                int to=edge[i].to;
                if  (edge[i].flow>0 && dis[to]>dis[now]+edge[i].dis)
                {
                    dis[to]=edge[i].dis+dis[now];
                    flow[to]=min(edge[i].flow,flow[now]);
                    last[to]=i;
                    pre[to]=now;
                    if (!vis[to])
                    {
                        q.push(to); vis[to]=1;
                    }
                }
            }
        }
        return pre[t]!=-1;
    }
    inline void MCMF(int s,int t)
    {
        while (spfa(s,t))
        {
            int now=t;
            maxflow+=flow[t];
            mincost+=flow[t]*dis[t];
            while (now!=s)
            {
                edge[last[now]].flow-=flow[t];//dis . flow
                edge[last[now]^1].flow+=flow[t];
                now=pre[now];
            }
        }
    }
    int n,m,s,t,ss,x,a,b,S,T;
    
    int main()
    {
        scanf("%d",&n);
        s=n+1;t=s+1;S=t+1;T=S+1;
    
        ins(s,1,0,inf,0);
        int sum=0;
        rep(i,1,n)
        {
            ins(i,t,0,inf,0);
            int k;scanf("%d",&k);
            while(k--)
            {
                scanf("%d%d",&a,&b);sum+=b;
                ins(i,a,1,inf,b);
            }
        }
        ins(t,s,0,inf,0);
    
        rep(i,1,t)
        if(d[i]>0)add(S,i,d[i],0);
        else if(d[i]<0)add(i,T,-d[i],0);
        MCMF(S,T);
    
        cout<<mincost+sum;
    
        return 0;
    }
    View Code
  • 相关阅读:
    简单理解桶排序
    实现js的类似alert效果的函数
    简单理解插入排序
    一个js简单的日历显示效果的函数
    详解一个自己原创的正则匹配IP的表达式
    一个简单的js实现倒计时函数
    简单理解冒泡排序
    简单理解js的this
    vue项目分享html页面(服务器只能内网访问)
    vue项目移动端查看、分享pdf(服务器只能内网访问)
  • 原文地址:https://www.cnblogs.com/bxd123/p/11309102.html
Copyright © 2020-2023  润新知