zoukankan      html  css  js  c++  java
  • ZOJ 3933 Team Formation

    费用流裸题......比赛的时候少写了一句话....导致增加了很多无用的边一直在TLE

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<vector>
    #include<queue>
    #include<algorithm>
    using namespace std;
    
    const int maxn=500+10;
    int n;
    int belong[maxn];
    int cost[maxn];
    int g[maxn][maxn];
    char str[maxn];
    
    const int INF=0x7FFFFFFF;
    struct Edge
    {
        int from,to,cap,flow,cost;
    };
    int N,M,K,len,s,t;
    vector<Edge> edges;
    vector<int> G[maxn];
    int inq[maxn];
    int d[maxn];
    int p[maxn];
    int a[maxn];
    int use[maxn];
    int dis[maxn][maxn];
    
    void init()
    {
        for(int i=0; i<maxn; i++) G[i].clear();
        edges.clear();
    }
    
    void Addedge(int from,int to,int cap,int cost)
    {
        edges.push_back((Edge)
        {
            from,to,cap,0,cost
        });
        edges.push_back((Edge)
        {
            to,from,0,0,-cost
        });
        len=edges.size();
        G[from].push_back(len-2);
        G[to].push_back(len-1);
    }
    
    bool BellmanFord(int s,int t,int &flow,int &cost)
    {
    
        for(int i=0; i<maxn; i++) d[i]=INF;
    
        memset(inq,0,sizeof(inq));
        memset(p,-1,sizeof(p));
    
        d[s]=0;
        inq[s]=1;
        p[s]=0;
        a[s]=INF;
    
        queue<int>Q;
        Q.push(s);
        while(!Q.empty())
        {
            int u=Q.front();
            Q.pop();
            inq[u]=0;
            for(int i=0; i<G[u].size(); i++)
            {
                Edge& e=edges[G[u][i]];
                if(e.cap>e.flow&&d[e.to]>d[u]+e.cost)
                {
                    d[e.to]=d[u]+e.cost;
                    p[e.to]=G[u][i];
                    a[e.to]=min(a[u],e.cap-e.flow);
                    if(!inq[e.to])
                    {
                        Q.push(e.to);
                        inq[e.to]=1;
                    }
                }
            }
        }
        if(d[t]==INF) return false;
        flow+=a[t];
        cost+=d[t]*a[t];
        int u=t;
        while(u!=s)
        {
            edges[p[u]].flow+=a[t];
            edges[p[u]^1].flow-=a[t];
            u=edges[p[u]].from;
        }
        return true;
    }
    
    void Mincost (int s,int t)
    {
        int flow=0,cost=0;
        while(BellmanFord(s,t,flow,cost));
        printf("%d ",flow);
        printf("%d
    ",2*flow-cost);
    }
    
    void read()
    {
        scanf("%s",str);
        for(int i=0;str[i];i++) belong[i+1]=str[i]-'0';
        scanf("%s",str);
        for(int i=0;str[i];i++) cost[i+1]=str[i]-'0';
    
        memset(g,0,sizeof g);
    
        for(int i=1;i<=n;i++)
        {
            int num; scanf("%d",&num);
            while(num--)
            {
                int id; scanf("%d",&id);
                g[i][id]=g[id][i]=1;
            }
        }
    }
    
    int main()
    {
        int T; scanf("%d",&T);
        while(T--)
        {
            scanf("%d",&n); s=0,t=n+1;
            init();
            read();
            for(int i=1;i<=n;i++)
            {
                if(belong[i]==0) Addedge(0,i,1,0);
                else Addedge(i,n+1,1,0);
            }
    
            for(int i=1;i<=n;i++)
            {
                if(belong[i]==1) continue;
    
                for(int j=1;j<=n;j++)
                {
                    if(i==j) continue;
                    if(g[i][j]==1) continue;
                    if(belong[j]==0) continue;
                    Addedge(i,j,1,cost[i]+cost[j]);
                }
            }
            Mincost(s,t);
            for(int i=0;i<edges.size();i=i+2)
            {
                if(edges[i].from==0) continue;
                if(edges[i].to==n+1) continue;
                if(edges[i].flow==0) continue;
                printf("%d %d
    ",edges[i].from,edges[i].to);
            }
        }
        return 0;
    }
  • 相关阅读:
    FR #3题解
    L3-005. 垃圾箱分布
    L2-004. 这是二叉搜索树吗?
    L2-002. 链表去重
    L1-009. N个数求和
    L3-003. 社交集群
    L3-004. 肿瘤诊断
    L2-001. 紧急救援
    L3-002. 堆栈
    L2-007. 家庭房产
  • 原文地址:https://www.cnblogs.com/zufezzt/p/5375373.html
Copyright © 2011-2022 走看看