• Codeforces 863F Almost Permutation


      OvO http://codeforces.com/problemset/problem/863/F

      (Educational Codeforces Round 29 - F)

      费用流

      对每个ax,从源点连一条费用为0,流量为1的边,

      对于每个ax,对能取到的每个值,连一条费用为0,流量为1的边,

      对于每个能取到的值(n个),向汇点连n条流量为1的边,满足:第一条到第i条边的费用和为i^2(即费用分别为1,3,5,7,9……)

    代码

    #include <stdio.h>
    #include <algorithm>
    #include <string.h>
    #include <iostream>
    #include <string>
    #include <queue>
    
    using namespace std;
    
    const int M=55;
    const int MAXN = 10000;
    const int MAXM = 100000;
    const int INF = 0x3f3f3f3f;
    
    struct Edge
    {
        int to,next,cap,flow,cost;
    }edge[MAXM];
    
    int head[MAXN],tol;
    int pre[MAXN],dis[MAXN];
    bool vis[MAXN];
    int N;//节点总个数,节点编号从0~N-1
    
    void init(int n)
    {
        N = n;
        tol = 0;
        memset(head,-1,sizeof(head));
    }
    
    void addedge(int u,int v,int cap,int cost)
    {
        edge[tol].to = v;
        edge[tol].cap = cap;
        edge[tol].cost = cost;
        edge[tol].flow = 0;
        edge[tol].next = head[u];
        head[u] = tol++;
        edge[tol].to = u;
        edge[tol].cap = 0;
        edge[tol].cost = -cost;
        edge[tol].flow = 0;
        edge[tol].next = head[v];
        head[v] = tol++;
    }
    
    bool spfa(int s,int t)
    {
        queue<int>q;
        for(int i = 0;i < N;i++)
        {
            dis[i] = INF;
            vis[i] = false;
            pre[i] = -1;
        }
        dis[s] = 0;
        vis[s] = true;
        q.push(s);
        while(!q.empty())
        {
            int u = q.front();
            q.pop();
            vis[u] = false;
            for(int i = head[u]; i != -1;i = edge[i].next)
            {
                int v = edge[i].to;
                if(edge[i].cap > edge[i].flow &&
                   dis[v] > dis[u] + edge[i].cost )
                {
                    dis[v] = dis[u] + edge[i].cost;
                    pre[v] = i;
                    if(!vis[v])
                    {
                        vis[v] = true;
                        q.push(v);
                    }
                }
            }
        }
        if(pre[t] == -1)return false;
        else return true;
    }
    
    //返回的是最大流,cost存的是最小费用
    int minCostMaxflow(int s,int t,int &cost)
    {
        int flow = 0;
        cost = 0;
        while(spfa(s,t))
        {
            int Min = INF;
            for(int i = pre[t];i != -1;i = pre[edge[i^1].to])
            {
                if(Min > edge[i].cap - edge[i].flow)
                    Min = edge[i].cap - edge[i].flow;
            }
            for(int i = pre[t];i != -1;i = pre[edge[i^1].to])
            {
                edge[i].flow += Min;
                edge[i^1].flow -= Min;
                cost += edge[i].cost * Min;
            }
            flow += Min;
        }
        return flow;
    }
    
    int n,q;
    int lmt[M][3];
    
    void solve()
    {
    	int i,j,ans,lst,now,tmp;
    	int s=0,t=2*n+1;
    	init(2*n+2);
    	for(i=1;i<=n;i++)
    		if(lmt[i][0]>lmt[i][1])
    		{
    			printf("-1
    ");
    			return ;
    		}
    	for(i=1;i<=n;i++)
    		addedge(s,i,1,0);
    	for(i=1;i<=n;i++)
    		for(j=lmt[i][0];j<=lmt[i][1];j++)
    			addedge(i,n+j,1,0);
    	for(i=1;i<=n;i++)
    	{
    		lst=0;
    		for(j=1;j<=n;j++)
    		{
    			addedge(n+i,t,1,j*j-lst);
    			lst=j*j;
    		}
    	}
    	minCostMaxflow(s,t,ans);
    	printf("%d
    ",ans);
    }
    
    int main()
    {
    	int i,j,t,li,ri,v;
    	scanf("%d%d",&n,&q);
    	for(i=1;i<=n;i++)
    		lmt[i][0]=1,lmt[i][1]=n;
    	for(i=1;i<=q;i++)
    	{
    		scanf("%d%d%d%d",&t,&li,&ri,&v);
    		if(t==1)
    			for(j=li;j<=ri;j++)
    				lmt[j][0]=max(lmt[j][0],v);
    		else
    			for(j=li;j<=ri;j++)
    				lmt[j][1]=min(lmt[j][1],v);
    	}
    	solve();
    	return 0;
    }
    
    
    	
    

      

  • 相关阅读:
    [noip2013]华容道
    [tyvj 1061] Mobile Service (线性dp 滚动数组)
    [bzoj 2726] 任务安排 (斜率优化 线性dp)
    [洛谷 P2365] 任务安排 (线性dp)
    [poj 3666] Making the Grade (离散化 线性dp)
    【模板】manacher算法
    [tyvj 1071] LCIS
    [NOIP 2012] 国王游戏
    [NOIP 2010] 关押罪犯 (二分+二分图判定 || 并查集)
    [NOI 2002] 银河英雄传说 (带权并查集)
  • 原文地址:https://www.cnblogs.com/FxxL/p/7596305.html
Copyright © 2020-2023  润新知