码迷,mamicode.com
首页 > 其他好文 > 详细

111

时间:2017-08-16 21:37:29      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:fine   names   while   bfs   turn   cap   std   sizeof   out   

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define N 200005
using namespace std;

struct edge
{
    int u,v,cap,next;
}e[200005];
int n,m,l,cnt,head[N],dep[N],gap[N],cur[N],st[N],que[N];
int pro[205],pay[205],ti[205];
vector<int> v[205];

void addedge(int u,int v,int cap)
{
    e[cnt].u = u;
    e[cnt].v = v;
    e[cnt].cap = cap;
    e[cnt].next = head[u];
    head[u] = cnt++;
    e[cnt].u = v;
    e[cnt].v = u;
    e[cnt].cap = 0;
    e[cnt].next = head[v];
    head[v] = cnt++;
}

void bfs(int des)
{
    memset(dep,-1,sizeof(dep));
    memset(gap,0,sizeof(gap));
    gap[0] = 1;
    int front = 0, rear = 0;
    dep[des] = 0;
    que[rear++] = des;
    int u,v;
    while(front != rear)
    {
        u = que[front++];
        front %= N;
        for(int i = head[u];i != -1;i = e[i].next)
        {
            v = e[i].v;
            if(e[i].cap != 0 || dep[v] != -1)   continue;
            que[rear++] = v;
            rear %= N;
            ++gap[dep[v] = dep[u]+1];
        }
    }
}

int isap(int src, int des, int n)//源点、汇点、图中点的总数
{
    bfs(des);
    int ans = 0,top = 0;
    memcpy(cur, head, sizeof(head));
    int u = src,i;
    while(dep[src] < n)
    {
        if(u == des)
        {
            int temp = INF,inser = n;
            for(i = 0;i != top;i++)
            {
                if (temp > e[st[i]].cap)
                {
                    temp = e[st[i]].cap;
                    inser = i;
                }
            }
            for(i = 0;i != top;i++)
            {
                e[st[i]].cap -= temp;
                e[st[i]^1].cap += temp;
            }
            ans += temp;
            top = inser;
            u = e[st[top]].u;
        }
        if(u != des && !gap[dep[u]-1])  break;
        for(i = cur[u];i != -1;i = e[i].next)
        {
            if(e[i].cap != 0 && dep[u] == dep[e[i].v]+1)  break;
        }
        if(i != -1)
        {
            cur[u] = i;
            st[top++] = i;
            u = e[i].v;
        }
        else
        {
            int minn = n;
            for(i = head[u];i != -1;i = e[i].next)
            {
                if(!e[i].cap)    continue;
                if(minn > dep[e[i].v])
                {
                    minn = dep[e[i].v];
                    cur[u] = i;
                }
            }
            --gap[dep[u]];
            ++gap[dep[u] = minn+1];
            if(u != src)    u = e[st[--top]].u;
        }
    }
    return ans;
}


bool ok(int t)
{
    int sum = 0;
    cnt = 0;
    for(int i = 1;i <= m;i++)
    {
        int flag = 0;
        for(int j = 0;j < v[i].size();j++)
        {
            int tt = v[i][j];
            if(t < ti[tt])
            {
                flag = 1;
                break;
            }
            addedge(i,tt+m,INF);
        }
        if(!flag)
        {
            addedge(0,i,pro[i]);
            sum += pro[i];
        }
    }
    for(int i = 1;i <= n;i++)
    {
        if(t >= ti[i])    addedge(i+m,n+m+1,pay[i]);
    }
    if(sum-isap(0,n+m+1,n+m+2) >= l)    return 1;
    return 0;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&n,&m,&l);
        for(int i = 1;i <= m;i++)    v[i].clear();
        for(int i = 1;i <= n;i++)    scanf("%d%d",&pay[i],&ti[i]);
        for(int i = 1;i <= m;i++)
        {
            scanf("%d",&pro[i]);
            int t;
            while(t--)
            {
                int x;
                scanf("%d",&x);
                v[i].push_back(x);
            }
        }
        int l = 1,r = 1e9;
        while(l < r)
        {
            int mid = (l+r)/2;
            if(ok(mid))    r = mid;
            else    l = mid+1;
        }
        cout << l << endl;
    }
}

 

111

标签:fine   names   while   bfs   turn   cap   std   sizeof   out   

原文地址:http://www.cnblogs.com/zhurb/p/7375587.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!