HDU 5855 Less Time, More profit

二分t+最大权闭合图。

很显然二分那个t作为limit。每一个limit下,有一些边不能用了,然后要知道这种情况下怎么选点获得的价值最大。

这么想:一个shop想获得收益,就必须选择某一些plant,问题就转化成了最大权闭合图。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-8;
void File()
{
    freopen("D:\\in.txt","r",stdin);
    freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
    char c = getchar(); x = 0;while(!isdigit(c)) c = getchar();
    while(isdigit(c)) { x = x * 10 + c - ‘0‘; c = getchar();  }
}

int T,n,m;
int L;
struct X
{
    int pay;
    int t;
}p[300];
vector<int>g[300];
int pro[300];
bool flag[300],se[300];

const int maxn = 3000 + 10;
const int INF = 0x7FFFFFFF;
struct Edge
{
    int from, to, cap, flow;
    Edge(int u, int v, int c, int f) :from(u), to(v), cap(c), flow(f){}
};
vector<Edge>edges;
vector<int>G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
int s, t;

void init()
{
    for (int i = 0; i < maxn; i++)
        G[i].clear();
    edges.clear();
}
void AddEdge(int from, int to, int cap)
{
    edges.push_back(Edge(from, to, cap, 0));
    edges.push_back(Edge(to, from, 0, 0));
    int w = edges.size();
    G[from].push_back(w - 2);
    G[to].push_back(w - 1);
}
bool BFS()
{
    memset(vis, 0, sizeof(vis));
    queue<int>Q;
    Q.push(s);
    d[s] = 0;
    vis[s] = 1;
    while (!Q.empty())
    {
        int x = Q.front();
        Q.pop();
        for (int i = 0; i<G[x].size(); i++)
        {
            Edge e = edges[G[x][i]];
            if (!vis[e.to] && e.cap>e.flow)
            {
                vis[e.to] = 1;
                d[e.to] = d[x] + 1;
                Q.push(e.to);
            }
        }
    }
    return vis[t];
}
int DFS(int x, int a)
{
    if (x == t || a == 0)
        return a;
    int flow = 0, f;
    for (int &i = cur[x]; i<G[x].size(); i++)
    {
        Edge e = edges[G[x][i]];
        if (d[x]+1 == d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>0)
        {
            edges[G[x][i]].flow+=f;
            edges[G[x][i] ^ 1].flow-=f;
            flow+=f;
            a-=f;
            if(a==0) break;
        }
    }
    if(!flow) d[x] = -1;
    return flow;
}
int dinic(int s, int t)
{
    int flow = 0;
    while (BFS())
    {
        memset(cur, 0, sizeof(cur));
        flow += DFS(s, INF);
    }
    return flow;
}

int check(int limit)
{
    memset(flag,0,sizeof flag);
    memset(se,0,sizeof se);
    for(int i=1;i<=n;i++) if(p[i].t<=limit) flag[i]=1;

    init();

    s=0; t=n+m+1;

    int sum=0;

    for(int i=1;i<=m;i++)
    {
        bool fail=0;
        for(int j=0;j<g[i].size();j++)  if(flag[g[i][j]]==0) fail=1;
        if(fail==1) continue;

        for(int j=0;j<g[i].size();j++) AddEdge(i,g[i][j]+m,INF);

        AddEdge(s,i,pro[i]);
        sum=sum+pro[i];
    }

    for(int i=1;i<=n;i++) AddEdge(i+m,t,p[i].pay);

    return sum-dinic(s,t);
}

int main()
{
    //File();
    scanf("%d",&T); int cas=1;
    while(T--)
    {
        scanf("%d%d%d",&n,&m,&L);
        for(int i=0;i<=m;i++) g[i].clear();
        for(int i=1;i<=n;i++) scanf("%d%d",&p[i].pay,&p[i].t);
        for(int i=1;i<=m;i++)
        {
            scanf("%d",&pro[i]);
            int k; scanf("%d",&k);
            while(k--)
            {
                int x; scanf("%d",&x);
                g[i].push_back(x);
            }
        }
        int left=0,right=1000000000;

        int ans1=-1,ans2=-1;
        while(left<=right)
        {
            int mid=(left+right)/2;
            int ppp=check(mid);
            if(ppp>=L)
            {
                right=mid-1;
                ans1=mid;
                ans2=ppp;
            }
            else
                left=mid+1;
        }
        printf("Case #%d: ",cas++);
        if(ans1==-1) printf("impossible\n");
        else printf("%d %d\n",ans1,ans2);
    }
    return 0;
}
时间: 2024-10-20 13:51:34

HDU 5855 Less Time, More profit的相关文章

HDU 5855 Less Time, More profit(最大权闭合子图)

题目链接:点击打开链接 思路: 最大权闭合子图的裸题,  给个学习资料:点击打开链接 当结点即有正权值又有负数权值时, 怎么求任意闭合子图的最大和呢?  只要求出最小割E, 用总的正数权值TOT 减去E就是答案. 细节参见代码: #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<string> #include<vector&

【HDU 5855】Less Time, More profit(网络流、最小割、最大权闭合子图)

Less Time, More profit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description The city planners plan to build N plants in the city which has M shops. Each shop needs products from some plants to make p

hdu 5052 Yaoge’s maximum profit

Yaoge’s maximum profit Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 442    Accepted Submission(s): 122 Problem Description Yaoge likes to eat chicken chops late at night. Yaoge has eaten too

Hdu 5052 Yaoge’s maximum profit(树链剖分)

题目大意: 给出一棵树,每个点有商店,每个商店都有一个价格,Yaoge每次从x走到y都可以在一个倒卖商品,从中得取利益,当然,买一顶要在卖之前.但是没次走过一条路,这条路上的所有商品都会增加一个v. 输出每次的最大利益. 思路分析: 很容易想到树链剖分,可是关键在于如何维护这样一个变量,使得每次都要让买的再卖的前面. 维护变量 ltr 和 rtl ,表示从左去右和从右去左. 剖分熟练的时候,判断x 和 y的深度,一步一步往上爬. 然后维护区间最大值和最小值,爬的时候更新答案. ...4921ms

HDU 5052 Yaoge’s maximum profit 裸树链剖分 2014 ACM/ICPC Asia Regional Shanghai Online

题意: 给定n个点的带点权树. 下面n行给出每个点点权表示每个点买卖鸡腿的价格 下面n-1行给出树边 下面Q个操作 Q行 u, v, val 从u走到v,过程中可以买一个鸡腿,然后到后面卖掉,输出max(0, 最大的收益) 然后给[u,v]路径上点点权+=val 思路: 树链剖分裸题 屌丝题解:点击打开链接 #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include &

HDU5855 Less Time, More profit(最大权闭合子图)

题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5855 Description The city planners plan to build N plants in the city which has M shops. Each shop needs products from some plants to make profit of proi units. Building ith plant needs investment o

HDU 1217 Arbitrage 【最短路,map+spfa】

Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6985    Accepted Submission(s): 3212 Problem Description Arbitrage is the use of discrepancies in currency exchange rates to transform

HDU 1505 City Game (hdu1506 dp二维加强版)

F - City Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1505 Appoint description: Description Bob is a strategy game programming specialist. In his new city building game the gaming enviro

hdu 1505

City Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4516    Accepted Submission(s): 1909 Problem Description Bob is a strategy game programming specialist. In his new city building game th